/* DS1820 - reads DS1820 sensor and put it onto telnet Copyright (C) 2009 Jonas Genannt This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . */ #include #include #include #include #define CONVERT 0x44 #define READSCRATCH 0xBE // The DS1820 is connected on pin 3 OneWire ds(3); Server server = Server(23); static uint8_t mac[16] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; static uint8_t ip[4] = { 192, 168, 1, 3 }; static uint8_t gw[4] = { 192, 168, 1, 1 }; static uint8_t nm[4] = { 255, 255, 255, 0 }; int address_index=0; byte address_storage[64]; void setup() { Serial.begin(9600); Ethernet.begin(mac,ip,gw,nm); server.begin(); byte addr[8]; while(1) { if( ! ds.search(addr)) { ds.reset_search(); break; } else { if ( OneWire::crc8( addr, 7) != addr[7] && addr[0] != 0x10) // CRC Check or is not an DS1820 { Serial.print("CRC is not valid or device is not an DS1820!\n"); } else { Serial.print("Found:"); for (int i =0; i< 8; i++) { address_index++; Serial.print(addr[i], HEX); Serial.print(":"); address_storage[address_index]= addr[i]; } Serial.println(""); } } delay(10); } Serial.print("We got "); Serial.print((int) (address_index/8) ); Serial.print(" sensors\n"); } void getSensorData(char *buffer) { int index = 1; Serial.println("In function getSensorData()"); for (int i=1; i <= (address_index/7); i++) { byte addr[8]; Serial.print("Sensor: "); Serial.println(i); strcat(buffer, "Sensor:"); sprintf(buffer, "%s %x",buffer, i); strcat(buffer, ": "); for (int e =0; e< 8; e++) { addr[e] = address_storage[index]; index++; } Serial.println("Nach address_storage -> adresse"); for (int r=0; r<8; r++) { char address[10]; sprintf(address, "%x", addr[r]); strcat(buffer, address); Serial.print(addr[r],HEX); } Serial.println("Nach message_data.append(adresse zu hexa; "); strcat(buffer, " : "); byte present = 0; byte data[12]; // OK we have sensor in addr we read it out! ds.reset(); ds.select(addr); ds.write(CONVERT,1); delay(1000); //delay(750); present = ds.reset(); // Reset device ds.select(addr); // Select device ds.write(READSCRATCH); // Read Scratchpad for (int b = 0; b < 9; b++) { // get 9 bytes from sensor data[b] = ds.read(); } if(OneWire::crc8( data, 8) == data[8]) // Check CRC is valid { // CRC is ok float read_temp=((data[1]<<8) | data[0]) >> 1 ; // Divide the temperature by 2 float temp_count=float(data[7] - data[6])/(float)data[7]; // Convert to real temperature float real_temp = ((float)read_temp-0.25)+temp_count; char buff[12]; tempToAscii(real_temp, buff); Serial.println(buff); strcat(buffer, buff); } else { Serial.print("Error on CRC8"); strcat(buffer, "Cound not CRC8 data"); } strcat(buffer, "\n"); } } void loop() { Client client = server.available(); if (client) { Serial.println("Client has connected"); char buffer[412]=""; getSensorData(buffer); server.write(buffer); client.stop(); } } void tempToAscii(double temp, char *buff) { int frac; frac=(unsigned int)(temp*1000)%1000; itoa((int)temp,buff,10); strcat(buff,"."); itoa(frac,&buff[strlen(buff)],10); //put the frac after the deciaml }