Live Temperature Logging on Pachube with a Xino, XBBO, XV & DS18B20

 

One of first goals for many people wanting to get into the "Internet Of Things" is to be able to log simple data and save it somewhere. This example reads a temperature and publishes it to a website that can save and display your data, it's called Pachube.com. The equipment detailed is connected to the internet using a bog standard WiFi network, like the one's many of us have at home or work.

Here is a step by step guide to acheiving this for under £40.00 

 

You will need;

 

 

Step 1 Build your hardware

Both the Xino and XBBO are supplied in kit form.  They use through-hole components, making them suitable for anyone with even moderate soldering skills.  For instructions on either part, see the following articles;

Next you will need to connect the Dallas DS18B20 temperature sensor.  The easiest way is to mount it on the prototyping area at the top of the Xino board as show in Andrew Lindsays XINO Basic for Atmel example 1 - Measuring Temperature article.  You may also want to consider using a length of 3 core wire to the sensor so that it can be mounted remotely from the Xino.

  • Pin 1 of the DS18B20 connects to ground
  • Pin 2 of the DS18B20 connects to Digital Input 06 on the Xino (Note that the example listed above shows 03, but we will be using 03 as a software serial port for monitoring)
  • Pin 3 of the DS18B20 connects to 5V
  • A 4.7K resistor connects Digital Input 06 and 5V

 

 

Step 2 Set up your Pachube account

If you don't alreayd have a Pachube account you will need to sign up for one.  Pachube accounts are free, and you now get unlimited datastreams, datapoints and history.  Go to https://pachube.com/signup to sign up, and then click the link in the confirmation email.

Whilst the master API key you are assigned will allow you to upload data, it is advisable to create an access key with only the PUT permission.  To do this, go to the 'My Keys' link, and click the 'create a new API key' link.  You will need to give it PUT permissions, but you may also want to restrict what IP addresses can use the key, restrict it to private feeds or set up an expirary date.  Once you've created the key, click on 'show key' to reveal a 43 digit key.  Copy this to notepad or somewhere, you'll need it later.

Next, create a feed.  Give it a title, description and any other info you wish.  Here you can also select if the feed is to be public or private.  Add two data streams.  One with an ID of 0, which will be the uptime count, and one with an ID of 1 which will be temperature in degrees C.  This will now appear in your feeds section.  Note that is creates a unique 5 digit feed ID that you will need to copy for use later.

 

Step 3 Program your Xino

I will make the assumption here that you already have the Arduino IDE installed, and you are reasonably familiar with programming Arduinos.  If not, take a look at http://www.arduino.cc/ in particular the Download and the Getting Started section.

If you haven't got tem already, you will need the following libraries installed;

Create a script called Pachube_Temperature_Logger with the following code;

 
// WiFly Pachube Client

// Send data to a Pachube Feed

// (Based on Ethernet's WebClient Example)

// (based upon Sparkfun WiFly Web Client example)

// Sparkfun WiFly library updated and can be found here

// https://github.com/jcrouchley/WiFly-Shield

// Built using Arduino IDE V0.22
#include "WiFly.h"
// Wifly RN-XV (XBee shaped) module connected

//  WiFly Tx to pin 0 (Arduino Rx)

//  WiFly Rx to pin 1 (Arduino Tx)
// using NewSoftSerial V11 beta

// downloaded from here http://arduiniana.org/2011/01/newsoftserial-11-beta/

// this will be included as Software Serial in Arduino IDE 1.0

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
#include <DallasTemperature.h>

#include <OneWire.h>

OneWire oneWire(6);  //DS18B20 connected to pin 6

DallasTemperature sensors(&oneWire);
// Edit credentials.h to provide your own credentials

#include "Credentials.h"
// Using Pachube API V2

Client client("api.pachube.com", 80);
void setup() {
  pinMode(8,OUTPUT);    // power up the XBee socket

  digitalWrite(8,HIGH);

  // lots of time for the WiFly to start up and also in case I need to stop the transmit

  delay(10000);
  Serial.begin(9600);

  mySerial.begin(9600);

  sensors.begin();         // scan for DS18B20 temperature sensors
  WiFly.setUart(&Serial); // Tell the WiFly library that we are not using the SPIUart

  mySerial.println("Wifly begin");

  WiFly.begin();    // startup the WiFly
  mySerial.println("Wifly join");
  // Join the WiFi network

  if (!WiFly.join(ssid, passphrase, WEP_MODE)) {

    mySerial.println("Association failed.");

    while (1) {

      // Hang on failure.

    }

  } 

}
uint32_t timeLastUpdated;

int i;

char buff[64];
void loop() {

  if (millis() - timeLastUpdated > TIMETOUPDATE)

  {  // time for the next update

    timeLastUpdated = millis();
    // prepare the data to send

    // format (API V2)

    // multiple lines each with <datastreamID>,<datastreamValue>

    // feedID can be the datastream name of the numberic ID
    sensors.requestTemperatures();           //Get temperature from 18B20

    long tempC = sensors.getTempCByIndex(0); //First 18B20 is index (0)

    sprintf(buff,"0,%d\n1,%d",i++,tempC);

    mySerial.println("connecting...");

    if (client.connect()) {

      mySerial.println("connected");

      client.print("PUT /v2/feeds/");  // APIV2

      client.print(PACHUBEFEED);

      client.println(".csv HTTP/1.1");

      client.println("Host: api.pachube.com");

      client.print("X-PachubeApiKey: ");

      client.println(APIKEY);
      client.println("User-Agent: Arduino (WiFly RN-XV)");

      client.print("Content-Type: text/csv\nContent-Length: ");

      client.println(strlen(buff));

      client.println("Connection: close");

      client.println();
      client.print(buff);

      client.println();
    } else {

      mySerial.println("connection failed");

    }
    delay(2000);

    while (client.available()) {

      // TODO verify success (HTTP/1.1 200 OK)

      mySerial.print(client.read());  // display the result

    }

    mySerial.println();
    if (client.connected()) {

      mySerial.println("disconnecting.");

      client.stop();

      mySerial.println("disconnected.");

    }

  }

}
 

Create another script called credentials.h with the following code.  You will need to supply your Feed ID and API key (as noted earlier), and your wifi credentials.

 

 


#ifndef __CREDENTIALS_H__

#define __CREDENTIALS_H__
// supply your own Pachube feed ID

#define PACHUBEFEED "yourFEEDIDwillgohere"

// supply your own API Key

#define APIKEY "yourAPIKEYwillgohere"

#define TIMETOUPDATE 15000  // frequency of update - every 15 seconds
// Wifi parameters

char passphrase[] = "yourWIFIKEYwillgohere";

char ssid[] = "yourSSIDwillgohere";

boolean mode = WEP_MODE; //or WPA_MODE
#endif


Compile Pachube_Temperature_Logger and upload it to the Xino.

 

Unplug the Xino, put the XV in to the XBBO, plug the XBBO in to the Xino and apply power

 

Step 4 Sit back and relax

 

Have a look at your feed on Pachube.  The view initially defaults to 24 hours, so if you have only just turned it on, you might only see a single dot for the uptime and temperature data streams.  Change this to 1 hour to zoom in.  You should see stream 0 continually rise from zero.  (It will fall back to zero every time the Xino is restarted).  Feed 1 should display your temperature.  To see that this is working, place it somewhere warm, or somewhere cold, or breath heavily on it and you will see the graph change accordingly.