In association with heise online

Working with real-time open data web services

Now that we have a small computer with a sensor attached, we need to do something useful with the data this produces. Cosm is a free-to-use "real-time open data web service for the Internet of Things", that makes it trivial to publish and subscribe to sensor feeds using a simple web API. We can use this to make our data available to web applications and IoT nodes that control physical outputs.

A Cosm feed is intended to be context-specific, e.g. for a particular location, and each feed can contain multiple datastreams, e.g. for different sensors. In this example we'll make use of a single feed containing just one datastream: the temperature reported by our DS1820B sensor.

First we need to log in to the Cosm console and add a new feed. There is an option which will also create template Arduino code for us, but we won't be using this so we'll select Something Else for the type of feed. Next we configure the feed for pushing data to Cosm, provide a meaningful title and add any appropriate tags. Once this is done we can then add a datastream to the feed, give it a title, and add any tags and also the units and symbol, e.g. Celsius and C. In this example we're using a datastream called ActualTemp.


Zoom A Cosm feed configured with a datastream

With a feed and datastream configured we need to create an API key that the Nanode will use when updating the feed. From the top-right menu we select Keys and then add a new key, giving this a meaningful label and configuring which of our feeds and datastreams it will have access to. If we want to err on the side of caution we configure the key to only have access to the feed we have just created, or perhaps even just one datastream within this. We also need to specify what privileges the key will have, and in this instance we only need to be able to update the feed. Where an application is reading from a private feed we would only specify read access, and if we wanted an app to be able to create new datastreams and delete them we could also grant those privileges to a key.


Zoom Creating a Cosm API key

To enable updating a Cosm feed from our application we need to first define the API key and the URL of the feed to use

#define COSM_API_KEY "your-secret-cosm-API-key"
#define HTTPFEEDPATH "/v2/feeds/50266"

Next we set up a template to use when updating the feed, specifying the datastream(s) to be updated and the value(s) to use.

static char feedTemplate[] = "{\"version\":\"1.0.0\",\"datastreams\":[{\"id\":\"ActualTemp\", \"current_value\":\"%d\"}]}";

static char feedPost[FEED_POST_MAX_LENGTH] = {0};

uint8_t fillOutTemplateWithSensorValues(uint16_t sensorValue1){
snprintf(feedPost, FEED_POST_MAX_LENGTH, feedTemplate, sensorValue1);
return (1);
}

See the Cosm API documentation for more details on what information you can pass to a feed and how to format it.

Then we add code to our main loop() which will attempt to populate the template, and if this is successful, use the EtherShield library's HTTP client to do the actual update.

if(fillOutTemplateWithSensorValues(DS1820Temp)){
//...
es.ES_client_http_post(PSTR(HTTPFEEDPATH),\
PSTR(FEEDWEBSERVER_VHOST),PSTR(FEEDHOSTNAME),\
PSTR("PUT "), feedPost, &sensor_feed_post_callback);
}

Note that, for the sake of brevity, things such as setting up TCP/IP and timers have been omitted from this example, but the complete working code can be found at GitHub.

Consuming data via the Cosm API is similarly easy to do and a device such as a Nanode could be used to do this and control physical outputs such as an LCD display, heating, lighting or an appliance.

The Cosm API also makes it easy to generate PNG graphs that can be embedded in web pages, by using simple-to-construct URIs that specify the required feed, datastream, scale, duration and timezone, etc.

https://api.cosm.com/v2/feeds/50266/datastreams/ActualTemp.png?width=730&height=250&colour=%23ff0000&duration=1day&legend=Temperature&title=Day&show_axis_labels=true&detailed_grid=true&scale=auto&timezone=UTC

Cosm graph
Zoom A Cosm graph generated using the above API call

Although the temperature sensor example given here is extremely basic, it needn't take much more complexity to create an application that is useful or even world-changing – such as the Air Quality Egg citizen sensor which is assembled from a Nanode and a handful of simple components. In this example though, our device has been at the end of a wired connection to the internet, but, to avoid the massive amount of work involved in wiring everything directly to the internet, there has to be another way.

Next: White space and democracy

Print Version | Permalink: http://h-online.com/-1571050
  • Twitter
  • Facebook
  • submit to slashdot
  • StumbleUpon
  • submit to reddit
 


  • July's Community Calendar





The H Open

The H Security

The H Developer

The H Internet Toolkit