#include #include #include #include #include #include "ntp_client.h" # Another personal library ;-) // The device id of a DS18S20 is 0x10 #define DS18S20 0x10 CollectdPacket packet( "Arduino", 30 ); // Init 'CollectdPacket' class with : // client name = Arduino // interval = 30s OneWire ds18s20( 8 ); // Init the OneWire on pin 8 static byte mac[] = { 0x00, 0x04, 0x25 , 0x15, 0x48, 0x12 }; static byte ip[] = { 10, 1, 1, 40 }; static byte collectd_ip[] = { 10, 1, 1, 100 }; static byte ntp_ip[] = { 10, 1, 1, 254 }; // Init the UDP instance EthernetUDP udp_handler; void setup() { Serial.begin( 9600 ); Serial.println( "Initialized a packet for 'Arduino' with interval 30" ); Ethernet.begin( mac, ip ); udp_handler.begin( 8888 ); // Start an UDP instance listening on port 8888 } void loop() { float temperature; byte ds_addr[8]; byte ds_buffer[12]; char ds_idstr[17]; // 16 chars + \0 // Search for the Dallas sensor until found while ( ! ds18s20.search( ds_addr ) ) { ds18s20.reset_search(); delay( 250 ); } // Not so beautiful but just for DEBUGING purpose... 8-) sprintf( ds_idstr, "%02x%02x%02x%02x%02x%02x%02x%02x", ds_addr[0], ds_addr[1], ds_addr[2], ds_addr[3], ds_addr[4], ds_addr[5], ds_addr[6], ds_addr[7] ); Serial.print( "Found sensor id : " ); Serial.println( ds_idstr ); if ( ds_addr[0] != DS18S20 ) { Serial.println( "Found sensor is not a DS18S20, skipping" ); return; } if ( OneWire::crc8( ds_addr, 7 ) != ds_addr[7] ) { Serial.println("CRC is not valid, skipping..."); return; } // Send a mesure command to the sensor ds18s20.reset(); ds18s20.select( ds_addr ); ds18s20.write( 0x44, 1 ); // Wait for conversion to finish (~750ms) // There is no reason to wait it, but as this program is an example // we can proceed that way. delay( 800 ); // Retrieve mesured data ds18s20.reset(); ds18s20.select( ds_addr ); ds18s20.write( 0xBE ); // Read the scratchpad ds18s20.read_bytes( ds_buffer, 9 ); unsigned int raw = ( ds_buffer[1] << 8 ) | ds_buffer[0]; raw = raw << 3; // 9 bit resolution default if ( ds_buffer[7] == 0x10 ) { // count remain gives full 12 bit resolution raw = ( raw & 0xFFF0 ) + 12 - ds_buffer[6]; } temperature = (float) raw / 16.0; Serial.print( "Sensor temperature : " ); Serial.println( temperature ); Serial.println( "Resetting packet..." ); packet.resetPacket(); packet.addTimestampHR( GetTimeFromNTPServer( ntp_ip, 5 ) ); packet.addPlugin( "sensors" ); packet.addPluginInstance( "ds18s20" ); // Could be 'ds_idstr' if you want... packet.addType( "temperature" ); packet.addValue( VALUE_TYPE_GAUGE, temperature ); Serial.print( "Generated a packet of " ); Serial.print( packet.getPacketSize() ); Serial.println( " bytes" ); Serial.print( "Sending packet to 10.1.1." ); Serial.println( collectd_ip[3] ); udp_handler.beginPacket( collectd_ip, 25826 ); udp_handler.write( packet.getPacketBuffer(), packet.getPacketSize() ); udp_handler.endPacket(); delay( 29 * 1000 ); // Sleep 29 seconds (30s - 800ms... ;)) }