Ss Brewing Technologies Giveaway!

Home Brew Forums > Home Brewing Beer > DIY Projects > Arduino - Networked Temperature Monitor
Reply
 
LinkBack Thread Tools
Old 07-09-2012, 04:36 PM   #11
phoenixs4r
Feedback Score: 0 reviews
Recipes 
 
Join Date: Jun 2011
Location: Hayward, California
Posts: 1,506
Liked 54 Times on 49 Posts
Likes Given: 38

Default

Quote:
Originally Posted by jimmayhugh View Post
FYI, I just created two new temp probes using a flat flexible telephone extension cable that was lying around. It was a flat 4-wire cable, and the two probe cables are about 10ft each, and are working just fine.
Neat
phoenixs4r is offline
 
Reply With Quote Quick reply to this message
Old 07-09-2012, 10:03 PM   #12
jimmayhugh
Turgid Member
HBT_LIFETIMESUPPORTER.png
Feedback Score: 0 reviews
 
jimmayhugh's Avatar
Recipes 
 
Join Date: Feb 2011
Location: Las Vegas, NV
Posts: 581
Liked 46 Times on 41 Posts
Likes Given: 2

Default

Today i'm futzing around with a 4x20 non-RGB I2C display. Since I have a few extra pins, I added an RGB LED and some extra code. I'm going to try to make a "master" file that will compile with different boards (regular LCD, I2C LCD, RGB I2C LCD, 16x2, 20x4, 40x4, sd card, Uno Mega,etc), that can be configured via defines and then compiled for a specific configuration. Some thing are just not doable (sd card with ethernet and standard LCD shield on an Uno for instance) because of pin conflicts, or just running out of pins.

This is a Arduino Uno / Ethernet Shield Combo with a breadboard / screw shield sandwiched in between:

jimmayhugh is offline
 
Reply With Quote Quick reply to this message
Old 07-10-2012, 01:05 AM   #13
carlisle_bob
Feedback Score: 0 reviews
Recipes 
 
Join Date: Apr 2012
Location: Carlisle, PA
Posts: 1,205
Liked 27 Times on 26 Posts

Default

Quote:
Originally Posted by jimmayhugh View Post
I bought 50 of the DS18B20's and breadboarded 15 of them, no problems. As long as you don't use parasitic mode, ie, as long as you use a single pull-up resistor on the data line, I think the biggest problem would be the time lag polling each probe.

I'm using an RJ-45 patch panel wired in parallel, and am using some of the several bazillion ethernet cables I have laying around to make new probes.

Supposedly there can be reflection problems if you use a star-type network instead of a short stub off of a main line, but I haven't seen it in my tests.

Hi

Unless you are running very fast edges (why would you?) or very long cables (like 10's of feet) reflections are un-likey to be an issue. Bulk capacitance it going to kill you first.

Bob
__________________
carlisle_bob is offline
 
Reply With Quote Quick reply to this message
Old 07-13-2012, 10:29 PM   #14
jimmayhugh
Turgid Member
HBT_LIFETIMESUPPORTER.png
Feedback Score: 0 reviews
 
jimmayhugh's Avatar
Recipes 
 
Join Date: Feb 2011
Location: Las Vegas, NV
Posts: 581
Liked 46 Times on 41 Posts
Likes Given: 2

Default

Quote:
Originally Posted by carlisle_bob View Post
Hi

Unless you are running very fast edges (why would you?) or very long cables (like 10's of feet) reflections are un-likey to be an issue. Bulk capacitance it going to kill you first.

Bob
I just made 4 probe cables using surplus telephone cords, and ran them through a standard multi-line adapter:


Three of them are about 10ft long, and the coil on the table is a 50ft coil, and they all seem to work fine.

If you're planning to use the telephone cords to make probe cables, be aware that the phone cable is essentially a crossover-style cable, as the connectors are not both wired the same, so make sure that you use the same end connector for each cable. I use the red wire for VDD, black for VSS and the green and yellow wires are twisted together and used for the data line.
jimmayhugh is offline
 
Reply With Quote Quick reply to this message
Old 07-14-2012, 01:42 AM   #15
gromitdj
Feedback Score: 0 reviews
Recipes 
 
Join Date: Feb 2008
Posts: 141
Liked 7 Times on 5 Posts
Likes Given: 1

Default

Quote:
Originally Posted by jimmayhugh View Post
If you're planning to use the telephone cords to make probe cables, be aware that the phone cable is essentially a crossover-style cable, as the connectors are not both wired the same, so make sure that you use the same end connector for each cable. I use the red wire for VDD, black for VSS and the green and yellow wires are twisted together and used for the data line.
This is how I built my probes and ran into this exact problem, not realizing that they were crossover-style. Took me a while to figure out why that sensor wasn't working. This is a great tip for anyone looking to do this. My sensors work great. I used a wall jack with modular plugs in my enclosure for attachment.
__________________

Donnie

gromitdj is offline
 
Reply With Quote Quick reply to this message
Old 07-14-2012, 11:57 PM   #16
jimmayhugh
Turgid Member
HBT_LIFETIMESUPPORTER.png
Feedback Score: 0 reviews
 
jimmayhugh's Avatar
Recipes 
 
Join Date: Feb 2011
Location: Las Vegas, NV
Posts: 581
Liked 46 Times on 41 Posts
Likes Given: 2

Default 320x240 graphic LCD

I also have a 3.2" 320x240 graphic LCD that can be used in portrait or landscape mode, with small or larger fonts, that can display up to 21 probes, each with settable parameters and colors.

Landscape with large font:


Portrait with small font:


Here's the current code for the Graphical LCD ONLY:

Code:
/********** ITDB02 Networked Temperature **********
This program requires the ITDB02_Graph16 library.
It uses the OneWire Library and an Arduino-compatable
ethernet shield The display can be used with small or
large fonts, and in portrait or landscape mode.

The OneWire runs on digital pin 9 to avoid a conflict
with the LCD software.
**************************************************/

/********** Various Defines **********/

// #define SerialDebug    // Used for debugging with the Serial Terminal, disable for normal use
// #define GetAddresses   // Used to get Sensor Addresses 
// #define EthernetDebug  // Used to debug Ethernet problems

// #define P15X16         // define 15 column by 16 row display in portrait mode
// #define P30X21         // define 30 column by 21 row display in portrait mode
// #define L40X16         // define 40 column by 20 row display in landscape mode
// #define L20X12         // define 20 column by 15 row display in landscapr mode

#include <Wire.h>
#include <ITDB02_Graph16.h>
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>

// Declare which fonts we will be using
#if defined P30X21
extern uint8_t SmallFont[];
char *blankLine = "                              ";
#endif

#if defined L40X16
extern uint8_t SmallFont[];
char *blankLine = "                                        ";
#endif

#if defined P15X16
extern uint8_t BigFont[];
char *blankLine = "               ";
#endif

#if defined L20X12
extern uint8_t BigFont[];
char *blankLine = "                    ";
#endif

//myGLCD(RS,WR,CS,RST,ALE,mode);
ITDB02 myGLCD(A1,A2,A0,A4,A5,2);   //

const byte nbsp            = 0x20; // space character
const byte zeroChar        = 0x30; // zero character
const char degChar         = 0xdf; // degree character
const byte orientLandscape = 0x01;
const byte orientPortrait  = 0x00;

// if set to TRUE, blank lines are printed for lines with no probe
// if set to FALSE, lines with no probe are marked in yellow
bool showBlankLine = TRUE;

/********** Ethernet / Web Stuff **********
 Enter a MAC address and IP address for your controller below.
 The IP address will be dependent on your local network:
 The Ethernet hardware utilizes digital pins 10, 11, 12, and 13.
 ********** Ethernet / Web Stuff **********/
byte mac[] = { 0xDE, 0xAD, 0xBA, 0xBE, 0x00, 0x04 };
IPAddress ip(192,168,1,175);
const int BUFSIZE = 100; // buffer for internet request
const int rowSize = 4;

// 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  = 9; // One Wire data Bus
OneWire  ds(dsDataPin); // OneWire bus on pin 2
bool showCelsius = FALSE;

typedef struct
{
  bool  sensorActive; // set to "TRUE" if you put a sensor address in the "addr" array.
  char  *sensorName;
  char  *xmlName;
  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 P15X16
const int  maxSensors = 16; // total number of DS18B20 sensors
Sensor ds18[maxSensors] =
{

  {FALSE, "Sensor  1", "Sensor1",  {0,0,0,0,0,0,0,0}, 0.0, 90, 100},
  {FALSE, "Sensor  2", "Sensor2",  {0,0,0,0,0,0,0,0}, 0.0, 70, 80},
  {FALSE, "Sensor  3", "Sensor3",  {0,0,0,0,0,0,0,0}, 0.0, 70, 90},
  {FALSE, "Sensor  4", "Sensor4",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  5", "Sensor5",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  6", "Sensor6",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  7", "Sensor7",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  8", "Sensor8",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  9", "Sensor9",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 10", "Sensor10", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 11", "Sensor11", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 12", "Sensor12", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 13", "Sensor13", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 14", "Sensor14", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 15", "Sensor15", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 16", "Sensor16", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}
};
#endif

#if defined L20X12
const int  maxSensors = 12; // total number of DS18B20 sensors
Sensor ds18[maxSensors] =
{
  {FALSE, "Sensor  1", "Sensor1",  {0,0,0,0,0,0,0,0}, 0.0, 90, 100},
  {FALSE, "Sensor  2", "Sensor2",  {0,0,0,0,0,0,0,0}, 0.0, 70, 80},
  {FALSE, "Sensor  3", "Sensor3",  {0,0,0,0,0,0,0,0}, 0.0, 70, 90},
  {FALSE, "Sensor  4", "Sensor4",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  5", "Sensor5",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  6", "Sensor6",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  7", "Sensor7",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  8", "Sensor8",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor  9", "Sensor9",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 10", "Sensor10", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 11", "Sensor11", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor 12", "Sensor12", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}
};
#endif

#if defined P30X21
const int  maxSensors = 21; // total number of DS18B20 sensors
Sensor ds18[maxSensors] =
{
  {FALSE, "Sensor      1", "Sensor1",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor      2", "Sensor2",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor      3", "Sensor3",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor      4", "Sensor4",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor      5", "Sensor5",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor      6", "Sensor6",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor      7", "Sensor7",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor      8", "Sensor8",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor      9", "Sensor9",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     10", "Sensor10", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     11", "Sensor11", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     12", "Sensor12", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     13", "Sensor13", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     14", "Sensor14", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     15", "Sensor15", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     16", "Sensor16", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     17", "Sensor17", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     18", "Sensor18", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     19", "Sensor19", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     20", "Sensor20", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor     21", "Sensor21", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}
};
#endif

#if defined L40X16
const int  maxSensors = 16; // total number of DS18B20 sensors
Sensor ds18[maxSensors] =
{
  {FALSE, "Sensor                  1", "Sensor1",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                  2", "Sensor2",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                  3", "Sensor3",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                  4", "Sensor4",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                  5", "Sensor5",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                  6", "Sensor6",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                  7", "Sensor7",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                  8", "Sensor8",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                  9", "Sensor9",  {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                 10", "Sensor10", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                 11", "Sensor11", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                 12", "Sensor12", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                 13", "Sensor13", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                 14", "Sensor14", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                 15", "Sensor15", {0,0,0,0,0,0,0,0}, 0.0, 45, 55},
  {FALSE, "Sensor                 16", "Sensor16", {0,0,0,0,0,0,0,0}, 0.0, 45, 55}
};
#endif

/********** IMPORTANT!! **********
BE SURE TO SET THIS VALUE TO THE
   NUMBER OF ACTIVE SENSORS!!
*********** IMPORTANT!! **********/
int activeSensors = 0;
int cnt = 0;

void setup()
{
  
#if defined SerialDebug || defined GetAddresses || defined EthernetDebug
  Serial.begin(9600);
  delay(1000);
  Serial.println(F("Serial Debug running"));
#endif

#if defined P15X16 || defined P30X21
  myGLCD.InitLCD(PORTRAIT);
#endif

#if defined L40X16 || defined L20X12
  myGLCD.InitLCD(LANDSCAPE);
#endif

#if defined P15X16 || defined L20X12
  myGLCD.setFont(BigFont);
#endif

#if defined L40X16 || defined P30X21
  myGLCD.setFont(SmallFont);
#endif

#if defined SerialDebug
  Serial.println(F("InitLCD"));
#endif

  myGLCD.clrScr();
  myGLCD.setBackColor(0,0,0);
  myGLCD.fillScr(0,0,0);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
#if defined SerialDebug || defined EthernetDebug
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
#endif
}

void checkEthernet(void)
{
  char clientline[BUFSIZE];
  int index = 0;

 // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    
    Serial.println("new client");

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        // If it isn't a new line, add the character to the buffer
        if (c != '\n' && c != '\r')
        {
          clientline[index] = c;
          index++;
          // are we too big for the buffer? start tossing out data
          if (index >= BUFSIZE) 
            index = BUFSIZE -1;
          
          // continue to read more data!
          continue;
        }
        
        // got a \n or \r new line, which means the string is done
        clientline[index] = 0;
        
        // Print it out for debugging
        
        Serial.println(clientline);


        // 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)
        {
          if((strstr(clientline, "GET / ") != 0) ||
             (strstr(clientline, "index.htm") != 0) ||
             (strstr(clientline, "index.html") != 0))
          {
            // 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("<html><head>");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
            client.println(F("<meta http-equiv=\"refresh\" content=\"5\">"));
            client.println(F("</head>"));
            client.println(F("<body><table border=\"5\" align=\"center\"><tr>"));
            if(activeSensors == 0)
            {
              client.println(F("<td align=\"center\" valign=\"center\"><font size=\"10\" color=\"red\">&nbsp;&nbsp;No Sensors Selected&nbsp;&nbsp;</font></td></tr></table></body></html>"));
              break;
            }else{
              for (int n=0; n<activeSensors; n++)
              {
                if(n<=maxSensors)
                {
                  client.print(F("<td align=\"center\" valign=\"center\"><font size=\"10\">&nbsp;&nbsp;"));
                  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("&nbsp;&nbsp;<br />"));
                  client.print(ds18[n].deg, 0);
                  if (showCelsius == TRUE)
                  {
                    client.print(F("&deg;C"));
                  }else{
                    client.print(F("&deg;F"));
                  }
                  client.println(F("</font></font></td>"));
                }
                if((n+1) % rowSize == 0)
                {
                  client.println(F("</tr><tr>"));
                }
              }
              client.println(F("</tr></table></body>"));
              client.println(F("</html>"));
              break;
            }
          }else if (strstr(clientline, "buttcrack.xml") !=0){
#if defined SerialDebug
            Serial.print(F("Sending buttcrack.xml..."));
#endif
            client.println(F("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"));
            client.println(F("<!--Arduino Buttcrack-->"));
            client.println(F("<buttcrack>"));
            for (int n = 0; n < activeSensors; n++)
            {
              client.print(F("<"));
              client.print(ds18[n].xmlName);
              client.print(F(">"));
              client.print(ds18[n].deg,0);
              client.print(F("</"));
              client.print(ds18[n].xmlName);
              client.println(F(">"));
            }
            client.println(F("</buttcrack>"));
            delay(1);
            client.stop();
          }else{
            // 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>"));
            delay(1);
            client.stop();
          }
          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 defined SerialDebug || defined EthernetDebug
    Serial.println(F("client disonnected"));
#endif
  }
}

void getOneWire(void)
{
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];

#if defined SerialDebug
  Serial.print(F("ROM "));
  Serial.print(cnt);
  Serial.print(F(" = "));
  for( i = 0; i < 8; i++)
  {
    Serial.write(nbsp);
    if(ds18[cnt].addr[i] < 16){Serial.write(zeroChar);} // prepend hex 0-15 with a 0
    Serial.print(ds18[cnt].addr[i], HEX);
  }

  if (OneWire::crc8(ds18[cnt].addr, 7) != ds18[cnt].addr[7]) {
      Serial.println(F("CRC is not valid!"));
      return;
  }
  Serial.println();
#endif
  if (ds18[cnt].sensorActive == TRUE)
  {
  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

#if defined SerialDebug
  Serial.print(F("  Data = "));
  if(present < 16){Serial.write(zeroChar);} // prepend hex 0-15 with a 0
  Serial.print(present, HEX);
  Serial.write(nbsp);
#endif
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
#if defined SerialDebug
    if(data[i] < 16){Serial.write(zeroChar);} // prepend hex 0-15 with a 0
    Serial.print(data[i], HEX);
    Serial.write(nbsp);
#endif
  }
#if defined SerialDebug
  Serial.print(F(" CRC="));
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();
#endif
  // 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
  }

#if defined SerialDebug
  Serial.print("  Temperature = ");
  Serial.print(ds18[cnt].deg);
  if( showCelsius == TRUE)
  {
    Serial.println(" Celsius");
  }else{
    Serial.println(" Fahrenheit");
  }
#endif
  }
  if( ++cnt >= maxSensors){cnt = 0;}

}

void displayLCD(void)
{
#if defined SerialDebug
  Serial.println(F("Entering displayLCD()"));
#endif
// Clear the screen and draw the frame
//  myGLCD.clrScr();
  
  myGLCD.setColor(255,255,255);
  myGLCD.setBackColor(0, 0, 0);
#if defined P30X21
  for (int x=0, y=0;x<310;x+=15,y++)
#endif
#if defined P15X16
  for (int x=0, y=0;x<319;x+=20,y++)
#endif
#if defined L40X16
  for (int x=0, y=0;x<230;x+=15,y++)
#endif
#if defined L20X12
  for (int x=0, y=0;x<239;x+=20,y++)
#endif
  {
    if(ds18[y].sensorActive == TRUE)
    {
      if(ds18[y].deg > ds18[y].tooHot)
      {
        myGLCD.setBackColor(255, 0, 0);
      }else if(ds18[y].deg < ds18[y].tooCold){
        myGLCD.setBackColor(0, 0, 255);
      }else{
        myGLCD.setBackColor(0, 255, 0);
      }
#if defined P30X21
//      myGLCD.print(blankLine,0,x);
      myGLCD.print(ds18[y].sensorName,0,x);
      int temp = (int) ds18[y].deg;
      if(temp >= 100)
      {
        myGLCD.print(" = ", (13*8), x);
        myGLCD.printNumI(temp, (17*8), x);
      }else{
        myGLCD.print(" =  ", (13*8), x);
        myGLCD.printNumI(temp, (17*8), x);
      }
#endif
#if defined P15X16
//      myGLCD.print(blankLine,0,x);
      myGLCD.print(ds18[y].sensorName,0,x);
      int temp = (int) ds18[y].deg;
      if(temp >= 100)
      {
        myGLCD.print(" = ", (8*16), x);
        myGLCD.printNumI(temp, (11*16), x);
      }else{
        myGLCD.print(" =  ", (9*16), x);
        myGLCD.printNumI(temp, (12*16), x);
      }
#endif
#if defined L40X16
//      myGLCD.print(blankLine,0,x);
      myGLCD.print(ds18[y].sensorName,0,x);
      int temp = (int) ds18[y].deg;
      if(temp >= 100)
      {
        myGLCD.print(" = ", (24*8), x);
        myGLCD.printNumI(temp, (28*8), x);
      }else{
        myGLCD.print(" =  ", (25*8), x);
        myGLCD.printNumI(temp, (29*8), x);
      }
#endif
#if defined L20X12
//      myGLCD.print(blankLine,0,x);
      myGLCD.print(ds18[y].sensorName,0,x);
      int temp = (int) ds18[y].deg;
      if(temp >= 100)
      {
        myGLCD.print(" = ", (9*16), x);
        myGLCD.printNumI(temp, (12*16), x);
      }else{
        myGLCD.print(" =  ", (9*16), x);
        myGLCD.printNumI(temp, (13*16), x);
      }
#endif
    }else{
      if(showBlankLine == FALSE)
      {
        myGLCD.setColor(255,0,0);
        myGLCD.setBackColor(255, 255, 0);
        myGLCD.print(ds18[y].sensorName,0,x);
#if defined P30X21
        myGLCD.print(" NOT ACTIVE ", (13*8), x);
#endif
#if defined L40X16
        myGLCD.print(" NOT ACTIVE ", (25*8), x);
#endif
#if defined P15X16
        myGLCD.print(" OFF ", (9*16), x);
#endif
#if defined L20X12
        myGLCD.print(" NOT ACTIVE", (9*16), x);
#endif
        myGLCD.setColor(255,255,255);
      }else{
        myGLCD.setBackColor(0,0,0);
        myGLCD.setColor(255,255,255);
        myGLCD.print(blankLine, 0, x);
      }
    }
  }
}

#if defined SerialDebug || defined GetAddresses
void displayAddresses(void)
{

/********** Get Sensor Addresses **********
   Used to get initial values for Sensor structure. Enable SerialDebug, and obtain DS18B20 addresses
   from the serial terminal. Plug those values into the Sensor's "addr" array, re-compile and upload
   with SeralDebug disabled. Note that when SerialDebug is enabled, response times on buttons and
   display are greatly reduced, so be sure to disable SerialDebug when not needed.
******************************************/

 byte addr[8];
 int cntx = 0;
 
  while ( ds.search(addr))
  {
    Serial.print(F("Sensor "));
    Serial.print(cntx);
    Serial.print(F("= {"));
    for( int i = 0; i < 8; i++)
    {
      Serial.print(F("0x"));
      if(addr[i] < 16){Serial.write(0x30);} // prepend hex 0-15 with a 0
      Serial.print(addr[i], HEX);
      if(i < 7){Serial.print(F(","));}
    }
    Serial.println(F("}"));
    cntx++;
    delay(500);
  }
  Serial.print(cntx);
  Serial.print(F(" Sensor"));
  if(cntx == 1)
  {
    Serial.println(F(" detected"));
  }else{
     Serial.println(F("s detected"));
 }
  
  ds.reset_search();
}
#endif

void loop()
{
#if defined SerialDebug || defined GetAddresses 
  displayAddresses();
#endif
  getOneWire();
  checkEthernet();
  displayLCD();
}
__________________
TeensyNet Networked Controller
ButtCrack Brewery
And This Is Why We Drink...
jimmayhugh is offline
 
Reply With Quote Quick reply to this message
Old 07-28-2012, 03:33 AM   #17
jimmayhugh
Turgid Member
HBT_LIFETIMESUPPORTER.png
Feedback Score: 0 reviews
 
jimmayhugh's Avatar
Recipes 
 
Join Date: Feb 2011
Location: Las Vegas, NV
Posts: 581
Liked 46 Times on 41 Posts
Likes Given: 2

Default Busy, Busy...

OK more fooling around

I wrote a network-only (no LCD display) monitor that can be updated with new sensors via web pages, and saves the sensor structures info to the Arduino EEPROM.

When a brand-new Arduino Uno is downloaded with this code, the first page that show up should look like this:


Pressing the "UPDATE" button brings up this screen:


The upper table has the available structures, while the lower table shows the currently detected sensors. Select a structure to modify and press the "Submit" button and the following screen appears:


After you update your info, make sure the sensor is set to "Active", and press "Submit". The data will be written to the EEPROM, and a confirmation screen appears:


