Created
June 9, 2014 12:59
-
-
Save dwblair/35ccbb4e76d75ea9aeeb to your computer and use it in GitHub Desktop.
Riffle-ito REVC: measuring RTC temp and thermistor temp, and recording values (with RTC unix timestamp) to SD card (and flashing LED); deep sleep for 5 seconds in between measurements
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(); } | |
// which analog pin to connect | |
#define THERMISTORPIN A2 | |
// 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 10000 | |
#define outFileName "testDWB3.csv" | |
#include <Wire.h> | |
int samples[NUMSAMPLES]; | |
int led = 6; | |
#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(SDpower,OUTPUT); | |
pinMode(sensorPower,OUTPUT); | |
digitalWrite(SDpower,LOW); | |
digitalWrite(sensorPower,LOW); | |
pinMode(led, OUTPUT); | |
// for i2c for RTC | |
Wire.begin(); | |
RTC.begin(); | |
Serial.begin(9600); | |
//analogReference(EXTERNAL); | |
//eeprom | |
int addr = 0; | |
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."); | |
// 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(5000); | |
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(1000); | |
// delay(1000); | |
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; | |
Serial.print("Average analog reading "); | |
Serial.println(average); | |
// convert the value to resistance | |
average = 1023 / average - 1; | |
average = SERIESRESISTOR / average; | |
Serial.print("Thermistor resistance "); | |
Serial.println(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 | |
Serial.print("Temperature "); | |
Serial.print(steinhart); | |
Serial.println(" *C"); | |
// get RTC vals | |
/* | |
// send request to receive data starting at register 0 | |
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address | |
Wire.write((byte)0); // start at register 0 | |
Wire.endTransmission(); | |
Wire.requestFrom(0x68, 3); // request three bytes (seconds, minutes, hours) | |
*/ | |
// 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) ; | |
} | |
float temp10=steinhart*10; | |
int temp10int=(int) temp10; | |
int onboardTempInt = (int) RTCTemp*10; | |
// make a string for assembling the data to log: | |
String dataString = ""; | |
dataString += String(unixNow); | |
dataString +=","; | |
dataString += onboardTempInt; | |
//dataString += String(unixNow); | |
dataString+=","; | |
//Serial.print(steinhart); | |
dataString += String(temp10int); | |
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(); | |
//delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
instead of using the unix timestamp, I think you can add a datetime stamp directly to the file (avoiding conversion after downloading the file). See https://github.com/adafruit/RTClib/blob/master/examples/ds1307/ds1307.pde
Like this but would append to dataString instead of Serial.print()