2012/03/08

Pachube on Adafruit's IoT Printer

I've been meaning to check out pachube for a while now and it seemed like fiddling with Adafruit's Internet of Things Printer was a perfect chance.  The JSON parsing code from the Gutenbird sketch lent itself nicely to tweaking for the pachube datastream JSON feed.  Pachube was also very easy to get up and running with between the developer documentation, easy API key generation, and each feed page having buttons to see the various formats (XML, CSV,  JSON).  The end result is that I can get a print out of air quality data for Boston:
(https://pachube.com/feeds/23716 - "Air Quality Index and pollutant data for Boston, Massachusetts webscraped from http://www.airnow.gov/. AIRNow observational data are not fully verified or validated; these data are subject to change and should be considered preliminary. If observational data are used for analyses, displayed on web pages, or used for other programs or products, the analysis results, displays, or products must indicate that these data are preliminary.")

It's still definitely a work in progress - I'd like to format the dates a bit and there seems to be an intermittent bug with the fetch/parse process.  Nonetheless, I've put the code up on github for anyone interested in playing around with it.  There are a couple section of code that I think are a bit interesting.

Adding the pachube API key is as simple as setting one additional header in the HTTP request.
The pachube feed does contain a last updated field for the feed; unfortunately, it is the last field in the feed.  I wanted to stick with (for lack of a better term) the SAX style parsing of the JSON that the original Gutenbird sketch uses rather than do a DOM style load of the data.  Each entry does have a timestamp, but I was looking for something that would tell me when the feed as a whole had been updated.

I ended up looking at the HTTP response header for Last-Modified (I'm considering whether to refactor the code to just store a hash of the value since I'm only using it to decided whether to print the feed or not).  If the value has changed, I save it and return true to indicate the feed should be printed.
Looking through the pachube API feed, the structuring of the interesting data is close enough to the twitter feed that reusing the Gutenbird parsing code made a lot of sense to me.  As an aside, Jenkins build feeds seem to follow this same "object which contains a single array of interesting shallow objects" (JSON-SAISO anyone? no?):
It took me a couple reads to understand the original code from Gutenbird; but, once I grokked it, I think it's a really elegant approach.  It's looking in the JSON for a given name, which will have an array as the value.  When in each object, it looks for specific names and saves the values. Finally, at the end of each object, it handles the data for that object.

I tweaked it as below for the name/value pairs I'm interested in (and I'm using a callback function for printing).  Again, very few modifications were needed to switch from parsing twitter to parsing pachube.
I'm curious to see what data feeds other IoT owners will find interesting.  I'm guessing that as time passes, there'll be more and more sketches available to track different data.

[Updated]
Welcome Adafruit and Flickr readers - here's a little bonus content :)

The same feed as above, but with swanky outlining:

And the relevant code changes to printEntryToPrinter :


And setup :

No comments:

Post a Comment