Advertisement
eduardobio

Datalogger sktech

Feb 29th, 2020
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. //SD card attached to SPI bus as follows:
  2. // MOSI - pin 11
  3. // MISO - pin 12
  4. // CLK - pin 13
  5. // CS - pin 4
  6. //
  7.  
  8. #define DHTPIN 8
  9. #define CSPIN 4
  10. #define SSPIN 10
  11. #define DHTTYPE DHT22
  12.  
  13. // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
  14. #include "RTClib.h"
  15. #include <DHT.h>
  16. #include <DHT_U.h>
  17. #include <Adafruit_Sensor.h>
  18. #include <SPI.h>
  19. #include <SD.h>
  20.  
  21. DHT dht(DHTPIN, DHTTYPE);
  22. RTC_DS1307 rtc;
  23. char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  24. File logFile;
  25. bool fileLogged = false;
  26.  
  27. void setup () {
  28.   while (!Serial); // for Leonardo/Micro/Zero
  29.  
  30.   dht.begin();
  31.   Serial.begin(9600);
  32.  
  33.   Serial.println("Initializing RTC...");
  34.   if (! rtc.begin()) {
  35.     Serial.println("Couldn't find RTC");
  36.     return;
  37.   }
  38.  
  39.   Serial.println("Running RTC...");
  40.   if (! rtc.isrunning()) {
  41.     Serial.println("RTC is NOT running!");
  42.     // following line sets the RTC to the date & time this sketch was compiled
  43.     //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  44.     // This line sets the RTC with an explicit date & time, for example to set
  45.     // January 21, 2014 at 3am you would call:
  46.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  47.  
  48.     return;
  49.   }
  50.  
  51.   Serial.println("Initializing SD card...");
  52.   // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  53.   // Note that even if it's not used as the CS pin, the hardware SS pin
  54.   // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  55.   // or the SD library functions will not work.
  56.   pinMode(SSPIN, OUTPUT);
  57.  
  58.   if (!SD.begin(CSPIN)) { // Initialize the SD card
  59.     Serial.println("SD initialization FAILED!");
  60.     return;
  61.   }
  62.   logFile = SD.open("log.txt", FILE_WRITE); // Open the log.txt file for writing
  63.  
  64.   if (!logFile) { // If file didn't open successfully
  65.     Serial.println("ERROR opening log.txt on SD card!");
  66.     return;
  67.   }
  68. }
  69.  
  70. void loop () {
  71.   DateTime now = rtc.now();
  72.   float u = dht.readHumidity();
  73.   float t = dht.readTemperature();
  74.  
  75.   String  log = String(now.day()) + "/" + String(now.month()) + "/" + String(now.year()) + ";"; // Concat day/month/year
  76.   log += String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()) + ";"; // Concat hour/min/sec
  77.   log += String(u) + ";"; // Concat humidity
  78.   log += String(t); // Concat temp
  79.  
  80.   Serial.println(log); // Print log string to serial
  81.  
  82.   if (!fileLogged) { // If the log hasn't been written to the SD card yet
  83.     fileLogged = true;
  84.     logFile.println(log); // Write log string to log.txt on SD card
  85.     logFile.close(); // Close the file
  86.   }
  87.  
  88.   delay(1800000);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement