The code with formatting still in tact this time....
Code:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <EthernetUdp.h>
// Pins in use
// 20 21 RTC - SDA - SLC
// 3 2 5 6 7 8 9 LCD
// 22 ONE WIRE BUS
// 10 11 12 13 53 ETHERSHIELD
// 4 SD
//...........................................................
// defines
//...........................................................
// LCD
//#define BUTTON_ADC_PIN A0 // A0 is the button ADC input
#define LCD_BACKLIGHT_PIN 3 // D3 controls LCD backlight
//some example macros with friendly labels for LCD backlight/pin control, tested and can be swapped into the example code as you like
//#define LCD_BACKLIGHT_OFF() digitalWrite( LCD_BACKLIGHT_PIN, LOW )
//#define LCD_BACKLIGHT_ON() digitalWrite( LCD_BACKLIGHT_PIN, HIGH )
// do we print to serial
#define ECHO_TO_SERIAL false
// Data wire is plugged into pin 22 on the Arduino Mega
#define ONE_WIRE_BUS 22
// RTC
RTC_DS1307 RTC;
// LCD
LiquidCrystal lcd( 8, 9, 2, 5, 6, 7 ); //Pins for the freetronics 16x2 LCD shield. LCD: ( RS, E, LCD-D4, LCD-D5, LCD-D6, LCD-D7 )
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
// Working Probes
DeviceAddress Probe01 = { 0x28, 0xD2, 0xCD, 0xE6, 0x03, 0x00, 0x00, 0xDE };
DeviceAddress Probe02 = { 0x28, 0xA8, 0xCD, 0xE6, 0x03, 0x00, 0x00, 0x89 };
DeviceAddress Probe03 = { 0x28, 0xB2, 0xBB, 0xE6, 0x03, 0x00, 0x00, 0xEC };
DeviceAddress Probe04 = { 0x28, 0x99, 0xD0, 0xE6, 0x03, 0x00, 0x00, 0xC3 };
DeviceAddress Probe05 = { 0x28, 0x9D, 0xCB, 0xE6, 0x03, 0x00, 0x00, 0x13 };
DeviceAddress Probe06 = { 0x28, 0x1F, 0xD2, 0xE6, 0x03, 0x00, 0x00, 0x18 };
// test Probes
//DeviceAddress Probe01 = { 0x28, 0x82, 0xBF, 0xE6, 0x03, 0x00, 0x00, 0x1E };
//DeviceAddress Probe02 = { 0x28, 0xB1, 0xCB, 0xE6, 0x03, 0x00, 0x00, 0xD8 };
//DeviceAddress Probe03 = { 0x28, 0xF3, 0xC9, 0xE6, 0x03, 0x00, 0x00, 0x40 };
//DeviceAddress Probe04 = { 0x28, 0xEB, 0xD5, 0xE6, 0x03, 0x00, 0x00, 0xE7 };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,50,40);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
//...........................................................
// SD Card
//...........................................................
// for the ethernet shield, we use digital pin 2 for the SD cs line
const int chipSelect = 4;
//...........................................................
// storage arrays
// with the data in arrays of 10 elements we can recall the last 10 readings
//...........................................................
String ADateTime[10] = "17/03/2013 23:59:00";
double AProbe01[10];
double AProbe02[10];
double AProbe03[10];
double AProbe04[10];
double AProbe05[10];
double AProbe06[10];
int AIndex = -1;
int IndexCount = -1;
//...........................................................
// const
//...........................................................
//...........................................................
// general variables
//...........................................................
// log file
File logFile;
char CurrentLogName[] = "00000000.txt";
char LastLogName[] = "00000000.txt";
String DataTimeStamp;
unsigned long NextRun = 0;
unsigned long NextRunLCD = 0;
// interval for logging
int LogInterval = 900; // 15 minutes
int LCDInterval = 5; // 5 sec
// Probe descriptions
char Probe01N[10] = "Tank";
char Probe02N[10] = "Sump";
char Probe03N[10] = "GBed";
char Probe04N[10] = "OutSide";
char Probe05N[10] = "Test01";
char Probe06N[10] = "Test02";
// rotating lcd display
int NextStats = 0;
//...........................................................
// setup
//...........................................................
void setup() {
// start serial port
#if ECHO_TO_SERIAL
Serial.begin(9600);
#endif
digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //backlight control pin D3 is high (on)
pinMode( LCD_BACKLIGHT_PIN, OUTPUT ); //D3 is an output
//set up the LCD number of columns and rows:
lcd.begin( 16, 2 );
lcd.setCursor( 0, 0 ); //top left
lcd.print( "LCD READY.." );
lcd.setCursor( 0, 1 ); //top left
lcd.print( " " );
Wire.begin();
RTC.begin();
RTC.sqw(1); //0 Led off - 1 Freq 1Hz - 2 Freq 4096kHz - 3 Freq 8192kHz - 4 Freq 32768kHz
if (! RTC.isrunning()) {
#if ECHO_TO_SERIAL
Serial.println("RTC is NOT running!");
#endif
logFile.println("// RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
// disable w5100 SPI while starting SD
pinMode(53,OUTPUT);
digitalWrite(53,HIGH);
#if ECHO_TO_SERIAL
Serial.print("Starting SD...");
#endif
if(SD.begin(chipSelect) == 0) {
#if ECHO_TO_SERIAL
Serial.println("SD failed");
#endif
}
else
{
#if ECHO_TO_SERIAL
Serial.println(F("ok"));
#endif
}
#if ECHO_TO_SERIAL
Serial.print(F("Starting w5100..."));
#endif
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
lcd.setCursor( 0, 0 ); //top left
lcd.print( "Starting w5100..." );
#if ECHO_TO_SERIAL
Serial.println(Ethernet.localIP());
Serial.println("Ethernet Ready...");
#endif
lcd.setCursor( 0, 0 ); //top left
lcd.print( "Ethernet Ready..." );
if (! startlogfile()) {
#if ECHO_TO_SERIAL
Serial.println("failed to write to card!");
#endif
}
sensors.requestTemperatures();
delay(12000);
sensors.requestTemperatures();
displaylog(false);
}
//...........................................................
// startlogfile
//...........................................................
// this where we set the log files up
boolean startlogfile()
{
generatedatetimecodes();
for(int x = 0; x < 11; x++) {
LastLogName[x] = CurrentLogName[x];
}
logFile = SD.open(CurrentLogName, FILE_WRITE);
// if the file opened okay, write to it:
if (logFile) {
// the file existed so write a new header then close the it
logFile.print("DateTime,");
logFile.print(Probe01N);
logFile.print(",");
logFile.print(Probe02N);
logFile.print(",");
logFile.print(Probe03N);
logFile.print(",");
logFile.print(Probe04N);
logFile.print(",");
logFile.print(Probe05N);
logFile.print(",");
logFile.println(Probe06N);
logFile.flush();
logFile.close();
return true;
}
else {
return false;
}
}
//...........................................................
// generatedatetimecodes
//...........................................................
// we format the two types of time codes required
void generatedatetimecodes()
{
DateTime now = RTC.now();
int temp;
String SYear, SMonth, SDay, SHour, SMin, SSec;
SYear = (String)now.year();
temp = now.month();
if (temp < 10) {
SMonth = ("0" + (String)temp);
}
else {
SMonth = (String)temp;
}
temp = now.day();
if (temp < 10) {
SDay = ("0" + (String)temp);
}
else {
SDay = (String)temp;
}
temp = now.hour();
if (temp < 10) {
SHour = ("0" + (String)temp);
}
else {
SHour = (String)temp;
}
temp = now.minute();
if (temp < 10) {
SMin = ("0" + (String)temp);
}
else {
SMin = (String)temp;
}
temp = now.second();
if (temp < 10) {
SSec = ("0" + (String)temp);
}
else {
SSec = (String)temp;
}
// log file name
// Year
for(int x = 0; x < 4; x++) {
CurrentLogName[x] = SYear[x];
}
// Month
for(int x = 0; x < 2; x++) {
CurrentLogName[x + 4] = SMonth[x];
}
// Day
for(int x = 0; x < 2; x++) {
CurrentLogName[x + 6] = SDay[x];
}
// log date time stamp
DataTimeStamp = SDay;
DataTimeStamp += "/";
DataTimeStamp += SMonth;
DataTimeStamp += "/";
DataTimeStamp += SYear;
DataTimeStamp += " ";
DataTimeStamp += SHour;
DataTimeStamp += ":";
DataTimeStamp += SMin;
DataTimeStamp += ":";
DataTimeStamp += SSec;
}
//...........................................................
// printTemperature
//...........................................................
// read our Dallas Temperature probes DS18B20
double printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
return 0;
}
else {
return tempC;
}
}
//...........................................................
// displaylog
//...........................................................
void displaylog(boolean LCDOnly)
{
sensors.requestTemperatures();
generatedatetimecodes();
if (! LCDOnly) {
// store the data in arrays
// inc the index counter
AIndex ++;
if (AIndex >= 10) AIndex = 0;
// note how many elements have been filled since start up
if (AIndex > IndexCount) IndexCount = AIndex;
ADateTime[AIndex] = DataTimeStamp;
AProbe01[AIndex] = printTemperature(Probe01);
AProbe02[AIndex] = printTemperature(Probe02);
AProbe03[AIndex] = printTemperature(Probe03);
AProbe04[AIndex] = printTemperature(Probe04);
AProbe05[AIndex] = printTemperature(Probe05);
AProbe06[AIndex] = printTemperature(Probe06);
// if the new log file name does not match the last one
// ceate a new one with headers.
if (strcmp(LastLogName, CurrentLogName)) {
startlogfile();
}
// log the data to file
logFile = SD.open(CurrentLogName, FILE_WRITE);
// if the file opened okay, write to it:
if (logFile) {
// the file existed so write a new header then close it
logFile.print(ADateTime[AIndex]);
logFile.print(",");
logFile.print(ADateTime[AIndex]);
logFile.print(",");
logFile.print(AProbe02[AIndex]);
logFile.print(",");
logFile.print(AProbe03[AIndex]);
logFile.print(",");
logFile.print(AProbe04[AIndex]);
logFile.print(",");
logFile.print(AProbe05[AIndex]);
logFile.print(",");
logFile.println(AProbe06[AIndex]);
logFile.flush();
logFile.close();
}
#if ECHO_TO_SERIAL
Serial.print(AIndex);
Serial.print(" ");
Serial.print(ADateTime[AIndex]);
Serial.print(",");
Serial.print(AProbe01[AIndex]);
Serial.print(",");
Serial.print(AProbe02[AIndex]);
Serial.print(",");
Serial.print(AProbe03[AIndex]);
Serial.print(",");
Serial.print(AProbe04[AIndex]);
Serial.print(",");
Serial.print(AProbe05[AIndex]);
Serial.print(",");
Serial.println(AProbe06[AIndex]);
#endif
}
}
//...........................................................
// main
//...........................................................
void loop() {
int Index = 0;
// data logging
// we check if we are at our future time yet
// if so we display the logs and set a new future time
// get the current time
DateTime now = RTC.now();
unsigned long CurrentTime = now.unixtime();
if (CurrentTime >= NextRun) {
// set the next run time
NextRun = (now.unixtime() + LogInterval);
displaylog(false);
}
// lcd display to show temps at this time
// we check if we are at our future time yet
// if so we display probe results in sequence and set a new future time
if (CurrentTime >= NextRunLCD) {
// set the next run time
NextRunLCD = (now.unixtime() + LCDInterval);
// get the current readings
displaylog(true);
lcd.setCursor( 0, 0 ); //top left
lcd.print(DataTimeStamp);
switch (NextStats) {
case 1:
lcd.setCursor( 0, 1 ); //bottom left
lcd.print(" Tank : ");
lcd.print(printTemperature(Probe01));
break;
case 2:
lcd.setCursor( 0, 1 ); //bottom left
lcd.print(" Sump : ");
lcd.print(printTemperature(Probe02));
break;
case 3:
lcd.setCursor( 0, 1 ); //bottom left
lcd.print(" G-Bed : ");
lcd.print(printTemperature(Probe03));
break;
case 4:
lcd.setCursor( 0, 1 ); //bottom left
lcd.print("Outside : ");
lcd.print(printTemperature(Probe04));
break;
case 5:
lcd.setCursor( 0, 1 ); //bottom left
lcd.print("Test 01 : ");
lcd.print(printTemperature(Probe05));
break;
case 6:
lcd.setCursor( 0, 1 ); //bottom left
lcd.print("Test 02 : ");
lcd.print(printTemperature(Probe06));
break;
}
// index our NextStats couter and if we are over 6 then reset to 0
NextStats ++;
if (NextStats > 6) NextStats = 0;
}
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
#if ECHO_TO_SERIAL
Serial.println("new client");
#endif
// an http request ends with a blank line
char tmpstr[client.available()];
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
#if ECHO_TO_SERIAL
Serial.write(c);
#endif
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.print("Record Count:");
client.println(IndexCount);
for (int x = 0; Index < IndexCount; x ++) {
Index ++;
client.print("Record No:");
client.println(Index);
client.print("DateTime:");
client.println(ADateTime[Index]);
client.print(Probe01N);
client.print(":");
client.println(AProbe01[Index]);
client.print(Probe02N);
client.print(":");
client.println(AProbe02[Index]);
client.print(Probe03N);
client.print(":");
client.println(AProbe03[Index]);
client.print(Probe04N);
client.print(":");
client.println(AProbe04[Index]);
client.print(Probe05N);
client.print(":");
client.println(AProbe05[Index]);
client.print(Probe06N);
client.print(":");
client.println(AProbe06[Index]);
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
#if ECHO_TO_SERIAL
Serial.println("client disonnected");
#endif
}
}