Monday, 10 September 2012

Raspberry Pi as a Cosm data server

Part of concept I'm working up uses a base station to act as the bridge between a suite of sensors and the internet. Cosm is a web service that manages data upload, storage and retrieval and enables sharing of data streams.

Getting data off the Pi and into Cosm is reasonably trivial, but it took me a few tries to get it working, but here's the Cosm datastream. The Pi is hooked up to a BMP085, as described previously. In the end I bought an Adafruit cobbler for the convenience of hooking up to a breadboard and popped the Pi in a crust of laser-cut clear acrylic.

The code used the python BMP085 example from Adafruit. To upload to Cosm, I found a small library by Josh Heling that generates the JSON data structure and uploads to your datastream on Cosm. I did have to get the mechanize library for python, but that was a simple as downloading the latest zipped source and running the installer script... Cosm itself recommends using EEML libraries but I couldn't install them properly, possibly due to the /temp RAM drive being set too small on my Pi...

Another thing to note is that Cosm controls access to your streams using 'API keys'. These keys can be set on a per-datastream basis or one for each data feed. They let you control read, write, create and delete. Make sure you get the settings right as I got stuck with '403 forbidden' HTML errors that I was trying to debug from my code, but in the end the cause was a write-locked API key :-/

Anyhow, here's the code:


from Adafruit_BMP085 import BMP085
from pachube import PachubeFeedUpdate
#import eeml

# ===========================================================================
# Example Code
# ===========================================================================

# Initialise the BMP085 and use STANDARD mode (default value)
# bmp = BMP085(0x77, debug=True)
bmp = BMP085(0x77)

# To specify a different operating mode, uncomment one of the following:
# bmp = BMP085(0x77, 0)  # ULTRALOWPOWER Mode
# bmp = BMP085(0x77, 1)  # STANDARD Mode
# bmp = BMP085(0x77, 2)  # HIRES Mode
# bmp = BMP085(0x77, 3)  # ULTRAHIRES Mode

temp = bmp.readTemperature()
pressure = bmp.readPressure()
altitude = bmp.readAltitude()

print "Temperature: %.2f C" % temp
print "Pressure:    %.2f hPa" % (pressure / 100.0)
print "Altitude:    %.2f" % altitude

API_KEY = 'API key goes in here'
FEED = '73642'

API_URL = '/v2/feeds/1234.xml' .format(feednum = FEED)

pfu = PachubeFeedUpdate(FEED,API_KEY)
# do some stuff; gather data, repeating as necessary for any number of datastreams

# finish up and submit the data


  1. In the Raspberry Pi as a Cosm data server example for the BMP085 shown on the website, will that program work to send to COSM without modification? what other libraries do I need to make it work?

    1. Hi Tom,

      The libraries are linked as above - you might need to have the occidentalis distro of Linux (from Adafruit) to use the I2C bus on the Raspberry Pi.

  2. I am a little confused, you mentioned in the text that you couldn't load EEML files than in the program it calls for EEML?
    You mentioned that you used mechanize and JSON but didn't notice that in the program.Can you clarify, btw, I am using Wheezy OS.

    1. Tom,

      Yeah, the EEML library wouldn't load on my 'pi, probably due to virtual drive space and magic linux-y things to do with temp drives etc... :-/

      The EEML library is commented out in the code... the lib that does the work is from Josh Heling at:

      That code uses mechanise (oops mechanize) which I could get working on my Pi. I'm not sure what's built into Wheezy in terms of I2C support, so you may wish to try occidentalis from Adafruit:

      This build includes I2C and SPI amongst other useful hardware interfacing stuff...

      Hope this helps!