November 20, 2008

Posting Data to Pachube Using Ruby

This is just a short post to share how I'm pushing the data being recorded by the Mazzini prototype up to Pachube. It's a fairly simple snippet of Ruby and I'll stick it into the extended entry as it's only really of interest to the committed. Click on to read more.

Pachube feeds can be generated in CSV or EEML and either pushed up to Pachube (when you want to update it) or pulled by the Pachube servers (where you stick the file on a publicly-accessible web server and Pachube poll it). I wanted to publish my feed in EEML (because it's more detailed) and also wanted to push the updates out myself as that saved setting up an externally-facing server and meant I can publish updates more frequently than every fifteen minutes.

The Pachube guys have made uploading data for a feed pretty trivial, you just push the CSV or EEML data to the URL for the feed with an HTTP PUT request.

You'll need to know the URL for your feed (for example /api/12345.xml) and your Pachube API key, plus have your sensor data ready to upload. Generating the data was one of the reasons I ended up with this script in Ruby - I feed the data from the Arduino into a Ruby on Rails app, and use it to generate the EEML as a simple XML document.

That lets me pull the data out in the middle of a rake task with something like this:

app =
app.get(app.url_for(:controller => 'reporter', :action => 'latest'))
data_to_send = app.html_document.root.to_s

That results in the EEML data being stored as a string in the variable data_to_send.

You'll need to add require 'net/http' to pull in the HTTP library, and then you can publish data to Pachube with this code, replacing the relevant parts with your feed path and Pachube developer key.

# And update our feed on Pachube
http ='', 80)
http.start do |http|
req ='/api/12345.xml', {'X-PachubeApiKey' => 'PUT_YOUR_API_KEY_HERE'})
req.body = data_to_send
resp = http.request(req)
puts resp.body
puts "Submitted to Pachube"
# Probably not much we can do, lets hope that the next one works
puts "Problem submitting results to Pachube"
puts "Error reported was:"
puts $!

And that's it. Your data will now be live on Pachube.

Update: When I posted about this to the Green-Web-UK mailing list, James reminded me that I could've used his EEML Ruby gem to generate the EEML data rather than create a whole Rails app.


Posted by Adrian at November 20, 2008 11:54 AM | TrackBack

This blog post is on the personal blog of Adrian McEwen. If you want to explore the site a bit further, it might be worth having a look at the most recent entries or look through the archives or categories over on the left.

You can receive updates whenever a new post is written by subscribing to the recent posts RSS feed or


just to let you know that I wrote the ruby code for reading from pachube.

this is a post with a couple of examples and link to github:

Posted by: Chiaroscuro at January 16, 2009 02:18 AM
Post a comment

Remember personal info?

Note: I'm running the MT-Keystrokes plugin to filter out spam comments, which unfortunately means you have to have Javascript turned on to be able to comment.