Advertisement
Giampiero

Arduino Logger

Aug 20th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SD.h>
  2. #include <RTClib.h>
  3. #include <DHT.h>
  4. #define DHTPIN 9
  5. #define DHTTYPE DHT22
  6. DHT dht(DHTPIN, DHTTYPE);
  7. File file;
  8. char hms[20];
  9. char gma[20];
  10. RTC_DS1307 rtc;
  11. String nomefile;
  12. float temp = dht.readTemperature();
  13. float umid = dht.readHumidity();
  14. float pre_temp = 0;
  15. float pre_umid = 0;
  16.  
  17. void setup() {
  18.   rtc.begin();
  19.   if (! rtc.isrunning()) {
  20.     rtc.adjust(DateTime(__DATE__, __TIME__));
  21.   }
  22.   SD.begin(4);
  23.   if (SD.begin(4) == true);
  24.   Serial.begin(9600);
  25. }
  26.  
  27. void loop() {
  28.   DateTime now = rtc.now();
  29.   temp = dht.readTemperature();
  30.   umid = dht.readHumidity();
  31.   sprintf(hms, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
  32.   sprintf(gma, "%02d/%02d/%d", now.day(), now.month(), now.year());
  33.   Serial.println(hms);
  34.   Serial.print(" ");
  35.   Serial.print(gma);
  36.   gestione_sd();
  37.   if (pre_temp != temp || pre_umid != umid) {
  38.     scrivi_valori();
  39.   }
  40.   delay(100);
  41. }
  42.  
  43. int gestione_sd() {
  44.   String nomefile = "file_", gma;
  45.   file = SD.open(nomefile, FILE_WRITE | O_APPEND);
  46.   file.close();
  47.   Serial.println("nome file");
  48.   Serial.println(nomefile);
  49. }
  50.  
  51. int scrivi_valori() {
  52.   Serial.println(temp);
  53.   Serial.print(" ");
  54.   Serial.print(umid);
  55.   file.println();
  56.   file.print(hms);
  57.   file.print(",  Temp: ");
  58.   file.print(temp);
  59.   file.print("°C,  Hum: ");
  60.   file.print(umid);
  61.   file.print("%");
  62.   pre_temp = temp;
  63.   pre_umid = umid;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement