#include #include #include #include #include // MAC address for your Ethernet shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Your Xively key to let you upload data char xivelyKey[] = ""; // enter the key, under API Keys unsigned long feedId = 0; // enter your feed ID, under Activated int frequency = 15000; // delay between updates (milliseconds) // Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield) int sensorPin = 2; // Define the strings for our datastream IDs char sensorId[] = "temp_001122334455"; XivelyDatastream datastreams[] = { XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT), }; // Finally, wrap the datastreams into a feed XivelyFeed feed(feedId, datastreams, 1 /* number of datastreams */); EthernetClient client; XivelyClient xivelyclient(client); // initialize the one-wire interface // on pin sensorPin /* 5v - 4.7k resistor - 18B20 middle pin - sensorPin gnd - 18B20 both legs (joined together) */ OneWire ds(sensorPin); void setup() { Serial.begin(9600); while (Ethernet.begin(mac) != 1) { Serial.println("Error getting IP address via DHCP, trying again"); delay(5000); } Serial.print("IP address: "); for (byte thisByte = 0; thisByte < 4; thisByte++) { // print the value of each byte of the IP address: Serial.print(Ethernet.localIP()[thisByte], DEC); if(thisByte < 3) Serial.print("."); } Serial.println(); Serial.println("Initialising datastream upload to Xively.com"); } int howManyDS() { // returns the number of DS18x sensors are connected to the onewire bus int count = 0; byte addr[8]; ds.reset(); while(ds.search(addr)) { if (OneWire::crc8(addr, 7) == addr[7]) { switch(addr[0]) { case 0x10: case 0x22: case 0x28: count++; break; default: break; } } } return(count); } void loop() { byte present = 0; byte type_s; byte data[12]; byte addr[8]; float celsius; int i, sensor; int del = 0 ; ds.reset_search(); while(ds.search(addr)) { i = 0; if (OneWire::crc8(addr, 7) != addr[7]) { Serial.print("CRC is not valid!"); } else { // the first ROM byte indicates which chip switch (addr[0]) { case 0x10: Serial.print("Chip = DS18S20"); // or old DS1820 type_s = 1; i = 1; break; case 0x28: Serial.print("Chip = DS18B20"); type_s = 0; i = 1; break; case 0x22: Serial.print("Chip = DS1822"); type_s = 0; i = 1; break; default: Serial.println("Device is not a DS18x20 family device."); break; } if(i) { // valid chip ds.reset(); ds.select(addr); ds.write(0x44, 1); delay(1000); present = ds.reset(); ds.select(addr); // Read Scratchpad ds.write(0xBE); for ( i = 0; i < 9; i++) { data[i] = ds.read(); } 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; datastreams[0].setFloat(celsius); snprintf(sensorId, sizeof(sensorId), "temp_%02x%02x%02x%02x%02x%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); Serial.print("Current temperature from sensor "); Serial.print(sensorId); Serial.print(": "); Serial.print(celsius); Serial.print(" celsius"); Serial.println("Uploading it to Xively"); datastreams[0] = { XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT), }; int ret = xivelyclient.put(feed, xivelyKey); Serial.print("xivelyclient.put returned "); Serial.println(ret); } } } delay(frequency); }