This instructable will explain how to:
* connect a DS18B20 temperature sensor to an arduino
* read from the sensor using the 1-wire interface
* send that data to xively.com directly from the arduino
* view the resulting graph on the internet

http://xively.com is an Internet of Things host that generates graphs based on data you provide. In this example I am going to use it to monitor the temperature in my room. It's not a hackerspace, my wife keeps it far too tidy for that.

Step 1: Parts List

* Arduino with ethernet shield
I have an arduino Uno clone and a wiznet 5100 shield.
* Power supply (it can be powered via the USB port)
I was fortunate that my router has USB ports that provide the 5v output meaning the arduino  is powered whenever the router is and saves a plug, electricity, the environment and the  future of the world.
* DS18B20 temperature sensor
* RJ45 network cable connected between the arduino and your router

Step 2: Schematic

* Arduino gnd to DS18B20 both outer legs
* Arduino 5v to 4.7k resistor leg 1
* 4.7k resistor leg 2 to DS18B20 middle leg
* Arduino digital pin 2 to DS18B20 middle leg
* RJ45 cable from Arduino to router
* Power to Arduino (via USB or barrel jack, I use USB from my router)

Step 3: Prerequisites

Three libraries are required for the arduino:
onewire library from http://www.pjrc.com/teensy/td_libs_OneWire.html (specifically http://www.pjrc.com/teensy/arduino_libraries/OneWire.zip)
httpclient library from https://github.com/amcewen/HttpClient (https://github.com/amcewen/HttpClient/archive/master.zip)
Xively library from https://github.com/xively/xively_arduino (https://github.com/xively/xively_arduino/archive/master.zip)
install these libraries in your arduino/library directory.

An account with http://xively.com (formerly pachube.com and cosm.com)
Sign up for a free developer account at https://xively.com/get_started/
Pick a username, password, set your address and timezone etc. You will receive a confirmation email, click the activation link to avtivate your account.
You can choose to take the test drive to learn about xively or skip it, this is completely up to you.
Doing the test drive? I'll wait
- \ | / - \ | /
Done? Let's continue

Step 4: Add a New Device to Xively

You should now be on the Development Devices page of xively. This is where the fun starts.

Click on +Add Device

Give the new device a name eg Arduino DS18B20 temperature logger

Fill in a description about the device, its location such as My Hackerspace, and yourself if you like. I tend to add a link to my web site here although there is a dedicated web address area later in the instructions.

Choose whether the data is public or private. Do you really want the world knowing how hot your hackerspace is? I choose public unless it is something like an alarm which I keep private.

Click on the Add Device button at the bottom of the page

Step 5: The Xively Interface for Your New Device, and All the Access Codes

You will now be on a page with a lot of information. Don't be overwhelmed – it is mainly set and forget stuff.

The page is broken up into 8 parts:
* product ID, secret, serial number, activation code
* status (Activated), feed ID, feed URL (this shows the data on a graph), API endpoint
* “Add channels to your device!” - the arduino will handle this in the sketch
* request log – this is for debugging and confirming everything is working from the arduino side
* location – the geographical location of the sensor (optional)
* api keys – we will need this later in the arduino sketch
* metadata – can be edited
* triggers – ping a web page when something happens (eg temperature drops below freezing)

Step 6: Arduino Sketch

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
#include <OneWire.h>

// 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";
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
OneWire ds(sensorPin);  // on pin 2 (a 4.7K resistor is necessary)
5v - 4.7k resistor - 18B20 middle pin - D2
gnd - 18B20 both legs (joined together)

void setup() {
  // put your setup code here, to run once:
  Serial.println("Starting single datastream upload to Xively...");

  while (Ethernet.begin(mac) != 1)
    Serial.println("Error getting IP address via DHCP, trying again...");
  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);

void loop() {
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
int i;

if ( !ds.search(addr))
  Serial.println("No more addresses.");
if (OneWire::crc8(addr, 7) != addr[7])
  Serial.print("CRC is not valid!");
// the first ROM byte indicates which chip
switch (addr[0])
  case 0x10:
   Serial.print("  Chip = DS18S20");  // or old DS1820
   type_s = 1;
  case 0x28:
   Serial.print("  Chip = DS18B20");
   type_s = 0;
  case 0x22:
   Serial.print("  Chip = DS1822");
   type_s = 0;
   Serial.println("Device is not a DS18x20 family device.");
ds.write(0x44, 1);        // start conversion, with parasite power on at the end

delay(1000);     // 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.write(0xBE);         // Read Scratchpad

for ( i = 0; i < 9; i++) {           // we need 9 bytes
    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];
  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;
        Serial.print("Current temperature: ");
        Serial.println(" celsius");
  Serial.println("Uploading it to Xively");
  int ret = xivelyclient.put(feed, xivelyKey);
  Serial.print("xivelyclient.put returned ");


Step 7: Welcome to the Internet of Things!

Once you have uploaded the code to the Arduino you should start to see information appear in the Request Log section and datapoints appear on the graph.

Congratulations, you are now updating the world with the temperature in your hackerspace :)

