//Arduino datalogger con SD Card e DHT-11 per memorizzare
//i dati di temperatura e umidità su SD Card.
//Per maggiori info: www.progettiarduino.com
// libreria sensore DHT-11
#include <DHT.h>
// librerie già presenti di default nell`Arduino IDE
#include <SPI.h>
#include <SD.h>
//Orologio di sistema.
#include <RTClib.h>
RTC_DS3231 rtc;
//char t[32];
//Display LCD 20x4 i2C:
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
// Wiring: SDA pin is connected to A4 and SCL pin to A5.
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,20,4) for 20x4 LCD.
// Variabili globali
int DHTPIN = 2;
int lcdLine, lcdRow = 0;
const int chipSelect = 4;
unsigned long Secs=0;
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Make custom characters:
byte Heart[] = {
B00000,
B01010,
B11111,
B11111,
B01110,
B00100,
B00000,
B00000
};
byte Bell[] = {
B00100,
B01110,
B01110,
B01110,
B11111,
B00000,
B00100,
B00000
};
byte Alien[] = {
B11111,
B10101,
B11111,
B11111,
B01110,
B01010,
B11011,
B00000
};
byte Check[] = {
B00000,
B00001,
B00011,
B10110,
B11100,
B01000,
B00000,
B00000
};
byte Speaker[] = {
B00001,
B00011,
B01111,
B01111,
B01111,
B00011,
B00001,
B00000
};
byte Sound[] = {
B00001,
B00011,
B00101,
B01001,
B01001,
B01011,
B11011,
B11000
};
byte Skull[] = {
B00000,
B01110,
B10101,
B11011,
B01110,
B01110,
B00000,
B00000
};
byte Lock[] = {
B01110,
B10001,
B10001,
B11111,
B11011,
B11011,
B11111,
B00000
};
byte Celsius[] = {
B01100,
B10010,
B10010,
B01100,
B00000,
B00000,
B00000,
B00000
};
void setup() {
// Aprire la comunicazione seriale
Serial.begin(9600);
while (!Serial) {
; // Attendere l`apertura della comunicazione seriale
}
//Orologio di sistema
rtc.begin();
rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
//rtc.adjust(DateTime(2019, 1, 21, 5, 0, 0));
//rtc.adjust(DateTime(__DATE__, __TIME__));
dht.begin();
// Initiate the LCD:
lcd.init();
CreateCustomChar();
lcd.backlight();
//lcd.autoscroll();
lcd.setCursor(lcdRow, lcdLine);
lcd.write(3);
lcd.print(" Avvio Sistema ...");
lcdLine += 1;
lcd.setCursor(lcdRow, lcdLine);
//Serial.print("Inizializzazione SD card...");
// Se la card è presente inizia
if (!SD.begin(chipSelect)) {
//Serial.println("SD Card non valida o non presente");
lcd.print("Err SD Card");
lcdLine += 1;
lcd.setCursor(lcdRow, lcdLine);
// non eseguire più il codice
//return;
}
//for(int x = 0; x < 255; x++) {
// Serial.println(String(x) + " - " + String(char(176)));
// }
delay(3000); // Ogni 3 secondi
lcd.clear();
}
void loop() {
//Orario di sistema:
DateTime now = rtc.now();
//sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
//Serial.print(F("Date/Time: "));
//Serial.println(t);
//Display
lcdRow = lcdLine = 0;
lcd.setCursor(lcdRow, lcdLine);
// crea una stringa per assemblare i dati di log
String dataTXTString = "";
// ed una stringa per assemblare i dati per il diplay
//String dataLCDString = "";
// Leggi UMIDITA
int h = dht.readHumidity();
// Leggi VENTO
int v = 00; // m/s
// PERCENT. BATTERIA:
int b = 00; // m/s
// Leggi la TEMPERATURA in Celsius (di default)
int t = dht.readTemperature(false); // true= fahrenheit false= celsius
unsigned long Secs=millis();
//Serial.println(lcdLine);
//DATA
String anno = String(now.year());
if(anno.length() < 2){ anno = "0" + anno; }
String mese = String(now.month());
if(mese.length() < 2){ mese = "0" + mese; }
String giorno = String(now.day());
if(giorno.length() < 2){ giorno = "0" + giorno; }
String ore = String(now.hour());
if(ore.length() < 2){ ore = "0" + ore; }
String minuti = String(now.minute());
if(minuti.length() < 2){ minuti = "0" + minuti; }
String secondi = String(now.second());
if(secondi.length() < 2){ secondi = "0" + secondi; }
String strData =giorno + "/" + mese + "/" + anno + " " + ore + ":" + minuti;
int len = 20 - strData.length();
for(int x = 0; x < len; x++) {strData = strData + " ";}
lcd.print(strData);
lcdLine += 1;
lcd.setCursor(lcdRow, lcdLine);
//delay(3000);
//Serial.println(lcdLine);
//TEMPERATURA E UMIDITA
lcd.print("Temp: " + String(t));
lcd.write(8);
strData = " Ur: " + String(h) + "%";
len = 20 - String(t).length() - 7 - strData.length();
for(int x = 0; x < len; x++) {strData = strData + " ";}
lcd.print(strData);
lcdLine += 1;
lcd.setCursor(lcdRow, lcdLine);
//delay(3000);
//Serial.println(lcdLine);
//VENTO
strData = "Vento: " + String(v) + " m/s";
len = 20 - strData.length();
for(int x = 0; x < len; x++) {strData = strData + " ";}
lcd.print(strData);
lcdLine += 1;
lcd.setCursor(lcdRow, lcdLine);
//delay(3000);
//PERCENT. BATTERIA
//strData = "Batteria: " + String(b) + "%";
//len = 20 - strData.length();
//for(int x = 0; x < len; x++) {strData = strData + " ";}
//Serial.println(lcdLine);
//lcd.print(strData);
//lcdLine += 1;
//lcd.setCursor(lcdRow, lcdLine);
//Serial.println(lcdLine);
//delay(3000);
dataTXTString = anno + mese + giorno + ore + minuti + secondi + ";"+String(t)+";"+String(h)+";"+String(v);
//dataTXTString = String(Secs)+","+String(h)+","+String(t);
// Aprire il file. Da notare che puòessere apertoun solo file alla volta,
// quindi bisogna chiudere questa prima di aprirne un'altra.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// Se è presente il file, scrivi su i millisecondi,umidità, temperatura:
if (dataFile) {
dataFile.println(dataTXTString);
dataFile.close();
// Scrivi sul monitor seriale i millisecondi,umidità, temperatura:
Serial.println(dataTXTString);
//lcd.print(dataTXTString);
//lcdLine += 1;
//lcd.setCursor(lcdRow, lcdLine);
delay(3000); // Ogni 3 secondi
}
// Se il file non è aperto, scrivi sul monitor seriale:
else {
//Serial.println("Errore apertura datalog.txt");
lcd.print("Errore apertura datalog.txt");
lcdLine += 1;
lcd.setCursor(lcdRow, lcdLine);
}
//lcd.setCursor(0, 0);
//lcd.clear();
}
void CreateCustomChar()
{
// Create new characters:
lcd.createChar(0, Heart);
lcd.createChar(1, Bell);
lcd.createChar(2, Alien);
lcd.createChar(3, Check);
lcd.createChar(4, Speaker);
lcd.createChar(5, Sound);
lcd.createChar(6, Skull);
lcd.createChar(7, Lock);
lcd.createChar(8, Celsius);
}