Created
July 2, 2014 00:47
-
-
Save dwblair/dba3852885cba7da1e1d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <JeeLib.h> | |
ISR(WDT_vect) { Sleepy::watchdogEvent(); } | |
#define outFileName "humid1.csv" | |
#define BATTERYPIN A3 | |
// temp and humidity | |
#include "DHT.h" | |
#define DHTPIN A1 // what pin we're connected to | |
#define DHTTYPE DHT22 // DHT 22 (AM2302) | |
#include <Wire.h> | |
DHT dht(DHTPIN, DHTTYPE); | |
int led = A2; | |
#include <SPI.h> | |
#include <SD.h> | |
#include <RTClib.h> | |
#include <RTC_DS3231.h> | |
RTC_DS3231 RTC; | |
// On the Ethernet Shield, CS is pin 4. Note that even if it's not | |
// used as the CS pin, the hardware CS pin (10 on most Arduino boards, | |
// 53 on the Mega) must be left as an output or the SD library | |
// functions will not work. | |
const int chipSelect = 7; | |
int SDpower = 5; | |
int sensorPower = 4; | |
File dataFile; | |
void setup(void) { | |
pinMode(led, OUTPUT); | |
dht.begin(); | |
pinMode(SDpower,OUTPUT); | |
pinMode(sensorPower,OUTPUT); | |
digitalWrite(SDpower,LOW); | |
digitalWrite(sensorPower,LOW); | |
//initialize the SD card | |
Serial.print("Initializing SD card..."); | |
// make sure that the default chip select pin is set to | |
// output, even if you don't use it: | |
pinMode(SS, OUTPUT); | |
// see if the card is present and can be initialized: | |
if (!SD.begin(chipSelect)) { | |
Serial.println("Card failed, or not present"); | |
// don't do anything more: | |
while (1) ; | |
} | |
Serial.println("card initialized."); | |
//shut down the SD and the sensor -- HIGH is off | |
//digitalWrite(SDpower,HIGH); | |
// digitalWrite(sensorPower,HIGH); | |
pinMode(led, OUTPUT); | |
// for i2c for RTC | |
Wire.begin(); | |
RTC.begin(); | |
Serial.begin(9600); | |
//analogReference(EXTERNAL); | |
// check on the 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(__DATE__, __TIME__)); | |
} | |
DateTime now = RTC.now(); | |
DateTime compiled = DateTime(__DATE__, __TIME__); | |
if (now.unixtime() < compiled.unixtime()) { | |
//Serial.println("RTC is older than compile time! Updating"); | |
RTC.adjust(DateTime(__DATE__, __TIME__)); | |
} | |
} | |
void loop(void) { | |
digitalWrite(led, LOW); | |
//delay(200); | |
//Sleepy::loseSomeTime(10000); //-- will interfere with serial, so don't use when debugging | |
delay (2000); // use when debugging -- loseSomeTime does goofy things w/ serial | |
//wake up the SD card and the sensor | |
//digitalWrite(SDpower,LOW); | |
//digitalWrite(sensorPower,LOW); | |
//dht22 | |
// 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(); | |
// Read temperature as Celsius | |
float t = dht.readTemperature(); | |
// Read temperature as Fahrenheit | |
float f = dht.readTemperature(true); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
// Compute heat index | |
// Must send in temp in Fahrenheit! | |
float hi = dht.computeHeatIndex(f, h); | |
Serial.print("Humidity: "); | |
Serial.print(h); | |
Serial.print(" %\t"); | |
Serial.print("Temperature: "); | |
Serial.print(t); | |
Serial.print(" *C "); | |
Serial.print(f); | |
Serial.print(" *F\t"); | |
Serial.print("Heat index: "); | |
Serial.print(hi); | |
Serial.println(" *F"); | |
DateTime now = RTC.now(); | |
//get the onboard temp from the RTC | |
//RTC.forceTempConv(true); //DS3231 does this every 64 seconds, we are simply testing the function here | |
float RTCTemp = RTC.getTempAsFloat(); | |
long unixNow = now.unixtime(); | |
//bSerial.println(now.unixtime()); | |
digitalWrite(led, HIGH); | |
delay(50); | |
// delay(1000); | |
uint8_t i; | |
// get the battery level | |
int batteryLevel = analogRead(BATTERYPIN); | |
// write to sd card | |
// Open up the file we're going to log to! | |
dataFile = SD.open(outFileName, FILE_WRITE); | |
if (! dataFile) { | |
Serial.println("error opening datalog.txt"); | |
// Wait forever since we cant write data | |
while (1) ; | |
} | |
int onboardTempInt = (int) (RTCTemp*100); | |
// make a string for assembling the data to log: | |
String dataString = ""; | |
dataString += String(unixNow); | |
dataString +=","; | |
dataString += onboardTempInt; | |
//dataString += String(unixNow); | |
dataString+=","; | |
//Serial.print(h); | |
dataString += String((int) (h*100)); | |
dataString+=","; | |
//Serial.print(t); | |
dataString += String((int) (t*100)); | |
dataString+=","; | |
dataString += String(batteryLevel); | |
//no final comma needed | |
//write the string to the card | |
dataFile.println(dataString); | |
// print to the serial port too: | |
Serial.println(dataString); | |
// The following line will 'save' the file to the SD card after every | |
// line of data - this will use more power and slow down how much data | |
// you can read but it's safer! | |
// If you want to speed up the system, remove the call to flush() and it | |
// will save the file only every 512 bytes - every time a sector on the | |
// SD card is filled with data. | |
dataFile.flush(); | |
dataFile.close(); //<--- may be unnecessary | |
//shut down the SD and the sensor | |
//digitalWrite(SDpower,HIGH); | |
//digitalWrite(sensorPower,HIGH); | |
//delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment