Primul programior (sketch) pe care l-am testat este "ReadWrite", cel in care se creeaza un fisier text, se scrie in el ceva, se salveaza, apoi se deschide si se citeste textul..
am modificat repede textul de scris, obtinand:
Am deschis si programiorul din exemplele librariei SD, numit "CardInfo":
Avand deja realizata partea de indicare pe ecranul LCD 16x2 a temperaturii si umiditatatii oferite de senzorul DTH11, a orei si datei oferite de ceasul de timp real DS1307, am conectat si placa de retea care are si inscriptor de memorie microSD.
Dupa combinarea mai multor sketch-uri si ceva "sudoare", am realizat partea de scriere pe monitorizarea seriala si intr-un fisier pe cardul de memorie microSD:
Sketch-ul meu este:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// original sketck from http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/
// add part with SQW=1Hz from http://tronixstuff.wordpress.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/
// add part with LCD from http://blog.gotencool.com/2012/03/arduino-lcd-via-i2c.html
// add part wirh LCD special character http://www.roroid.ro/wiki/pmwiki.php/Main/KitLCDUtilizare
// adapted sketch for DHT11 by by niq_ro from http://nicuflorica.blogspot.ro/
// version 5.0
#include <SD.h>
const int chipSelect = 4;
#include <DHT.h>
#define DHTPIN A2 // what pin we're connected DHT11
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2); // 0x20 is adresss for LCC 16x2
RTC_DS1307 RTC;
byte picatura[8] = {
0b00100, 0b00100, 0b01010, 0b01010, 0b10001, 0b10001, 0b10001, 0b01110
};//Definim imaginea ce dorim sa o reprezentam in binar
void setup () {
dht.begin();
lcd.init();
lcd.createChar(0, picatura);//Se creeaza caracterul nostru
lcd.backlight(); //backlight is now ON
// set up the LCD's number of columns and rows:
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// }
lcd.begin(16, 2);
// Print a logo message to the LCD.
lcd.setCursor(0, 0);
lcd.print("www.tehnic.go.ro");
lcd.setCursor(0, 1);
lcd.print("creat de niq_ro");
delay (2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ceas cu calendar");
lcd.setCursor(0, 1);
lcd.print("temp. si umidit.");
delay (2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ceas cu calendar");
lcd.setCursor(2, 1);
lcd.print("versiunea 5.1");
delay (2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("scriere date pe");
lcd.setCursor(0, 1);
lcd.print("card microSD");
delay (2500);
lcd.clear();
Wire.begin();
// part code from http://tronixstuff.wordpress.com/
Wire.beginTransmission(0x68);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
// end part code from http://tronixstuff.wordpress.com/
RTC.begin();
// RTC.adjust(DateTime(__DATE__, __TIME__));
// if you need set clock... just remove // from line above this
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
int qiu=1;
void loop () {
qiu = qiu + 1;
DateTime now = RTC.now();
int h = dht.readHumidity();
int t = dht.readTemperature();
int hz = h/10;
int hu = h - hz*10;
lcd.setCursor(1, 0);
if ( now.hour() < 10)
{
lcd.print(" ");
lcd.print(now.hour(), DEC);
}
else
{
lcd.print(now.hour(), DEC);
}
lcd.print(":");
if ( now.minute() < 10)
{
lcd.print("0");
lcd.print(now.minute(), DEC);
}
else
{
lcd.print(now.minute(), DEC);
}
lcd.print(":");
if ( now.second() < 10)
{
lcd.print("0");
lcd.print(now.second(), DEC);
}
else
{
lcd.print(now.second(), DEC);
}
lcd.print(" ");
lcd.setCursor(12, 0);
if ( t < 10)
{
lcd.print(" ");
lcd.print(t);
}
else
{
//lcd.print(qiu);
lcd.print(t);
}
lcd.write(0b11011111);
lcd.print("C");
lcd.setCursor(0, 1);
if ( now.day() < 10)
{
lcd.print("0");
lcd.print(now.day(), DEC);
}
else
{
lcd.print(now.day(), DEC);
}
lcd.print("/");
if ( now.month() < 10)
{
lcd.print("0");
lcd.print(now.month(), DEC);
}
else
{
lcd.print(now.month(), DEC);
}
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.print("");
lcd.setCursor(12, 1);
lcd.print(hz);
lcd.print(hu);
lcd.print("%");
lcd.print(char(0));//tiparim caracterul nou creat
if (qiu > 60)
{
// this is part who I, niq_ro, work hard :-)))))))))
// make a string for name of txt file
String fisier = "";
// put the year
fisier += String(now.year(), DEC);
fisier += "/";
// put the month
fisier += String(now.month(), DEC);
fisier += "/";
// put the day
fisier += String(now.day(), DEC);
fisier += "-";
String fisier2 = "";
// put the hour
fisier2 += String(now.hour(), DEC);
fisier2 += ":";
// put the minute
fisier2 += String(now.minute(), DEC);
fisier2 += ":";
// put the second
fisier2 += String(now.second(), DEC);
fisier2 += "- ";
// make a string for assembling the data to log:
String dataString = "";
// put the temperature
dataString += "t=";
dataString += String(t);
dataString += "^C";
// put the humidity
dataString += " h=";
dataString += String(h);
dataString += "%";
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("fisier10.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(fisier);
dataFile.print(fisier2);
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.print(fisier);
Serial.print(fisier2);
Serial.println(dataString);
//Serial.println(qiu);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening fisier10.txt");
}
qiu = 0;
}
delay(500);
}
Am postat si un mic filmulet, care se numeste humidity, temperature, hour and data recorded on microSD using Ethernet shield with Arduino
Dupa ce am lasat conectat montajul mai multe ore, am constatat uneori ca apar erori la citirea datelor din RTC (ore si minute) si apoi la scriere, m-am gandit e cam mare consumul, asa ca am inlocuit cablul usb cu prelungitor cu un cablu de cca. 1m de la un scanner si, totodata, sting lumina de fundal al afisajului cu cristale lichide cand se face scrierea...
O alta solutie, pentru reducerea consumului, este aceea in care sa folosesc doar un modul de microSD...
Sketch-ul modificat este:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// original sketck from http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/
// add part with SQW=1Hz from http://tronixstuff.wordpress.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/
// add part with LCD from http://blog.gotencool.com/2012/03/arduino-lcd-via-i2c.html
// add part wirh LCD special character http://www.roroid.ro/wiki/pmwiki.php/Main/KitLCDUtilizare
// adapted sketch for DHT11 by by niq_ro from http://nicuflorica.blogspot.ro/
// version 5.3
#include <SD.h>
const int chipSelect = 4;
#include <DHT.h>
#define DHTPIN A2 // what pin we're connected DHT11
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2); // 0x20 is adresss for LCC 16x2
RTC_DS1307 RTC;
byte picatura[8] = {
0b00100, 0b00100, 0b01010, 0b01010, 0b10001, 0b10001, 0b10001, 0b01110
};//Definim imaginea ce dorim sa o reprezentam in binar
void setup () {
dht.begin();
lcd.init();
lcd.createChar(0, picatura);//Se creeaza caracterul nostru
lcd.backlight(); //backlight is now ON
// set up the LCD's number of columns and rows:
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// }
lcd.begin(16, 2);
// Print a logo message to the LCD.
lcd.setCursor(0, 0);
lcd.print("www.tehnic.go.ro");
lcd.setCursor(0, 1);
lcd.print("creat de niq_ro");
delay (2500);
lcd.clear();
lcd.noBacklight(); //backlight is now OFF
lcd.clear();
delay(200);
lcd.setCursor(0, 0);
lcd.print("ceas cu calendar");
lcd.setCursor(0, 1);
lcd.print("temp. si umidit.");
delay (2500);
lcd.noBacklight(); //backlight is now OFF
lcd.clear();
delay(200);
lcd.backlight(); //backlight is now OFF
lcd.setCursor(0, 0);
lcd.print("ceas cu calendar");
lcd.setCursor(2, 1);
lcd.print("versiunea 5.3");
delay (2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("scriere date pe");
lcd.setCursor(0, 1);
lcd.print("card microSD");
delay (2500);
lcd.clear();
Wire.begin();
// part code from http://tronixstuff.wordpress.com/
Wire.beginTransmission(0x68);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
// end part code from http://tronixstuff.wordpress.com/
RTC.begin();
// RTC.adjust(DateTime(__DATE__, __TIME__));
// if you need set clock... just remove // from line above this
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
int qiu=1;
void loop () {
qiu = qiu + 1;
DateTime now = RTC.now();
int h = dht.readHumidity();
int t = dht.readTemperature();
int hz = h/10;
int hu = h - hz*10;
lcd.setCursor(1, 0);
if ( now.hour() < 10)
{
lcd.print(" ");
lcd.print(now.hour(), DEC);
}
else
{
lcd.print(now.hour(), DEC);
}
lcd.print(":");
if ( now.minute() < 10)
{
lcd.print("0");
lcd.print(now.minute(), DEC);
}
else
{
lcd.print(now.minute(), DEC);
}
lcd.print(":");
if ( now.second() < 10)
{
lcd.print("0");
lcd.print(now.second(), DEC);
}
else
{
lcd.print(now.second(), DEC);
}
lcd.print(" ");
lcd.setCursor(12, 0);
if ( t < 10)
{
lcd.print(" ");
lcd.print(t);
}
else
{
//lcd.print(qiu);
lcd.print(t);
}
lcd.write(0b11011111);
lcd.print("C");
lcd.setCursor(0, 1);
if ( now.day() < 10)
{
lcd.print("0");
lcd.print(now.day(), DEC);
}
else
{
lcd.print(now.day(), DEC);
}
lcd.print("/");
if ( now.month() < 10)
{
lcd.print("0");
lcd.print(now.month(), DEC);
}
else
{
lcd.print(now.month(), DEC);
}
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.print("");
lcd.setCursor(12, 1);
lcd.print(hz);
lcd.print(hu);
lcd.print("%");
lcd.print(char(0));//tiparim caracterul nou creat
if (qiu > 60)
{
// this is part who I, niq_ro, work hard :-)))))))))
// make a string for name of txt file
String fisier = "";
// put the year
fisier += String(now.year(), DEC);
fisier += "/";
// put the month
fisier += String(now.month(), DEC);
fisier += "/";
// put the day
fisier += String(now.day(), DEC);
fisier += "-";
String fisier2 = "";
// put the hour
fisier2 += String(now.hour(), DEC);
fisier2 += ":";
// put the minute
fisier2 += String(now.minute(), DEC);
fisier2 += ":";
// put the second
fisier2 += String(now.second(), DEC);
fisier2 += "- ";
// make a string for assembling the data to log:
String dataString = "";
// put the temperature
dataString += "t=";
dataString += String(t);
dataString += "^C";
// put the humidity
dataString += " h=";
dataString += String(h);
dataString += "%";
// background should be off to reduce power consumption
lcd.noBacklight(); //backlight is now OFF
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("fisier19.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(fisier);
dataFile.print(fisier2);
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.print(fisier);
Serial.print(fisier2);
Serial.println(dataString);
//Serial.println(qiu);
lcd.backlight(); //backlight is now ON
lcd.setCursor(0, 0);
lcd.print("*");
delay(500);
lcd.setCursor(0, 0);
lcd.print(" ");
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening fisier19.txt");
lcd.backlight(); //backlight is now ON
lcd.setCursor(0, 0);
lcd.print("!");
delay(100);
lcd.setCursor(0, 0);
lcd.print(" ");
}
qiu = 0;
}
delay(500);
}
Filmuletul, care prezinta cum se comporta acest montaj, se numeste humidity, temperature, hour and data recorded on microSD using Ethernet shield with Arduino (II):
PS: daca se doreste salvarea datelor mai rar sau mai des, schimbati valoarea parametrulului qiu din linia cu if.
if (qiu > 60)
Daca valoarea se pune 600, salvarea se va face la cca. 60 de secunde (10 minute)....
PS: schema de conectare este clasica, doar ca am folosit suplimentar shield-ul de retea si inscriptorde memmorii SD:
Niciun comentariu:
Trimiteți un comentariu