Created
August 19, 2012 19:12
-
-
Save arduinoboard/3397099 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino Mega 2560 or Mega ADK with a serial number of 74136333932351D021E0
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
/* | |
Date Last Modified: 08/19/2012 | |
By: Jim Mayhugh | |
LCD and Web Temperature monitor utilizes Dallas Semiconductor / Maxim DS18B20 | |
One-Wire Digital Temperature sensors to monitor multiple containers, coolers, etc. | |
The temperatures are available from a various LCD displays and by accessing a small | |
text based web page generated in response to a "GET" command. | |
One-Wire data channel is on digital pin 2, which is pulled up to +5v with a 4K7 resistor. | |
The ethernet interface is the Wiznet W5100 based Ethernet Shield. | |
If you are using multiple Arduino/Ethernet shields, each one must have a unique MAC address and | |
IP Address. | |
*/ | |
/*********** Various defines **********/ | |
// #define SerialDebug // Used to debug code with the serial terminal | |
// #define D16X2 // Used for Adafruit RGB-LCD 16 column by 2 row I2C display | |
#define D20X4 // Used for Adafruit RGB-LCD 20 column by 4 row I2C display | |
#include <Wire.h> | |
#include <SPI.h> | |
#include <Adafruit_MCP23017.h> | |
#include <Adafruit_RGBLCDShield.h> | |
#include <Ethernet.h> | |
#include <EEPROM.h> | |
#include "EEPROMAnything.h" | |
#include <OneWire.h> | |
/********** LCD Stuff ********** | |
The shield uses the I2C SCL (pin 21) and SDA (pin 20) on the Arduino's Mega2560 | |
The Adafruit RGB-LCD shield uses I2C 0x20 by default, but MCP23017 pins 15 (A0), | |
16 (A1), and 17 (A2) can be carefully bent back and soldered to pin 9 (VDD) prior | |
to installing the chip into the board (A DIP SOCKET IS RECOMMENDED !!). | |
You can connect other I2C sensors to the I2C bus and share the I2C bus. | |
*********** LCD Stuff *********/ | |
const uint8_t AdafruitAddr0 = 0x0; // A0, A1, A2 tied to grounded | |
const uint8_t AdafruitAddr1 = 0x1; // A0 tied to VDD, A1 and A2 grounded | |
const uint8_t AdafruitAddr2 = 0x2; // A1 tied to VDD, A0 and A2 grounded | |
const uint8_t AdafruitAddr3 = 0x3; // A0 and A1 tied to VDD, A2 grounded | |
const uint8_t AdafruitAddr4 = 0x4; // A2 tied to VDD, A0 and A1 grounded | |
const uint8_t AdafruitAddr5 = 0x5; // A0 and A2 tied to VDD, A1 grounded | |
const uint8_t AdafruitAddr6 = 0x6; // A1 and A2 tied to VDD, A0 grounded | |
const uint8_t AdafruitAddr7 = 0x7; // A0, A1, and A2 tied to VDD | |
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield(AdafruitAddr0); | |
// These constants make it easy to set the backlight color | |
const int BLOFF = 0x0; | |
const int RED = 0x1; | |
const int YELLOW = 0x3; | |
const int GREEN = 0x2; | |
const int TEAL = 0x6; | |
const int BLUE = 0x4; | |
const int VIOLET = 0x5; | |
const int WHITE = 0x7; | |
bool showRed = FALSE; | |
bool showBlue = FALSE; | |
#if defined D16X2 | |
const int cols = 16; | |
const int rows = 2; | |
const int sensorNameArraySize = cols - 6; | |
const int sensorNameSize = sensorNameArraySize-1; | |
const char lcdToken[] = "D16X2"; | |
char LCDtoken[] = " "; | |
#endif | |
#if defined D20X4 | |
const int cols = 20; | |
const int rows = 4; | |
const int sensorNameArraySize = cols - 6; | |
const int sensorNameSize = sensorNameArraySize-1; | |
const char lcdToken[] = "D20X4"; | |
char LCDtoken[] = " "; | |
#endif | |
const int EEPROMsize = 4096; //Mega2560 | |
const int EEPROMidAddr = 0; | |
const int EEPROMdsAddr = 0x10; | |
const byte EEPROMidVal = 0x55; | |
int eepromSpace; | |
/********** Internet Stuff **********/ | |
byte mac[] = { 0xDE, 0xAD, 0xBA, 0xBE, 0xFE, 0xED }; | |
IPAddress ip(192,168,1,205); // local IP Address | |
// if you don't want to use DNS (and reduce your sketch size) | |
// use the numeric IP instead of the name for the server: | |
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com | |
char cosmServer[] = "api.cosm.com"; // name address for cosm API | |
unsigned long lastConnectionTime; // last time you connected to the server, in milliseconds | |
boolean lastConnected = false; // state of the connection last time through the main loop | |
const unsigned long postingInterval = 5*1000; //delay between updates to Cosm.com | |
const int BUFSIZE = 300; // buffer for internet request | |
char clientline[BUFSIZE]; | |
int index = 0; | |
const int newRow = 5; | |
#define APIKEY "7gkA5tF6I6jOwYRi055QIkIsJrWSAKxMSHJuMzRCUEpiQT0g" // your cosm api key | |
#define FEEDID 71626 // your feed ID | |
#define USERAGENT "Itead Temp Monitor" // user agent is the project name | |
// Initialize the Ethernet server library | |
// with the IP address and port you want to use | |
// (port 80 is default for HTTP): | |
EthernetServer server(80); | |
/********** OneWire Stuff ************/ | |
const int dsDataPin = 2; // One Wire data Bus | |
const int maxSensors = 20; // total number of DS18B20 | |
const byte nbsp = 0x20; // space character | |
const byte zeroChar = 0x30; // zero character | |
const char degChar = 0xdf; // degree character | |
OneWire ds(dsDataPin); // OneWire bus on pin 2 | |
bool addFSpace = FALSE; | |
bool showCelsius = FALSE; | |
int rowNum = 0; | |
typedef struct | |
{ | |
bool sensorActive; // set to "TRUE" if you put a sensor address in the "addr" array. | |
char sensorName[sensorNameArraySize]; | |
byte addr[8]; | |
float deg; | |
int tooCold; // if temperature is below this value, display is blue | |
int tooHot; // if temperature is above this value, display is red | |
} Sensor; | |
#if defined D20X4 | |
Sensor ds18[maxSensors] = | |
{ | |
{FALSE, "Sensor 1", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 2", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 3", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 4", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 5", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 6", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 7", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 8", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 9", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 10", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 11", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 12", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 13", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 14", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 15", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 16", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 17", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 18", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 19", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 20", {0,0,0,0,0,0,0,0}, 0.0, 45, 55} | |
}; | |
#endif | |
#if defined D16X2 | |
Sensor ds18[maxSensors] = | |
{ | |
{FALSE, "Sensor 1", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 2", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 3", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 4", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 5", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 6", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 7", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 8", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 9", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 10", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 11", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 12", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 13", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 14", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 15", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 16", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 17", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 18", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 19", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}, | |
{FALSE, "Sensor 20", {0,0,0,0,0,0,0,0}, 0.0, 45, 55} | |
}; | |
#endif | |
int activeSensors = 0; | |
int cnt = 0; | |
void setup() | |
{ | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
// set up the LCD's number of columns and rows: | |
lcd.begin(cols, rows); | |
#if defined SerialDebug | |
eepromSpace = sizeof(ds18) / sizeof(byte); | |
Serial.print(eepromSpace); | |
Serial.println(F(" bytes in ds18 ")); | |
Serial.print(F("maxSensors = ")); | |
Serial.println(maxSensors); | |
#endif | |
// delay(1000); | |
EEPROM_readAnything(EEPROMidAddr, LCDtoken); | |
lcd.clear(); | |
lcd.home(); | |
lcd.print(F("LCDtoken =")); | |
#if defined D16X2 | |
lcd.setCursor(0, 1); | |
#endif | |
#if defined D20X4 | |
lcd.setCursor(0, 2); | |
#endif | |
lcd.print(LCDtoken); | |
delay(1000); | |
if(strcmp(lcdToken, LCDtoken) != 0) // should only happen once | |
{ | |
lcd.clear(); | |
lcd.home(); | |
lcd.setBacklight(RED); | |
lcd.print(F("Clearing EEPROM")); | |
EEPROM_writeAnything(EEPROMidAddr, lcdToken); | |
for(int i = EEPROMdsAddr; i < eepromSpace; i++) | |
{ | |
EEPROM.write(i, 0); | |
} | |
#if defined SerialDebug | |
Serial.println(F("EEPROM cleared")); | |
#endif | |
EEPROM_writeAnything(EEPROMdsAddr, ds18); | |
lcd.clear(); | |
lcd.home(); | |
lcd.setBacklight(GREEN); | |
lcd.print(F("EEPROM INITIALIZED")); | |
delay(1000); | |
} | |
EEPROM_readAnything(EEPROMdsAddr, ds18); // get sensor structures from EEPROM | |
#if defined SerialDebug | |
Serial.print(F("ds18 initialized - ")); | |
Serial.print(eeRead); | |
Serial.println(F(" bytes read")); | |
#endif | |
for(int cntx=0; cntx < maxSensors; cntx++) | |
{ | |
if(ds18[cntx].sensorActive == TRUE){activeSensors++;} | |
} | |
// start the Ethernet connection and the server: | |
Ethernet.begin(mac, ip); | |
server.begin(); | |
#if defined SerialDebug | |
Serial.print(F("server is at ")); | |
Serial.println(Ethernet.localIP()); | |
#endif | |
lcd.clear(); | |
lcd.home(); | |
lcd.setBacklight(WHITE); | |
lcd.print(F("Server is at ")); | |
#if defined D20X4 | |
lcd.setCursor(0, 2); | |
#endif | |
#if defined D16X2 | |
lcd.setCursor(0, 1); | |
#endif | |
lcd.print(Ethernet.localIP()); | |
delay(2000); | |
lastConnectionTime = millis(); | |
} | |
void getOneWire(void) | |
{ | |
byte i; | |
byte present = 0; | |
byte type_s; | |
byte data[12]; | |
#if defined SerialDebug | |
Serial.print(F("cnt = ")); | |
Serial.println(cnt); | |
#endif | |
if (ds18[cnt].sensorActive) | |
{ | |
type_s = 0; | |
ds.reset(); | |
ds.select(ds18[cnt].addr); | |
ds.write(0x44,1); // start conversion, with parasite power on at the end | |
delay(750); // maybe 750ms is enough, maybe not | |
// we might do a ds.depower() here, but the reset will take care of it. | |
present = ds.reset(); | |
ds.select(ds18[cnt].addr); | |
ds.write(0xBE); // Read Scratchpad | |
for ( i = 0; i < 9; i++) | |
{ // we need 9 bytes | |
data[i] = ds.read(); | |
} | |
// convert the data to actual temperature | |
unsigned int raw = (data[1] << 8) | data[0]; | |
unsigned char t_mask[4] = {0x7, 0x3, 0x1, 0x0}; | |
byte cfg = (data[4] & 0x60) >> 5; | |
raw &= ~t_mask[cfg]; | |
if( showCelsius == TRUE) | |
{ | |
ds18[cnt].deg = ((float)raw / 16.0); | |
}else{ | |
ds18[cnt].deg = ((((float)raw / 16.0) * 1.8) + 31.0); //the raw value is Celsius, convert to Fahrenheit | |
} | |
} | |
Serial.print(F("Sensor ")); | |
Serial.print(cnt); | |
Serial.println(F(" read...")); | |
if( ++cnt >= maxSensors){cnt = 0;} | |
} | |
void sendHeader(EthernetClient client, bool refresh, int time) | |
{ | |
// send a standard http response header | |
client.println(F("HTTP/1.1 200 OK")); | |
client.println(F("Content-Type: text/html")); | |
client.println(F("Connnection: close")); | |
client.println(); | |
client.println(F("<!DOCTYPE HTML>")); | |
client.println(F("<html>")); | |
if(refresh==TRUE) | |
{ | |
// add a meta refresh tag, so the browser pulls again every (time) seconds: | |
client.print(F("<head><meta http-equiv=\"refresh\" content=\"")); | |
client.print(time); | |
client.println(F("\"></head>")); | |
} | |
client.println(F("<body><table border=\"5\" align=\"center\"><tr>")); | |
} | |
void sendFooter(EthernetClient client) | |
{ | |
client.println(F("</tr></table></body>")); | |
client.println(F("</html>")); | |
} | |
void checkEthernet(void) | |
{ | |
// listen for incoming clients | |
EthernetClient client = server.available(); | |
if (client) { | |
Serial.println(F("new client")); | |
// an http request ends with a blank line | |
boolean currentLineIsBlank = true; | |
while (client.connected()) | |
{ | |
if (client.available()) | |
{ | |
memset(clientline, 0, sizeof(clientline)); // clear the clientline | |
if(client.readBytesUntil('/', clientline, BUFSIZE)) | |
{ | |
if(strcmp(clientline, "GET ") == 0 ) | |
{ | |
memset(clientline, 0, sizeof(clientline)); // clear the clientline | |
if(client.readBytesUntil('HTTP', clientline, BUFSIZE)) | |
{ | |
Serial.print(F("GET Request - ")); | |
Serial.println(clientline); | |
if(strstr(clientline, "index.htm") != 0) | |
{ | |
sendHeader(client, TRUE, 5); | |
showActiveSensors(client); | |
sendFooter(client); | |
break; | |
}else if(strstr(clientline, "findsensors.htm") != 0){ | |
sendHeader(client, TRUE, 5); | |
findSensors(client); | |
sendFooter(client); | |
break; | |
}else if(strstr(clientline, "getsensor.htm") != 0){ | |
sendHeader(client, FALSE, 0); | |
getSensors(client); | |
sendFooter(client); | |
break; | |
}else if(strstr(clientline, ".ico") != 0){ | |
Serial.println(F("dumping .ico request")); | |
client.flush(); | |
break; | |
}else if((strstr(clientline, " HTT") != 0) && | |
(strstr(clientline, ".") == 0)){ | |
// no file named at all | |
sendHeader(client, TRUE, 5); | |
showActiveSensors(client); | |
sendFooter(client); | |
break; | |
}else{ | |
noFileFound(client); | |
break; | |
} | |
} | |
}else if(strcmp(clientline, "POST ") != 0 ){ | |
Serial.print(F("POST Request - ")); | |
Serial.println(clientline); | |
if(strstr(clientline, "updatesensor") != 0) | |
{ | |
sendHeader(client, FALSE, 0); | |
updateSensor(client); | |
sendFooter(client); | |
}else if(strcmp(clientline, "modifysensor") != 0) | |
{ | |
sendHeader(client, FALSE, 0); | |
modifySensor(client); | |
sendFooter(client); | |
}else{ | |
noFileFound(client); | |
} | |
break; | |
} | |
} | |
} | |
} | |
endInternetSession(client); | |
} | |
} | |
void endInternetSession(EthernetClient client) | |
{ | |
// give the web browser time to receive the data | |
delay(1); | |
// close the connection: | |
client.stop(); | |
Serial.println(F("client disonnected")); | |
} | |
void modifySensor(EthernetClient client) | |
{ | |
int sensor, cntx, eecnt; | |
showSensorForm(client, "getsensor.htm", "get", TRUE); | |
memset(clientline, 0, sizeof(clientline)); // clear the clientline | |
index=0; | |
do{ | |
char c = client.read(); | |
// Serial.write(c); | |
clientline[index] = c; | |
if(++index >= BUFSIZE){index = 0;} | |
}while(strstr(clientline, "\n\r") == 0); | |
findString(client, "="); | |
sensor = client.parseInt(); | |
client.println(F("<td>Sensor ")); | |
client.print(sensor); | |
client.print(F("</td>")); | |
findString(client, "E&"); | |
client.print(F("<td>")); | |
if(strstr(clientline, "TRUE") != 0) | |
{ | |
ds18[sensor].sensorActive = TRUE; | |
client.print(F("TRUE")); | |
}else{ | |
ds18[sensor].sensorActive = FALSE; | |
client.print(F("FALSE")); | |
} | |
client.print(F("</td>")); | |
findString(client, "="); | |
findString(client, "&"); | |
clientline[index-1] = 0; | |
cntx=0; | |
do | |
{ | |
if(clientline[cntx] == '+') | |
{ | |
ds18[sensor].sensorName[cntx] = ' '; | |
}else{ | |
ds18[sensor].sensorName[cntx] = clientline[cntx]; | |
} | |
cntx++; | |
}while((clientline[cntx] != 0) && (cntx < sensorNameSize)); | |
Serial.print(cntx); | |
Serial.println(F(" bytes written to sensorName")); | |
do{ | |
ds18[sensor].sensorName[cntx] = ' '; | |
Serial.print(F("space #")); | |
Serial.println(cntx); | |
cntx++; | |
}while(cntx < sensorNameSize); | |
cntx--; | |
ds18[sensor].sensorName[sensorNameSize]=0; // terminate the string | |
client.print(F("<td>")); | |
client.print(ds18[sensor].sensorName); | |
client.print(F("</td>")); | |
findString(client, "="); | |
client.print(F("<td>")); | |
for(cntx = 0; cntx < 8; cntx++) | |
{ | |
ds18[sensor].addr[cntx] = client.parseInt(); | |
client.print(ds18[sensor].addr[cntx]); | |
if(cntx < 7) | |
{ | |
client.print(F(", ")); | |
findString(client, "%2C"); | |
} | |
} | |
client.print(F("</td>")); | |
findString(client, "="); | |
client.print(F("<td>")); | |
ds18[sensor].tooCold = client.parseInt(); | |
client.print(ds18[sensor].tooCold); | |
client.print(F("</td>")); | |
findString(client, "tooHot"); | |
client.print(F("<td>")); | |
ds18[sensor].tooHot = client.parseInt(); | |
client.print(ds18[sensor].tooHot); | |
eecnt = EEPROM_writeAnything(EEPROMdsAddr, ds18); // save sensor info to EEPROM | |
Serial.print(eecnt); | |
Serial.println(F(" bytes written to EEPROM")); | |
EEPROM_readAnything(EEPROMdsAddr, ds18); // get sensor info from EEPROM | |
Serial.print(eecnt); | |
Serial.println(F(" bytes read from EEPROM")); | |
client.print(F("</td></tr><tr>")); | |
client.print(F("<td colspan=\"6\" align=\"center\"> Saved to EEPROM Press Go to change another: <input type=\"submit\" name=\"GO\"></td></tr>")); | |
client.println(F("</form>")); | |
} | |
void findString(EthernetClient client, char *string) | |
{ | |
// search for a given string in the incoming stream | |
memset(clientline, 0, sizeof(clientline)); // clear the clientline | |
index=0; | |
do{ | |
char c = client.read(); | |
// Serial.write(c); | |
clientline[index] = c; | |
if(++index >= BUFSIZE){index = 0;} | |
}while((strstr(clientline, string) == 0)); | |
} | |
void updateSensor(EthernetClient client) | |
{ | |
// Serial.println(F("entering updateSensor")); | |
findString(client, "SUBMIT"); | |
if(strstr(clientline, "=Sensor") != 0) | |
{ | |
char *loc = strstr(clientline, "=Sensor"); | |
if (loc[8] == '&') | |
{ | |
loc[8]=0; | |
}else{ | |
loc[9]=0; | |
} | |
showSensorForm(client, "modifySensor.htm", "post", TRUE); | |
client.print(F("<td align=\"center\">Sensor # ")); | |
int sensor = atoi(&loc[7]); | |
client.print(sensor+1); | |
client.print(F("<input type=\"hidden\" name=\"sensor\" value=\"")); | |
client.print(sensor); | |
client.print(F("\">")); | |
client.println(F("</td>")); | |
client.print(F("<td align=\"center\"><input type=\"radio\" name=\"active\" value=\"TRUE\"")); | |
if(ds18[sensor].sensorActive == TRUE){client.print(F(" checked "));} | |
client.print(F("> TRUE <br /><input type=\"radio\" name=\"active\" value=\"FALSE\"")); | |
if(ds18[sensor].sensorActive == FALSE){client.print(F(" checked "));} | |
client.print(F("> FALSE </td>")); | |
client.print(F("<td><input type=\"text\" name=\"sensorName\" size=\"")); | |
client.print(sensorNameSize); | |
client.print(F("\" maxlength=\"")); | |
client.print(sensorNameSize-1); | |
client.print(F("\" value=\"")); | |
client.print(ds18[sensor].sensorName); | |
client.print(F("\"></td><td><input type=\"text\" size=\"35\" name=\"sensorAddr\" value=\"")); | |
for(int y = 0; y < 8; y++) | |
{ | |
client.print(ds18[sensor].addr[y]); | |
if(y<=6){client.print(F(","));} | |
} | |
client.print(F("\"></td><td><input type=\"text\" name=\"tooCold\" size=\"3\" value=\"")); | |
client.print(ds18[sensor].tooCold); | |
client.print(F("\"></td><td><input type=\"text\" name=\"tooHot\" size=\"3\" value=\"")); | |
client.print(ds18[sensor].tooHot); | |
client.println(F("\"></td></tr><tr>")); | |
client.println(F("<td colspan=\"6\" align=\"center\"> Select the fields to modify, and press SUBMIT: <input type=\"submit\" name=\"SUBMIT\"></td></tr>")); | |
client.println(F("</form>")); | |
}else{ | |
client.println(F("<td><font size=\"5\" color=\"red\">No Sensor Selected</font></td>")); | |
} | |
endInternetSession(client); | |
} | |
void noFileFound(EthernetClient client) | |
{ | |
// everything else is a 404 | |
client.println(F("HTTP/1.1 404 Not Found")); | |
client.println(F("Content-Type: text/html")); | |
client.println(); | |
client.println(F("<h2>File Not Found</h2>")); | |
} | |
void findSensors(EthernetClient client) | |
{ | |
byte addr[8]; | |
int cntx = 0; | |
while ( ds.search(addr)) | |
{ | |
client.print(F("<td>Sensor ")); | |
client.print(cntx); | |
client.print(F("= {")); | |
for( int i = 0; i < 8; i++) | |
{ | |
client.print(addr[i]); | |
if(i < 7){client.print(F(", "));} | |
} | |
client.println(F("}</td></tr><tr>")); | |
cntx++; | |
delay(500); | |
} | |
client.print(F("<td colspan=\"2\" align=\"center\">")); | |
client.print(cntx); | |
client.print(F(" Sensor")); | |
if(cntx == 1) | |
{ | |
client.println(F(" Detected")); | |
}else{ | |
client.println(F("s Detected")); | |
} | |
client.println(F("</td></tr>")); | |
ds.reset_search(); | |
} | |
void showActiveSensors(EthernetClient client) | |
{ | |
if(activeSensors == 0) | |
{ | |
client.println(F("<td align=\"center\" valign=\"center\"><font size=\"10\" color=\"red\"> No Sensors Selected </font></td>")); | |
}else{ | |
for (int n=0, y=1; n<activeSensors; n++) | |
{ | |
if(n<=maxSensors) | |
{ | |
client.print(F("<td align=\"center\" valign=\"center\"><font size=\"10\"><div style=\"width: 250px\">")); | |
if((int)ds18[n].deg > ds18[n].tooHot) | |
{ | |
client.print(F("<font color=\"red\">")); | |
}else if((int)ds18[n].deg < ds18[n].tooCold){ | |
client.print(F("<font color=\"blue\">")); | |
}else{ | |
client.print(F("<font color=\"green\">")); | |
} | |
client.print(ds18[n].sensorName); | |
client.print(F("</div><br />")); | |
client.print(ds18[n].deg, 0); | |
if (showCelsius == TRUE) | |
{ | |
client.print(F("°C")); | |
}else{ | |
client.print(F("°F")); | |
} | |
client.println(F("</font></font></td>")); | |
if(y++ == newRow) | |
{ | |
client.println(F("</tr><tr>")); | |
y = 1; | |
} | |
} | |
} | |
} | |
showSensorForm(client, "getsensor.htm", "get", FALSE); | |
client.print(F("<td align=\"center\" colspan=\"5\">Add or Update a Sensor: <button type=\"submit\">UPDATE</button></td></form>")); | |
} | |
void showSensorForm(EthernetClient client, char *filename, char *method, bool showLabels) | |
{ | |
if(showLabels == TRUE) | |
{ | |
client.print(F("<td align=\"center\">SELECT</td>")); | |
client.print(F("<td align=\"center\">ACTIVE</td>")); | |
client.print(F("<td align=\"center\">SENSOR<br />NAME</td>")); | |
client.print(F("<td align=\"center\">SENSOR ADDRESS</td>")); | |
client.print(F("<td align=\"center\">TOO<br />COLD</td>")); | |
client.print(F("<td align=\"center\">TOO<br />HOT</td>")); | |
} | |
client.print(F("</tr><tr>")); | |
client.print(F("<form id=\"getSensor\" action=\"http://")); | |
client.print(Ethernet.localIP()); | |
client.print(F("/")); | |
client.print(filename); | |
client.print(F("\" method=\"")); | |
client.print(method); | |
client.println(F("\">")); | |
} | |
void getSensors(EthernetClient client) | |
{ | |
activeSensors = 0; | |
showSensorForm(client, "updatesensor.htm", "post", TRUE); | |
int x, y; | |
for(x = 0; x < maxSensors; x++) | |
{ | |
client.print(F("<td><input type=\"radio\" name=\"Sensor\" value=\"Sensor")); | |
client.print(x); | |
client.print(("\">Sensor ")); | |
if(x < 10){client.print(F(" "));} | |
client.print(x+1); | |
client.print(F("</td><td align=\"center\">")); | |
if(ds18[x].sensorActive==TRUE) | |
{ | |
client.print(F("TRUE")); | |
activeSensors++; | |
}else{ | |
client.print(F("FALSE")); | |
} | |
client.print(F("</td><td>")); | |
for(y = 0; y < strlen(ds18[x].sensorName); y++) | |
{ | |
if(ds18[x].sensorName[y] == ' ') | |
{ | |
client.print(F(" ")); | |
}else{ | |
client.write(ds18[x].sensorName[y]); | |
} | |
} | |
client.print(F("<td align=\"center\">")); | |
for(y = 0; y < 8; y++) | |
{ | |
client.print(ds18[x].addr[y]); | |
if(y<=6){client.print(F(","));} | |
} | |
client.print(F("</td><td align=\"center\">")); | |
client.print(ds18[x].tooCold); | |
client.print(F("</td><td align=\"center\">")); | |
client.print(ds18[x].tooHot); | |
client.println(F("</td></tr><tr>")); | |
} | |
client.println(F("<td colspan=\"6\" align=\"center\"> Select a sensor to modify, and press SUBMIT: <input type=\"submit\" name=\"SUBMIT\"></td></tr>")); | |
client.println(F("</form>")); | |
client.println(F("</tr></table><br /><br /><table border=\"5\" align=\"center\"><tr>"));; | |
findSensors(client); | |
} | |
void displayLCDTemp() | |
{ | |
lcd.home(); | |
if(activeSensors==0) | |
{ | |
lcd.setBacklight(BLOFF); | |
delay(500); | |
lcd.setBacklight(RED); | |
#if defined D16X2 | |
lcd.print(F("Sensor Selected?")); | |
lcd.setCursor(0, 1); | |
lcd.print(F("Sensor Selected?")); | |
#endif | |
#if defined D20X4 | |
lcd.setCursor(0, 0); | |
lcd.print(F("No Sensors Selected!")); | |
lcd.setCursor(0, 1); | |
lcd.print(F("No Sensors Selected!")); | |
lcd.setCursor(0, 2); | |
lcd.print(F("No Sensors Selected!")); | |
lcd.setCursor(0, 3); | |
lcd.print(F("No Sensors Selected!")); | |
#endif | |
}else{ | |
showRed = FALSE; | |
showBlue = FALSE; | |
for(int n=rowNum;n<activeSensors;n++) | |
{ | |
if(ds18[n].deg > ds18[n].tooHot) | |
{ | |
showRed = TRUE; | |
showBlue = FALSE; | |
}else if(ds18[n].deg < ds18[n].tooCold){ | |
showBlue = TRUE; | |
} | |
} | |
if(showRed) | |
{ | |
lcd.setBacklight(BLOFF); | |
delay(500); | |
lcd.setBacklight(RED); | |
}else if(showBlue){ | |
lcd.setBacklight(BLOFF); | |
delay(500); | |
lcd.setBacklight(BLUE); | |
}else{ | |
lcd.setBacklight(GREEN); | |
} | |
for(int n=rowNum;n<activeSensors;n++) | |
{ | |
if(ds18[n].deg>=100) | |
{ | |
addFSpace=TRUE; | |
break; | |
}else{ | |
addFSpace=FALSE; | |
} | |
} | |
if((activeSensors > rows) && (rowNum+rows) >= activeSensors){rowNum = activeSensors-rows;} | |
for(int n=rowNum,cnt=0;cnt<rows;cnt++,n++) | |
{ | |
lcd.setCursor(0, cnt); | |
if(ds18[n].sensorActive == TRUE) | |
{ | |
lcd.setCursor(0, cnt); | |
lcd.print(ds18[n].sensorName); | |
if(ds18[n].deg < 100 && addFSpace) | |
{ | |
lcd.print(F(" = ")); | |
}else{ | |
lcd.print(F(" = ")); | |
} | |
lcd.print(ds18[n].deg,0); | |
lcd.print(degChar); | |
if(addFSpace == FALSE){lcd.print(F(" "));} | |
#if defined SerialDebug | |
Serial.print(F("n = ")); | |
Serial.print(n); | |
Serial.print(F(" cnt = ")); | |
Serial.print(cnt); | |
Serial.print(F(" name = ")); | |
Serial.print(ds18[n].sensorName); | |
Serial.print(F(" temp = ")); | |
Serial.println(ds18[n].deg,0); | |
#endif | |
}else{ | |
lcd.print(ds18[n].sensorName); | |
lcd.print(F(" OFF ")); | |
#if defined SerialDebug | |
Serial.print(ds18[n].sensorName); | |
Serial.println(F(" Inactive")); | |
#endif | |
} | |
} | |
} | |
} | |
void checkButtons() | |
{ | |
uint8_t buttons = lcd.readButtons(); | |
switch(buttons) | |
{ | |
case BUTTON_UP: | |
if(rowNum > 0){rowNum--;} | |
#if defined SerialDebug | |
Serial.print(F("UP Button Pushed ")); | |
Serial.print(F("rowNum = ")); | |
Serial.println(rowNum); | |
#endif | |
break; | |
case BUTTON_DOWN: | |
if(rowNum < maxSensors-1){rowNum++;} | |
#if defined SerialDebug | |
Serial.println(F("DOWN Button Pushed")); | |
Serial.print(F("rowNum = ")); | |
Serial.println(rowNum); | |
#endif | |
break; | |
case BUTTON_RIGHT: | |
#if defined SerialDebug | |
Serial.println(F("RIGHT Button Pushed")); | |
#endif | |
break; | |
case BUTTON_LEFT: | |
#if defined SerialDebug | |
Serial.println(F("LEFT Button Pushed")); | |
#endif | |
break; | |
case BUTTON_SELECT: | |
#if defined SerialDebug | |
Serial.println(F("SELECT Button Pushed")); | |
#endif | |
rowNum = 0; | |
break; | |
} | |
} | |
void logData(void) | |
{ | |
EthernetClient cosmClient; | |
String tempString; | |
int tempStringLength; | |
for(int id = 0; id < activeSensors; id++) | |
{ | |
tempString = String(ds18[id].sensorName); | |
tempString.trim(); | |
tempString += ","; | |
tempString += (int) ds18[id].deg; | |
tempString += "\n"; | |
} | |
Serial.print(F("String Data = ")); | |
Serial.println(tempString); | |
tempStringLength = tempString.length(); | |
if (cosmClient.connect(cosmServer, 80)) | |
{ | |
Serial.println(F("connecting to cosm")); | |
// send the HTTP PUT request: | |
cosmClient.print(F("PUT /v2/feeds/")); | |
cosmClient.print(FEEDID); | |
cosmClient.println(F(".csv HTTP/1.1")); | |
cosmClient.println(F("Host: api.cosm.com")); | |
cosmClient.print(F("X-ApiKey: ")); | |
cosmClient.println(APIKEY); | |
cosmClient.print(F("User-Agent: ")); | |
cosmClient.println(USERAGENT); | |
cosmClient.print(F("Content-Length: ")); | |
cosmClient.println(tempStringLength); | |
// last pieces of the HTTP PUT request: | |
cosmClient.println(F("Content-Type: text/csv")); | |
cosmClient.println(F("Connection: close")); | |
cosmClient.println(); | |
// here's the actual content of the PUT request: | |
cosmClient.println(tempString); | |
// note the time that the connection was made or attempted | |
Serial.println(F("cosm connection success")); | |
}else { | |
// if you couldn't make a connection: | |
Serial.println(F("cosm connection failed")); | |
} | |
Serial.println(F("disconnecting.")); | |
cosmClient.stop(); | |
} | |
void loop(void) | |
{ | |
// Serial.println(F("Entering getOneWire...")); | |
getOneWire(); | |
// Serial.println(F("Entering checkEthernet...")); | |
checkEthernet(); | |
// Serial.println(F("Entering checkButtons...")); | |
checkButtons(); | |
// Serial.println(F("Entering displayLCDTemp...")); | |
displayLCDTemp(); | |
if(millis() > lastConnectionTime + postingInterval) | |
{ | |
Serial.println(F("Entering logData...")); | |
lastConnectionTime = millis(); | |
logData(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment