#include <Ethernet.h>
#include <SPI.h>
#include <OneWire.h>
// HZSteuerung
OneWire ds(20); // on pin 20 (a 4.7K resistor is necessary)
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 12, 118 };
EthernetClient client;
int HTTPPORT = 80;
String HTTPMETHOD = "GET";
char HOSTNAME[] = "192.168.12.117";
String PATHNAME = "/hzcontroller/ds18.php";
void setup() {
Serial.begin(9600);
Serial.println("Starting ....");
Ethernet.begin(mac, ip);
}
char *ftoa(char *a, double f, int precision)
{
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
char *ret = a;
long heiltal = (long)f;
itoa(heiltal, a, 10);
while (*a != '\0') a++;
*a++ = '.';
long desimal = abs((long)((f - heiltal) * p[precision]));
itoa(desimal, a, 10);
return ret;
}
int HZLoggerLoop() {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
char address[20];
char temperature[20];
if ( !ds.search(addr)) {
//Serial.print("No more addresses.");
ds.reset_search();
return 1;
}
for( i = 0; i < 8; i++) {
sprintf(address+(i*2),"%2.2X",addr[i]);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return 2;
}
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
//Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
//Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
//Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return 2;
}
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
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
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;
ftoa(temperature,celsius,2);
String queryString = "?s=" + String(address) + "&t=" + String(temperature);
if(client.connect(HOSTNAME, HTTPPORT)) {
//if connected make a HTTP request send HTTP header
client.println(HTTPMETHOD + " " + PATHNAME + queryString + " HTTP/1.1");
client.println("Host: " + String(HOSTNAME));
client.println("Connection: close");
client.println(); // end HTTP header
while(client.connected()) {
if(client.available()){
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
}
}
// the server's disconnected, stop the client:
client.stop();
} else {// if not connected:
Serial.println("connection failed");
}
return 0;
}
void loop(void) {
if (HZLoggerLoop() == 1)
delay(60000);
}