Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SD card attached to SPI bus as follows:
- // MOSI - pin 11
- // MISO - pin 12
- // CLK - pin 13
- // CS - pin 4
- //
- #define DHTPIN 8
- #define CSPIN 4
- #define SSPIN 10
- #define DHTTYPE DHT22
- // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
- #include "RTClib.h"
- #include <DHT.h>
- #include <DHT_U.h>
- #include <Adafruit_Sensor.h>
- #include <SPI.h>
- #include <SD.h>
- DHT dht(DHTPIN, DHTTYPE);
- RTC_DS1307 rtc;
- char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
- File logFile;
- bool fileLogged = false;
- void setup () {
- while (!Serial); // for Leonardo/Micro/Zero
- dht.begin();
- Serial.begin(9600);
- Serial.println("Initializing RTC...");
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- return;
- }
- Serial.println("Running RTC...");
- 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(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- return;
- }
- Serial.println("Initializing SD card...");
- // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
- // Note that even if it's not used as the CS pin, the hardware SS pin
- // (10 on most Arduino boards, 53 on the Mega) must be left as an output
- // or the SD library functions will not work.
- pinMode(SSPIN, OUTPUT);
- if (!SD.begin(CSPIN)) { // Initialize the SD card
- Serial.println("SD initialization FAILED!");
- return;
- }
- logFile = SD.open("log.txt", FILE_WRITE); // Open the log.txt file for writing
- if (!logFile) { // If file didn't open successfully
- Serial.println("ERROR opening log.txt on SD card!");
- return;
- }
- }
- void loop () {
- DateTime now = rtc.now();
- float u = dht.readHumidity();
- float t = dht.readTemperature();
- String log = String(now.day()) + "/" + String(now.month()) + "/" + String(now.year()) + ";"; // Concat day/month/year
- log += String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()) + ";"; // Concat hour/min/sec
- log += String(u) + ";"; // Concat humidity
- log += String(t); // Concat temp
- Serial.println(log); // Print log string to serial
- if (!fileLogged) { // If the log hasn't been written to the SD card yet
- fileLogged = true;
- logFile.println(log); // Write log string to log.txt on SD card
- logFile.close(); // Close the file
- }
- delay(1800000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement