Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created July 2, 2014 02:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwblair/93f9ad4133f2669f078f to your computer and use it in GitHub Desktop.
Save dwblair/93f9ad4133f2669f078f to your computer and use it in GitHub Desktop.
#include <JeeLib.h>
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
#define outFileName "poop6.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>
int debug=0;
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) {
if (debug==1){
Serial.begin(9600);
}
pinMode(led, OUTPUT);
dht.begin();
pinMode(SDpower,OUTPUT);
pinMode(sensorPower,OUTPUT);
digitalWrite(SDpower,LOW);
digitalWrite(sensorPower,LOW);
//initialize the SD card
if (debug==1){
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)) {
if (debug==1){
Serial.println("Card failed, or not present");
}
// don't do anything more:
while (1) ;
}
if (debug==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();
//analogReference(EXTERNAL);
// check on the RTC
if (! RTC.isrunning()) {
if (debug==1){
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(5000); //-- will interfere with serial, so don't use when debugging
//delay (5000); // 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)) {
if (debug==1){
Serial.println("Failed to read from DHT sensor!");
}
return;
}
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);
if (debug==1){
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
// Open up the file we're going to log to!
dataFile = SD.open(outFileName, FILE_WRITE);
if (! dataFile) {
if (debug==1){
Serial.println("error opening datalog.txt");
}
// Wait forever since we cant write data
while (1) ;
}
//write the string to the card
dataFile.println(dataString);
dataFile.close();
if (debug==1){
// 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);
}
@dwblair
Copy link
Author

dwblair commented Jul 7, 2014

uses the https://github.com/mizraith/RTClib library, the Jeenode library, and the adafruit DHT22 library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment