The sketch as it is currently running. it is just over 34K in size at present.
#include <OneWire.h> #include <DallasTemperature.h> #include <Wire.h> #include "RTClib.h" #include <SD.h> #include <SPI.h> #include <Ethernet.h> #include <LiquidCrystal.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 10 on the Arduino #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 };
// 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);
// (port 80 is default for HTTP): EthernetServer server(80);
//........................................................... // general variables //........................................................... // SD Card // for the ethernet shield, we use digital pin 2 for the SD cs line const int chipSelect = 4; // interval for logging const int LogInterval = 900; // 15 minutes const int LCDInterval = 5; // 5 sec
//........................................................... // general variables //........................................................... // log file File logFile; char CurrentLogName[] = "00000000.txt"; char LastLogName[] = "00000000.txt"; String DataTimeStamp; unsigned long NextRun = 0; unsigned long NextRunLCD = 0;
// probe descriptions char Probe01N[10] = "Tank"; char Probe02N[10] = "Sump"; char Probe03N[10] = "GBed"; char Probe04N[10] = "OutSide";
// probe results double ProbeR01, ProbeR02, ProbeR03, ProbeR04;
// rotating lcd display int nextprobe = 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
lcd.setCursor( 0, 0 ); //top left lcd.print( "Starting w5100..." );
// start the ethernet card Ethernet.begin(mac, ip); server.begin();
#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 } }
//........................................................... // 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.println(Probe04N); 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() { sensors.requestTemperatures();
ProbeR01 = printTemperature(Probe01); ProbeR02 = printTemperature(Probe02); ProbeR03 = printTemperature(Probe03); ProbeR04 = printTemperature(Probe04);
generatedatetimecodes();
// if the new log file name does not match the last one // ceate a new one with headers. if (strcmp(LastLogName, CurrentLogName)) { startlogfile(); }
#if ECHO_TO_SERIAL Serial.print(DataTimeStamp); Serial.print(","); Serial.print(ProbeR01); Serial.print(","); Serial.print(ProbeR02); Serial.print(","); Serial.print(ProbeR03); Serial.print(","); Serial.println(ProbeR04); #endif
// 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(DataTimeStamp); logFile.print(","); logFile.print(ProbeR01); logFile.print(","); logFile.print(ProbeR02); logFile.print(","); logFile.print(ProbeR03); logFile.print(","); logFile.println(ProbeR04); logFile.flush(); logFile.close(); } }
//........................................................... // main //........................................................... void loop() { // 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(); } // lcd display to show temps // 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); lcd.setCursor( 0, 0 ); //top left lcd.print(DataTimeStamp);
switch (nextprobe) { case 1: lcd.setCursor( 0, 1 ); //bottom left lcd.print(" Tank : "); lcd.print(ProbeR01); break; case 2: lcd.setCursor( 0, 1 ); //bottom left lcd.print(" Sump : "); lcd.print(ProbeR02); break; case 3: lcd.setCursor( 0, 1 ); //bottom left lcd.print("M G-Bed : "); lcd.print(ProbeR03); break; case 4: lcd.setCursor( 0, 1 ); //bottom left lcd.print("Outside : "); lcd.print(ProbeR04); break; } // index our nextprobe couter and if we are over 3 the reset to 0 nextprobe ++; if (nextprobe > 4) nextprobe = 0; } }
Last edited by Johny5 on Mar 16th, '13, 22:22, edited 1 time in total.
|