After you've updated the sensors, you can then monitor the sensors via the network:


This code is RAM-intensive,and has limited EEPROM on board, so it's currently limited to 10 sensors on the Uno. The Arduino Mega should be able to handle more sensors, and operate an LCD with room to spare. I'll let you know

Here's the current code:

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <EEPROM.h>
#include "EEPROMAnything.h"
#include <OneWire.h>

byte mac[] = { 0xDE, 0xAD, 0xBA, 0xBE, 0xFE, 0xED };
IPAddress ip(192,168,1,146);

const int BUFSIZE = 300; // buffer for internet request
char clientline[BUFSIZE];
int index = 0;
const int newRow = 5;

// 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
int        maxSensors; // total number of DS18B20 sensors
OneWire  ds(dsDataPin); // OneWire bus on pin 2

const byte nbsp        = 0x20; // space character
const byte zeroChar    = 0x30; // zero character
const char degChar     = 0xdf; // degree character
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[20];
  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;

Sensor ds18[] =
{
  {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}
};

int activeSensors = 0;

int cnt = 0;

void setup()
{
// Open serial communications and wait for port to open:
  Serial.begin(9600);
    
  maxSensors= sizeof(ds18) / sizeof(Sensor);
  
  EEPROM_readAnything(cnt, ds18); // get sensor structures from EEPROM
  
  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();

   Serial.print("server is at ");
   Serial.println(Ethernet.localIP());
}

void getOneWire(void)
{
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];

  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
    }
  }
  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("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"));
                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;
  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);
  ds18[sensor].sensorName[cntx]=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);
  for(int i=0; i < 512; i++){EEPROM.write(i, 0);} //clear EEPROM
  EEPROM_writeAnything(0, ds18); // save sensor info to EEPROM
  EEPROM_readAnything(0, ds18); // get sensor info 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=\"15\" 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\">&nbsp;&nbsp;No Sensors Selected&nbsp;&nbsp;</font></td>"));
  }else{
    for (int n=0; n<activeSensors; n++)
    {
      if(n<=maxSensors)
      {
        client.print(F("<td align=\"center\" valign=\"center\"><font size=\"10\">&nbsp;&nbsp;"));
        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("&nbsp;&nbsp;<br />"));
        client.print(ds18[n].deg, 0);
        if (showCelsius == TRUE)
        {
          client.print(F("&deg;C"));
        }else{
          client.print(F("&deg;F"));
        }
        client.println(F("</font></font></td>"));
        if(n == newRow){client.println(F("</tr><tr>"));}
      }
    }
  }
  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&nbsp;"));
    if(x < 10){client.print(F("&nbsp;"));}
    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("&nbsp;"));
      }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 loop(void)
{
  getOneWire();
  checkEthernet();
  delay(500);
}
__________________
TeensyNet Networked Controller
ButtCrack Brewery
And This Is Why We Drink...

Last edited by jimmayhugh; 07-28-2012 at 12:32 PM. Reason: Code Correction
jimmayhugh is offline
 
Reply With Quote Quick reply to this message
Old 07-28-2012, 07:33 PM   #18
gabeweisz
Feedback Score: 0 reviews
Recipes 
 
Join Date: Nov 2007
Posts: 171
Liked 2 Times on 2 Posts
Likes Given: 3

Default

Pretty neat. Since you have the arduino and probes, why not also get some SSRs and control the temperature too?

__________________
gabeweisz is offline
 
Reply With Quote Quick reply to this message
Old 07-28-2012, 09:57 PM   #19
jimmayhugh
Turgid Member
HBT_LIFETIMESUPPORTER.png
Feedback Score: 0 reviews
 
jimmayhugh's Avatar
Recipes 
 
Join Date: Feb 2011
Location: Las Vegas, NV
Posts: 581
Liked 46 Times on 41 Posts
Likes Given: 2

Default

Quote:
Originally Posted by gabeweisz View Post
Pretty neat. Since you have the arduino and probes, why not also get some SSRs and control the temperature too?
All in good time time

The Mega2560 has WAY more memory, flash, eeprom, and I/O, so I'm playing with it now.
__________________
TeensyNet Networked Controller
ButtCrack Brewery
And This Is Why We Drink...
jimmayhugh is offline
 
