#include <OneWire.h>
#include <SPI.h>
#include <Ethernet.h>
OneWire ds(9); // (a 4.7K resistor is necessary)
// KNOWN ROMs (IDs of Thermistors) //
const char* therm1 = "28ff988b8141cd"; const char* therm2 = "28ffac6d68142bc"; const char* therm3 = "28ffe6746814243"; const char* therm4 = "28ffd96f681424";
const char* therm5 = "28ff734b814175"; const char* therm6 = "28ff7bb66814378"; const char* therm7 = "28ff8758691438"; const char* therm8 = "28ff57bb81418";
const char* therms[] = {therm1, therm2, therm3, therm4, therm5, therm6, therm7, therm8};
char rel1; char rel2; char rel3; char rel4; char rel5; char rel6; char rel7; char rel8;
char rels[] = {rel1, rel2, rel3, rel4, rel5, rel6, rel7, rel8};
int o; int z;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 20);
// Initialize the Ethernet server library with the IP address and port you want to use (port 80 is default for HTTP):
EthernetServer server(80);
// RELAY LOGIC & TEMPERATURE
char on = LOW; char off = HIGH;
int celmax = 25.00;
void setup() {
pinMode(32, OUTPUT); pinMode(33, OUTPUT); pinMode(34, OUTPUT); pinMode(35, OUTPUT);
pinMode(36, OUTPUT); pinMode(37, OUTPUT); pinMode(38, OUTPUT); pinMode(39, OUTPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
// Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
byte i; byte present = 0; byte type_s; byte data[12]; byte addr[8]; float celsius;
String aa; String ab; String xx; String check;
// 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();
Serial.write(c);
// if you've gotten to the end of the line (newline character) and the line is blank, the http request has ended, so you reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 10"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html><head><style>.bold {font-weight: bold;}</style></head>");
client.println("<table border=1><tr class=bold><td>#</td><td>Chip</td><td>Data</td><td>CRC</td><td>Temperature</td><td>ROM</td><td>State</td></tr>");
for (o = 0; o < 8; o++) {
aa = "<tr><td>T";
aa += o;
aa += "</td><td>";
if ( !ds.search(addr)) {
// Serial.println("No more addresses.");
// Serial.println();
ds.reset_search(); delay(0); return; }
if (OneWire::crc8(addr, 7) != addr[7]) { aa += ("CRC is not valid!"); return; }
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
aa += "DS18S20"; type_s = 1; break; // or old DS1820
case 0x28:
aa += "DS18B20"; type_s = 0; break;
case 0x22:
aa += "DS1822"; type_s = 0; break;
default:
aa += "Device is not a DS18x20 family device"; return;
}
aa += "</td><td>";
ds.reset();
ds.select(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(addr);
ds.write(0xBE); // Read Scratchpad
aa += String(present, HEX);
// aa += "</td><td>";
//Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
aa += String(data[i], HEX);
// Serial.print(" ");
}
// aa += ", CRC = ";
aa += "</td><td>";
aa += String(OneWire::crc8(data, 8), HEX);
aa += "</td><td>";
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
// aa += (", Temperature = ");
aa += (celsius);
aa += "</td><td>";
delay(0);
check = "";
// aa += "ROM = ";
for( i = 0; i < 8; i++) {
check += String(addr[i], HEX);
}
aa += check;
aa += "</td><td>";
if (check == therms[o]) { if (celsius >= celmax) { rels[o] = on; aa += "on"; } else { rels[o] = off; aa += "off"; } }
digitalWrite(o+32, rels[o]);
client.print(aa);
client.println("</td></tr>");
}
client.println("</table></html>");
break;
}
if (c == '\n') { currentLineIsBlank = true; } else if (c != '\r') { currentLineIsBlank = false; }
}
}
delay(1);
client.stop();
}
}
Comments