Here is the code
// libraries
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20 sensors on I/O pin 5
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress AirThermometer = { 0x28, 0x2C, 0x41, 0x3A, 0x03, 0x00, 0x00, 0x46 };
DeviceAddress MichaelThermometer = { 0x28, 0xAD, 0x7F, 0x3A, 0x03, 0x00, 0x00, 0xBD };
DeviceAddress TitoThermometer = {0x28, 0x29, 0x7C, 0x3A, 0x03, 0x00, 0x00, 0xC5 };
DeviceAddress MarlonThermometer = { 0x28, 0x1C, 0x32, 0x3A, 0x03, 0x00, 0x00, 0x88 };
// definitions
#define LOG_INTERVAL 300000 // mills between entries (reduce to take more/faster data) 5 minutes
#define SYNC_INTERVAL 300000 // mills between calls to flush() - to write data to the card
#define ECHO_TO_SERIAL 1 // echo data to serial port-- 1 to see 0 when in service
#define redLEDpin 2
#define greenLEDpin 3
//#define tempPin A1 // analog 1
#define BANDGAPREF 14 // special indicator that we want to measure the bandgap
#define bandgap_voltage 1.1 // this is not super guaranteed but its not -too- off
// other stuff??
RTC_DS1307 RTC; // define the Real Time Clock object
uint32_t syncTime = 0; // time of last sync()
const int chipSelect = 10;// for the data logging shield, we use digital pin 10 for the SD cs line
File logfile; // the logging file
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
digitalWrite(redLEDpin, HIGH); // red LED indicates error
while(1);// lock up if error
}
void setup(void)
{
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(AirThermometer, 10);
sensors.setResolution(MichaelThermometer, 10);
sensors.setResolution(TitoThermometer, 10);
sensors.setResolution(MarlonThermometer, 10);
// use debugging LEDs
pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
digitalWrite(greenLEDpin, HIGH);
// initialize the SD card
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
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
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("millis,stamp,datetime,Pond temp,Air temp,vcc");
#if ECHO_TO_SERIAL
Serial.println("millis,stamp,datetime,Pond temp,Air temp,vcc");
#endif //ECHO_TO_SERIAL
float voltage = 0; // setup some variables float for 2 decimal places
float sensor = 0;
float TempC = 0;
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Error getting temperature");
} else {
logfile.print(tempC,2);
logfile.print(',');
#if ECHO_TO_SERIAL
Serial.print(tempC,2);
Serial.print(',');}
#endif
}
void loop(void)
{
DateTime now;
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
digitalWrite(greenLEDpin, HIGH); // have green light on just to see its working ok to here.
uint32_t m = millis(); // log milliseconds since starting
logfile.print(m); // milliseconds since start
logfile.print(", ");
#if ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif
// fetch the time
now = RTC.now();
// log time
logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(", ");
logfile.print('"');
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print('"');
logfile.print(',');
#if ECHO_TO_SERIAL
Serial.print(now.unixtime()); // seconds since 1/1/1970
Serial.print(", ");
Serial.print('"');
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print('"');
Serial.print(',');
#endif //ECHO_TO_SERIAL
{
delay(2000);
sensors.requestTemperatures();
printTemperature(AirThermometer);
delay(200);
printTemperature(MichaelThermometer);
delay(200);
printTemperature(TitoThermometer);
delay(200);
printTemperature(MarlonThermometer);
}
// Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
analogRead(BANDGAPREF);
delay(10);
int refReading = analogRead(BANDGAPREF);
float supplyvoltage = (bandgap_voltage * 1024) / refReading;
logfile.print(supplyvoltage);
#if ECHO_TO_SERIAL
Serial.print(supplyvoltage);
#endif
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL
digitalWrite(greenLEDpin, LOW);
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
// blink LED to show we are syncing data to the card & updating FAT!
digitalWrite(redLEDpin, HIGH);
logfile.flush();
digitalWrite(redLEDpin, LOW);
}