What's next? That's up to you.

How many times of day does your front door open?
What is the water level of your begonias?
Is your washing machine finished?
Is the coffee machine empty?
How loud are the kids playing music while you are out?

There are millions of possibilities for monitoring, just take a look around you. Does it do something? Then there must be a way to monitor it.

Here for the world to see is the temperature in my hackerspace<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs> living room:

Currently it is 26.5 degrees C, and it is supposed to be autumn.

Step 8:

I have not tested this code yet (but it does compile at least!)

This version should create temp_nnnnnnnnnnnn for each individual DS sensor and upload the data to xively.

Hopefully it will make the code multiple sensor friendly.

I apologise, I forgot to do the actual reading of the chip in the above code :P <br> <br>You need to add the following <br> <br>for ( i = 0; i &lt; 9; i++) { // we need 9 bytes <br> data[i] = ds.read(); <br> <br>after <br> <br>ds.write(0xBE); // Read Scratchpad <br> <br>
<p>Hi there..</p><p>Is it still possible to use xively ?</p>
<p>Hi. is there a way to give their own names sensors (data streams)? Eg. Kitchen, living room, etc. I have 6 sensors.</p>
<p>Hi<br>The lines to change are<br>&lt;code&gt;// Define the strings for our datastream IDs</p><p>char sensorId[] = &quot;temp&quot;;&lt;/code&gt;<br>specifically, change &quot;temp&quot; to each of your device names eg &quot;garage temp&quot;</p>
<p>I need to call each of the sensors connected to a single Arduino otherwise. Eg. Kitchen, garage, lounge.</p>
<p>I have been thinking about this for sensor 1 = kitchen, sensor 2 = living room and so on. Where to enter the name of the second third and subsequent sensors? Everyone must have a different name.</p>
<p>This setup will prevent you from using the capabilities of this perfectly designed IC. That is the adressing multiple IC's (temperature readouts) on the same line. </p>
<p>I want to make circuit b using 4 sensors and 4 single resistor sensor connections print.4 sensor enough for my seven-inch screen. Is there another Arduino, or need</p>
<p>Hi. if someone was able to run it with multiple sensors (5)?</p>
<p>for multiple sensors in the end of the code after Serial.println(&quot;Uploading it to Xively&quot;); </p><p>replace </p><p>datastreams[0] = { <br> XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT), <br> };</p><p>with</p><p>XivelyDatastream datastreams[0] = XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT);</p><p>Worked for me, hope that helps.</p>
<p>slightly corrected and in my code runs on multiple sensors.</p><p>I just have a problem with the naming of streams. What I mean is that the individual data struienia zwoimi call names and not the serial numbers of sensors.</p>
<p>silver111,</p><p>May I ask what you did to get multiple sensors working? I'd like to add additional temp probes but haven't been having much luck.</p>
<p>Error:</p><p>Serial.println(&quot;Uploading it to Xively&quot;);<br> datastreams[0] = {<br> XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),<br> };</p><ul><li>------------------------</ul>sketch_feb13d.ino: In function 'void loop()':<br><p>sketch_feb13d:178: error: expected primary-expression before '{' token<br>sketch_feb13d:178: error: expected `;' before '{' token</p>
<p>Hi. if you were able to run the project with multiple sensors?</p>
There is a missing after the , in that line. I will have a look shortly and get b back to you.
<p>Error,</p><p>Please give solution...</p><p>IP address:</p><p> Chip = DS18B20</p><p>Current temperature: 33.13 celsius</p><p>Uploading it to Xively</p><p>xivelyclient.put returned -3</p>
<p>Connection and setup done but not working...</p>
<p>Greg; Hi. As with rparkera and sidbin below, I have the same problem with the multiple sources sketch that they have.</p><p>I get the following errors:</p><p>Multiple_Xively.ino: In function 'void loop()':</p><p>Multiple_Xively:179: error: expected primary-expression before '{' token</p><p>Multiple_Xively:179: error: expected `;' before '{' token</p><p>Multiple_Xively:188: error: expected `}' at end of input</p><p>You were going to get back to sidbin, but don't seem to have done so.</p><p>And for the record, I have only just started using Arduino as it's the most practical solution (I think) to a long running temperature monitoring problem I have.</p><p>Thanks...!</p>
<p>Hi there - great instructable. Many thanks </p><p>Did you ever get back on sidbin's query? I have exactly the same problem. The single datastream works fine - but I can't get the multiple sources working. Help!</p>
<p>Some other example to use for xively reporting - use it to track URL performance..</p><p><a href="http://linuxadvices.blogspot.ru/2014/02/log-url-performance-in-xively.html" rel="nofollow">http://linuxadvices.blogspot.ru/2014/02/log-url-performance-in-xively.html</a></p>
<p>Works for me thank you to share it</p>
<p>here is my feed:</p><p><a href="https://xively.com/feeds/163946092" rel="nofollow">https://xively.com/feeds/163946092</a></p><p>But I don't think it's public yet. Can you help me with that?</p>
<p>It works for me :)</p>
hi i have followed your instructable and it works great! <br> <br>im am thinking of adding some xtra ds18b20 sensors and wonder how to adress them any hints? <br> <br>thanks <br> <br>Nico <br>
Just plug them in in parallel. The code will read them in order. <br>To update more than one datastream, look at the dataStreams[] parts of the code. <br>
<p>Thanks for the tutorial. I tried it and its working great with 1 sensor, but i cant see how to get a second or more sensors working. Any chance you could elaborate on the above a little more? Thanks in advance...</p>
Hi guys I need your help, <br> <br>I've followed all the steps in this great tutorial but it doesn't work for me. <br> <br>I keep on getting &quot;No more addresses&quot; in the serial monitor and xively is not receiving any datapoints. <br> <br>What does this mean and how could it be solved? <br> <br>It would be really great if one of you could help me
Hey guys I have a problem. <br>I would like to add more sensors (DS18B20) and it fails. <br> <br>Could you help me i am a beginner. <br> <br>Thank you in advance for your help <br> <br>standa
Hi, <br>Thanks for your intructable ! :-) <br>It worked perfectly for 9 minutes, but now it's not sending anything&hellip; <br>What is the problem ?
Hi Zenius :) <br> <br>Are you powering the Arduino from a steady power supply? <br>Have you tried resetting the Arduino? <br>Mine has been running pretty much continuously since it was set up with no issues apart from when the network cable became loose a couple of times. I think it has also only been turned off a couple of times too when I was sorting out round the router. <br> <br>Get back to me :)
Hi there! Im trying to use your xively code but Im getting an &quot;i&quot; was not declared in this scope error. Would you know why this might be ? Im a bit new to arduino
Fixed in the code.<br><br>I missed the line<br>int i;<br>after<br>float celcius;<br><br>I apologise for the oversight.
Thx a lot. Now it works perfect
Glad I could help :)<br><br>What's your xively link so I can see it working? :)
This is my link. https://xively.com/feeds/1172808344 Tried to get a second temp read working but luck so far
I have everything wired up the same way as in your sketch except I don't seem to be getting any data from the sensor. When i use the example one wire sketch specific to the sensor it has no problems. any ideas?
I apologise, I forgot to do the actual reading of the chip in the above code :P <br> <br>You need to add the following <br> <br>for ( i = 0; i &lt; 9; i++) { // we need 9 bytes <br> data[i] = ds.read(); <br> <br>after <br> <br>ds.write(0xBE); // Read Scratchpad <br> <br>
Please help, everything is fine just unable to get data from 18B20 Sensor it show IP address: <br>Chip = DS18B20 <br>Current temperature: 0.00 celsius <br>Uploading it to Xively <br>xivelyclient.put returned 200 <br>i have double check the connection is correct. <br>
I apologise, I forgot to do the actual reading of the chip in the above code :P <br> <br>You need to add the following <br> <br>for ( i = 0; i &lt; 9; i++) { // we need 9 bytes <br> data[i] = ds.read(); <br> <br>after <br> <br>ds.write(0xBE); // Read Scratchpad <br> <br>

About This Instructable




Bio: Focussing currently on supplying open source software to the masses. I supply linux CDs and DVDs across Europe, as well as mirror various open source ... More »
More by gregoryfenton:LinkIt One Tutorials - #14 Traffic lights LinkIt One Tutorial #13 - Send a tweet LinkIt One tutorials - #0 Fix that pesky Serial port! 
Add instructable to: