Advertisement
Seelenkind

Arduino Datalogger with Timestamp

Mar 29th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <SD.h>
  3. #include <Wire.h>
  4. #include <TimeLib.h>
  5. #include <DS1307RTC.h>
  6. #include <LiquidCrystal_I2C.h>
  7.  
  8. File myFile;
  9. LiquidCrystal_I2C lcd(0x27, 20, 4);
  10.  
  11. const char *monthName[12] = {
  12.   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  13.   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  14. };
  15.  
  16. tmElements_t tm;
  17.  
  18. void setup() {
  19.   bool parse = false;
  20.   bool config = false;
  21.  
  22.   // get the date and time the compiler was run
  23.   if (getDate(__DATE__) && getTime(__TIME__)) {
  24.     parse = true;
  25.     // and configure the RTC with this info
  26.     if (RTC.write(tm)) {
  27.       config = true;
  28.     }
  29.   }
  30.  
  31.   if (parse && config) {
  32.     Serial.print("DS1307 configured Time=");
  33.     Serial.print(__TIME__);
  34.     Serial.print(", Date=");
  35.     Serial.println(__DATE__);
  36.   } else if (parse) {
  37.     Serial.println("DS1307 Communication Error :-{");
  38.     Serial.println("Please check your circuitry");
  39.   } else {
  40.     Serial.print("Could not parse info from the compiler, Time=\"");
  41.     Serial.print(__TIME__);
  42.     Serial.print("\", Date=\"");
  43.     Serial.print(__DATE__);
  44.     Serial.println("\"");
  45.   }
  46.  
  47.   Serial.begin(9600);
  48.   lcd.init();
  49.   while (!Serial) ; // wait for serial
  50.   Serial.println("Initializing SD card...");
  51.   if (!SD.begin(4)) {
  52.     Serial.println("initialization failed!");
  53.     while (1);
  54.   }
  55.  
  56.  
  57.  
  58.  
  59.  
  60. }
  61.  
  62. void loop() {
  63.   String data;
  64.  
  65.   if (RTC.read(tm)) {
  66.  
  67.     myFile = SD.open("test.txt", FILE_WRITE); // test.txt durch RFID Tag ersetzen FileName = String(RfidTag)+".txt"
  68.     if (myFile) {
  69.       data = checkDigits(tm.Hour) + ":" + checkDigits(tm.Minute) + ":" + checkDigits(tm.Second);
  70.       Serial.println(data);
  71.       myFile.println(data);
  72.       lcd.backlight();
  73.       lcd.clear();
  74.       lcd.print(data);
  75.       myFile.close();
  76.       data = "";
  77.     } else {
  78.       // if the file didn't open, print an error:
  79.       Serial.println("error opening test.txt");
  80.     }
  81.   } else {
  82.     Serial.println("Uhr läuft nicht");
  83.   }
  84.   delay(1000);
  85.  
  86.   myFile = SD.open("test.txt");
  87.   if (myFile) {
  88.     Serial.println("File opened");
  89.     // read from the file until there's nothing else in it:
  90.     while (myFile.available()) {
  91.       Serial.write(myFile.read());
  92.  
  93.     }
  94.     // close the file:
  95.     myFile.close();
  96.    
  97.   } else {
  98.     // if the file didn't open, print an error:
  99.     Serial.println("error opening test.txt");
  100.   }
  101.  
  102. }
  103.  
  104. String checkDigits(int number) {
  105.   String DtZeit;
  106.   DtZeit = String (number);
  107.   if (number >= 0 && number < 10) {
  108.     DtZeit = "0" + String(number);
  109.  
  110.   }
  111.   return DtZeit;
  112. }
  113.  
  114. bool getTime(const char *str)
  115. {
  116.   int Hour, Min, Sec;
  117.  
  118.   if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  119.   tm.Hour = Hour;
  120.   tm.Minute = Min;
  121.   tm.Second = Sec;
  122.   return true;
  123. }
  124.  
  125. bool getDate(const char *str)
  126. {
  127.   char Month[12];
  128.   int Day, Year;
  129.   uint8_t monthIndex;
  130.  
  131.   if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  132.   for (monthIndex = 0; monthIndex < 12; monthIndex++) {
  133.     if (strcmp(Month, monthName[monthIndex]) == 0) break;
  134.   }
  135.   if (monthIndex >= 12) return false;
  136.   tm.Day = Day;
  137.   tm.Month = monthIndex + 1;
  138.   tm.Year = CalendarYrToTm(Year);
  139.   return true;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement