Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created July 25, 2014 03:26
Show Gist options
  • Save dwblair/a43ea3ba179fe601c00e to your computer and use it in GitHub Desktop.
Save dwblair/a43ea3ba179fe601c00e to your computer and use it in GitHub Desktop.
#include <JeeLib.h>
#include "DHT.h"
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
#include <RTC_DS3231.h>
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
int debug=0;
int led = 6;
#define LOG_INTERVAL 5000 // millsec between readings
#define BATTERYPIN A3
// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 8350
int samples[NUMSAMPLES];
//#define DHTPIN A1 // what pin we're connected to
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//DHT dht(DHTPIN, DHTTYPE);
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;
char filename[] = "LOGGER00.csv";
File dataFile;
String fileHeader = "DATETIME,RTC_TEMP_C,TEMP_C,HUMIDITY_PCT,BATTERY_LEVEL";
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.println();
Serial.print("Initializing SD card...");
}
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.");
}
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
if (debug==1) {
Serial.print("Writing to file: " );
Serial.println(filename);
}
dataFile = SD.open(filename, FILE_WRITE);
dataFile.println(fileHeader);
dataFile.close();
break; // leave the loop!
}
}
//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__));
}
Serial.println();
Serial.println(fileHeader);
//show that we're working
for (int j=0;j<5;j++) {
digitalWrite(led, LOW);
delay(1000);
digitalWrite(led, HIGH);
delay(1000);
}
digitalWrite(led, LOW);
delay(1000);
}
void loop(void) {
//delay(200);
//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 = 2.;
// Read temperature as Celsius
float t = 3.;
// Read temperature as Fahrenheit
float f = 4.;
// 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 = 1.0;
*/
DateTime now = RTC.now();
// long unixNow = now.unixtime();
// delay(1000);
// thermistor
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
float temp10=steinhart*10;
int temp10int=(int) temp10;
if (debug==1){
Serial.print("Average analog reading ");
Serial.println(average);
Serial.print("Thermistor resistance ");
Serial.println(average);
Serial.print("Temperature ");
Serial.print(steinhart);
Serial.println(" *C");
}
// Get the battery level
int batteryLevel = analogRead(BATTERYPIN);
// 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();
// make a string for assembling the data to log:
String dataString = "";
// dataString += String(unixNow);
dataString += now.year();
dataString += "-";
dataString += padInt(now.month(), 2);
dataString += "-";
dataString += padInt(now.day(), 2);
dataString += " ";
dataString += padInt(now.hour(), 2);
dataString += ":";
dataString += padInt(now.minute(), 2);
dataString += ":";
dataString += padInt(now.second(), 2);
dataString += ",";
dataString += int2string((int) (rtcTemp*100));
dataString += ",";
dataString += int2string((int) (steinhart*100));
dataString += ",";
dataString += int2string(batteryLevel);
// Open up the file we're going to log to!
// dataFile = SD.open(outFileName, FILE_WRITE);
dataFile = SD.open(filename, FILE_WRITE);
if (!dataFile) {
if (debug==1){
Serial.print("Error opening file:");
Serial.println(filename);
}
// Wait forever since we cant write data
while (1) ;
}
digitalWrite(led, HIGH);
delay(50);
// Write the string to the card
dataFile.println(dataString);
dataFile.close();
digitalWrite(led,LOW);
if (debug==1) {
Serial.println(dataString);
}
//sleep
if (debug==0) {
Sleepy::loseSomeTime(LOG_INTERVAL); //-- will interfere with serial, so don't use when debugging
} else {
delay (LOG_INTERVAL); // use when debugging -- loseSomeTime does goofy things w/ serial
}
// 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);
}
String padInt(int x, int pad) {
String strInt = String(x);
String str = "";
if (strInt.length() >= pad) {
return strInt;
}
for (int i=0; i < (pad-strInt.length()); i++) {
str += "0";
}
str += strInt;
return str;
}
String int2string(int x) {
// formats an integer as a string assuming x is in 1/100ths
String str = String(x);
int strLen = str.length();
if (strLen <= 2) {
str = "0." + str;
} else if (strLen <= 3) {
str = str.substring(0, 1) + "." + str.substring(1);
} else if (strLen <= 4) {
str = str.substring(0, 2) + "." + str.substring(2);
} else {
str = "-9999";
}
return str;
}
@dwblair
Copy link
Author

dwblair commented Jul 25, 2014

gives indication that it's working, sleeps after recording

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