#include "DHT.h" #define DHTPIN 2 // what pin we're connected to // Uncomment whatever type you're using! #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) // Connect pin 1 (on the left) of the sensor to +5V // Connect pin 2 of the sensor to whatever your DHTPIN is // Connect pin 4 (on the right) of the sensor to GROUND // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor DHT dht(dhtpin, DHTTYPE); // Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <Wire.h> #include "RTClib.h" RTC_DS1307 rtc; /* SD card attached to SPI bus as follows: ** MOSI - pin 11 ** MISO - pin 12 ** CLK - pin 13 ** CS - pin 10 */ #include <SD.h> File myfile; // variables long currentmillis = 0; long previousmillis = 0; long interval = 60000; float humidity; float temperature; int LDRPin = A1; void setup() { Serial.begin(9600); Serial.println("Datalogger Initiating...");
Serial.println("Temperature Sensor initiating..."); dht.begin(); #ifdef AVR Wire.begin(); #else Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due #endif Serial.println("Temperature Sensor ready"); Serial.println("Real Time Clock Initiating..."); //rtc.adjust(datetime( DATE, TIME )); //comment out this line unless the date/time needs updated 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( DATE, TIME )); Serial.println("Real Time Clock ready."); DateTime now = rtc.now(); Serial.print("date/time: "); Serial.print(now.year(), DEC); Serial.print(now.month(), DEC); Serial.print(now.day(), DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(now.minute(), DEC); Serial.print(now.second(), DEC); Serial.println(" "); Serial.println("SD Card initiating..."); // 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(10, OUTPUT);
if (!SD.begin(10)) { Serial.println("initialization failed!"); return; Serial.println("SD Card ready."); // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. myfile = SD.open("log.txt", FILE_WRITE); // if the file opened okay, write to it: if (myfile) { Serial.println("Writing to log"); myfile.print("date"); myfile.print("time"); myfile.print("time elapsed"); myfile.print("temp"); myfile.print("humidity"); myfile.println("light"); // close the file: myfile.close(); Serial.println("Write complete"); else { // if the file didn't open, print an error: Serial.println("error opening Log.txt"); Serial.println("Datalogger initialisaion complete."); Serial.println(""); void loop() { currentmillis = millis(); //Serial.println(currentMillis); if (currentmillis - previousmillis > interval){ // save the last time you measured temp and saved
previousmillis = previousmillis + 60000; Serial.println("time interval elapsed"); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) humidity = dht.readhumidity(); temperature = dht.readtemperature() - 2; // check if returns are valid, if they are NaN (not a number) then something went wrong! if (isnan(temperature) isnan(humidity)) { Serial.println("Failed to read from DHT"); else { DateTime now = rtc.now(); Serial.print("date/time: "); Serial.print(now.year(), DEC); Serial.print(now.month(), DEC); Serial.print(now.day(), DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(now.minute(), DEC); Serial.print(now.second(), DEC); Serial.println(" "); Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %\t"); Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" *C"); // read values from Light dependent resistor int LDR1 = analogread(ldrpin); int LDR2 = analogread(ldrpin); int LDR3 = analogread(ldrpin); int LDR4 = analogread(ldrpin);
int LDR5 = analogread(ldrpin); // average the values int LDR = (LDR1 + LDR2 + LDR3 + LDR4 + LDR5)/5; Serial.print("LDR: "); Serial.println(LDR); // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. myfile = SD.open("log.txt", FILE_WRITE); // if the file opened okay, write to it: if (myfile) { Serial.println("writing to log"); myfile.print(now.year(), DEC); myfile.print('/'); myfile.print(now.month(), DEC); myfile.print('/'); myfile.print(now.day(), DEC); myfile.print(','); myfile.print(now.hour(), DEC); myfile.print(':'); myfile.print(now.minute(), DEC); myfile.print(':'); myfile.print(now.second(), DEC); myfile.print(previousmillis); myfile.print(temperature); myfile.print(humidity); myfile.println(ldr); // close the file: myfile.close(); Serial.println("Write complete."); Serial.println(""); else { // if the file didn't open, print an error: Serial.println("error opening temperaturelog.txt");