Intro to Analyzing data from Cosm with Python (ex Pachube)

 

Intro

We have been playing a lot with Nanodes/Sensors/Cosm(Ex Pachube), but the way to perfection, testing and constantly improving  make it almost never available online. So I just plugged a Nanode with a thermistor, and very simple code and let it to feed my Cosm account for a few days.

You can see the feed here: https://cosm.com/feeds/37273

 

Proposal

Cosm easily gives you graphs, and a web page to share with your pals, and see another COSM users, even some plugins for your blog (check my example here). After some days probably you would like some more things. Thats when you may start learning how the API works, here is our experience.

I asked to Leandro to advise me in how to do more tricks with the data, he is an amazing programmer and super kind to offer me a Python intro, so our first place to check was the COSM API documentation, here.

The target was to do some basic statistics, but we were limited on time and we managed (I said we, but was him teaching Python and learning how the Cosm API works and me trying to support and learn) to obtain the last 1000 datapoints from the datastreams in a very hardcored way.

So the proposal is that maybe with this litle example you get interested in playing with the Python/COSM/Nanode .

 

Requirements

What would you need

  • A Cosm account, feed number and API Key.
  • A data source (we use a thermistor which is sending data trough a Nanode).
  • Python
  • Web server if you want to host your python code somewhere.
  • Your system at current time

 

Checking if feed is alive

With this small example we will obtain the Feed Name, check if it is Alive and the last updated date.

#!/usr/bin/python2.6
import urllib
import pprint
key = 'YOURKEY'
feed = 'YOURFEED'
timezone = '+1'
url = 'http://api.cosm.com/v2/feeds/%s.json?key=%s&timezone=%s' % (feed, key,timezone)
f = urllib.urlopen(url)
all_data_as_str = f.read()
d = eval(all_data_as_str)
#pprint.pprint(d)          #uncomment to see a lot
print "*******************************************"
pprint.pprint(d['title'])
if ('live' not in d['status']):
print "Feed not updated at least during the last 15 min"
else:
print "The feed is alive!"
print "Last updated on: %s" %(d['updated'])

And the results would be like this:

*******************************************
‘HackerSpace Brussels temperature monitor and Light’
The feed is alive!
Last updated on: 2012-05-17T00:54:48.659288Z

TODO:

Re obtaining your last datastream’s values, simple data stats

 

 

 

Categories: Arduino, Cosm, Pachube | Leave a comment