- Published: Friday, 30 March 2012
- Written by RangerBob
- Hits: 6659
[EDIT 08/06/2012 : Pachube has now changed its name to Cosm, but the Pachube API's listed here still work as is.]
Following on from my previous article here, we have a simple HTTP client for Swordfish. But what use can we make of it, besides browsing the source code of our favourite webpages?
Pachube (pronounced “Patch-Bay”) is an online database service which allows users and developers to upload and download data from their own “Feeds”, or from feeds from other users. Using our simple HTTP client, we can push data from our PIC web platform onto the Pachube servers, and can pull data back at our leisure, either to a PC or other web connected device such as our PIC based platform.
For example, this should be “live” data uploaded from my PICDEM.net 2 board sitting on my desk. It samples the Potentiometer and TC1047 temperature sensor every 30 seconds, and uploads that data to the Pachube servers. Apologies, if this is now offline, I may need that devboard for something else!
We can easily produce Graph image
Or more detailed Interactive visualisations, using Pachube’s built in “Apps” (linked to external page to allow scripts to run).
In order to simplify matters for everyone, I’ve written a Swordfish module which allows for interaction with a small number of routines, rather than handling requests directly with the HTTP client. The following demo code shows the functions in action.
Setting up Pachube and Swordfish:
Get the Swordfish source code here.
You’ll also need the Ethernet stack from here (if you didn’t get it before).
You’ll obviously need a Pachube account in order to use the service. Once you have an account you can create a new feed, or browse other public feeds. In order for our device to Upload or Read Datastreams or Feeds, you’ll need an API key. Click on the “My Keys” link and follow the instructions to get an API key. On the swordfish demo you’ll need to enter the API Key exactly as displayed into the #option API_KEY = “APIKEYHERE“ line.
You’ll also need to create a test feed. Click “Create a feed”, enter the information as needed, then click “Add a datastream”. Give the datastream the ID of “ADC”. Then do this twice more with “Temperature” and “User”. Click save. Once the feed has been created, note the Feed ID either from the URL at the top or the field marked “Website” (you just need the numbers). On the swordfish demo enter that FeedID into the #option FEED_ID = “FEEDIDHERE” line.
Running the demo
Compile the program and load it into your device. You should see the board attempt to get a DHCP connection (see the Wiki page here for more information).
Every 30 seconds (as configured) the board will attempt to read the two onboard sensors. The results for these are stored into the Datastream structures, then UpdateFeed() is called. This routine then takes care of uploading all the Datastream structures to Pachube at once. After a while, UpdateFeed will return a HTTP response code indicating success or otherwise (200 = OK, $FFFF = stack failure/No connection, 401 = not found, etc).
Calling this routine (using ‘a’ on serial communicator), reads the entire selected datastream feed from Pachube. Values are stored in the Datastream structures for the appropriate ID’s only. ReadFeed will return a HTTP response code indicating success or otherwise (200 = OK, $FFFF = stack failure/No connection, 401 = not found, etc).
Calling this routine (using ‘s’ on serial communicator), allows the user to update a single datastream within the feed, rather than all datastreams as with UpdateFeed. UpdateDatastream will return a HTTP response code indicating success or otherwise (200 = OK, $FFFF = stack failure/No connection, 401 = not found, etc).
Calling this routine (using ‘r’ on serial communicator), allows the user to read a single datastream within the feed, rather than all datastreams as with ReadFeed. The result will be stored in the pData string (as a string). UpdateDatastream will return a HTTP response code indicating success or otherwise (200 = OK, $FFFF = stack failure/No connection, 401 = not found, etc).
As demonstrated with the ‘k’ action, we can read any feeds that are set to public. In this example I am using the three datastream structures to hold the return of the “Kilpisjärvi Biological Station Weather Tunnel” feed. Simply set the FeedID variable to the desired Feed ID string, then you can make calls as needed. Be aware your API key should be set to allow reads of public feeds!
The Pachube module as written uses blocking calls. It may be advantageous to modify the calls to non-blocking in future.
The module currently uses the Pachube CSV data structures in its HTTP transactions. These are somewhat sparse already, and we throw away the time/date data on reads anyway. We could modify the routines to use the more fully featured XML or JSON data structures..
Pachube allows configurable triggers based upon the sensor data it receives. Want to track tempertaure and humidity, and receive a tweet if your greenhouse gets too hot so your tomatoes don’t scorch? Can do.
Want to send a SMS Text Message to your phone when the output from your solar panels gets too low, and track the output in near realtime with powerful visualisations? Yup, no problem.
We can now come up with simple yet powerful sensing platforms in Swordfish!
Also don’t forget that the communication is two way. Want an indicator of the radiation count in Yokohama on your desk? Come up with a fancy indicator (servo clock guys i’m looking at you!), read in a public feed, and no problem!
I hope this work inspires some good projects out there. Please let us know if you make use of this work, we’d love to see what you’ve come up with!