Created
March 30, 2015 18:08
-
-
Save lukeledet/712d320d48229a830724 to your computer and use it in GitHub Desktop.
Humidity logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DHT.h" | |
#include "SD.h" | |
#include "Wire.h" | |
#define DHTTYPE DHT22 | |
#define DHTPIN 4 | |
DHT dht(DHTPIN, DHTTYPE); | |
File logFile; | |
void setup() { | |
Serial.begin(9600); | |
Serial.print("Initializing SD card..."); | |
pinMode(10, OUTPUT); | |
if (!SD.begin(10)) { | |
Serial.println("initialization failed!"); | |
return; | |
} | |
Serial.println("initialization done."); | |
dht.begin(); | |
// Setup RTC | |
Wire.begin(); | |
Wire.beginTransmission(0x68); // address DS3231 | |
Wire.write(0x0E); // select register | |
Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC | |
Wire.endTransmission(); | |
} | |
void loop() { | |
delay(600000); | |
// Reading temperature or humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(true); | |
if (isnan(h) || isnan(t)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
logFile = SD.open("boat.txt", FILE_WRITE); | |
if (logFile) { | |
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address | |
Wire.write((byte)0); // start at register 0 | |
Wire.endTransmission(); | |
Wire.requestFrom(0x68, 7); // request 7 bytes (seconds, minutes, hours, day of week, day of month, month and year) | |
int second = bcdToDec(Wire.read()); // get seconds | |
int minute = bcdToDec(Wire.read()); // get minutes | |
int hour = bcdToDec(Wire.read()); // get hours | |
int dow = bcdToDec(Wire.read()); // get day of week | |
int dom = bcdToDec(Wire.read()); // get day of month | |
int month = bcdToDec(Wire.read()); // get month | |
int year = bcdToDec(Wire.read()); // get year | |
Serial.print(year + 2000); Serial.print("-"); Serial.print(month); Serial.print("-"); Serial.print(dom); Serial.print(" "); | |
Serial.print(hour); Serial.print(":"); Serial.print(minute); Serial.print(":"); Serial.print(second); Serial.print(","); | |
Serial.print(h); Serial.print(","); Serial.println(t); | |
logFile.print(year + 2000); logFile.print("-"); logFile.print(month); logFile.print("-"); logFile.print(dom); logFile.print(" "); | |
logFile.print(hour); logFile.print(":"); logFile.print(minute); logFile.print(":"); logFile.print(second); logFile.print(","); | |
logFile.print(h); logFile.print(","); logFile.println(t); | |
} | |
logFile.close(); | |
} | |
// Convert normal decimal numbers to binary coded decimal | |
byte decToBcd(byte val) { | |
return( (val/10*16) + (val%10) ); | |
} | |
// Convert binary coded decimal to normal decimal numbers | |
byte bcdToDec(byte val) { | |
return( (val/16*10) + (val%16) ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got this far with a similar code:
#include "dht.h"
#include "SD.h"
#include "Wire.h"
dht DHT;
#define DHTTYPE DHT22
#define DHTPIN 3
DHT dht(DHTPIN, DHTTYPE);
'DHT' does not name a type, any suggestions?