Reply With Quote Quick reply to this message
Old 07-29-2012, 04:12 PM   #20
jimmayhugh
Turgid Member
HBT_LIFETIMESUPPORTER.png
Feedback Score: 0 reviews
 
jimmayhugh's Avatar
Recipes 
 
Join Date: Feb 2011
Location: Las Vegas, NV
Posts: 581
Liked 46 Times on 41 Posts
Likes Given: 2

Default Mega2560 Update

Got the code running on the Mega2650 with a minimum of fuss. There are now 20 probe structures available, but more could probably be added.

It now supports the Adafruit I2C RGB-LCD display in either 16x2 or 20x4 mode. I chose this particular board since with a little bit of hardware diddling and some minor code changes, 6 more displays could be added to display up to 28 probes

I also added a COSM (formerly panchube) feed to allow online datalogging and display.

Here's the code. You'll have to get your own COSM account and plug in the relevant codes:

Code:
/*
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
// You can connect other I2C sensors to the I2C bus and share
// the I2C bus.

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// 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,146); // **local IP Address** insert your address


char cosmServer[] = "api.cosm.com";   // name address for cosm API
unsigned long lastConnectionTime = 0;          // 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         "YOUR COSM KEY GOES HERE" // your cosm api key
#define FEEDID         **YOUR ID GOES HERE** // your feed ID
#define USERAGENT      "Mega 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);
}

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
    }
  }
    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"));
                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\">&nbsp;&nbsp;No Sensors Selected&nbsp;&nbsp;</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("&deg;C"));
        }else{
          client.print(F("&deg;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&nbsp;"));
    if(x < 10){client.print(F("&nbsp;"));}
    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("&nbsp;"));
      }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 client;
  String tempString = "";
  int  tempStringLength;
  
  if((millis() - lastConnectionTime < postingInterval) || (activeSensors == 0)) return;

  for(int id = 0; id < activeSensors; id++)
  {
    tempString += String(id);
    tempString += ",";
    tempString += String((int) ds18[id].deg);
    tempString += "\n";
  }
  tempStringLength = tempString.length();
  if (client.connect(cosmServer, 80))
  {
    Serial.println(F("connecting to cosm"));
    // send the HTTP PUT request:
    client.print(F("PUT /v2/feeds/"));
    client.print(FEEDID);
    client.println(F(".csv HTTP/1.1"));
    client.println(F("Host: api.cosm.com"));
    client.print(F("X-ApiKey: "));
    client.println(APIKEY);
    client.print(F("User-Agent: "));
    client.println(USERAGENT);
    client.print(F("Content-Length: "));
    client.println(tempStringLength);

    // last pieces of the HTTP PUT request:
    client.println(F("Content-Type: text/csv"));
    client.println(F("Connection: close"));
    client.println();

    // here's the actual content of the PUT request:
    client.println(tempString);
  }else {
    // if you couldn't make a connection:
    Serial.println(F("cosm connection failed"));
    Serial.println();
    Serial.println(F("disconnecting."));
  }
   // note the time that the connection was made or attempted:
  lastConnectionTime = millis();
  Serial.println(F("connection success"));
  Serial.println(F("disconnecting."));
  client.stop();
}
void loop(void)
{
  getOneWire();
  checkEthernet();
  checkButtons();
  displayLCDTemp();
  logData();
}
__________________
TeensyNet Networked Controller
ButtCrack Brewery
And This Is Why We Drink...
jimmayhugh is offline
 
Reply With Quote Quick reply to this message
Reply



Quick Reply
Message:
Options
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Idea for Kegerator temperature monitor FourJ DIY Projects 0 04-26-2012 04:29 AM
Interesting Temperature Monitor with SMS Jeepninja DIY Projects 3 12-07-2011 05:59 PM
Arduino for kegerator temperature control. Orfy DIY Projects 19 04-21-2010 12:18 PM
1-Wire Temperature Monitor Vinic DIY Projects 12 01-12-2010 06:01 PM
Temperature Monitor N5629 DIY Projects 5 03-31-2009 04:54 PM