Using R to Fetch and Plot Cosm Data

I have a number of sensor devices that use Cosm as a cloud based store data.  Cosm provides a fairly easy to use system for storing, viewing, and reacting to data from sensor networks (or just about any other source).  The Cosm pages provide basic graph building functions but lack the ability to do any computations, overlays, etc.  To make use of this data more effectively it can be loaded into the R Statistical Computing software package.  R is a full featured statistical package that is freely available for all major operating systems.

This post describes the process of using the RCurl package to retrieve data via the Cosm API from within R.

First, make sure that the RCurl package is loaded:

> library('RCurl')

Set a variable with an appropriate Cosm API key with access to the feeds/datastreams in question:

> api_key = "asdfasdfasdfasdfasdfasdfasdf....asdfasdf"

Cosm provides an API for historical data queries.  This API has some restrictions on the number of points returned, intervals between points, etc.  Consult the documentation for full details.

In this example I’ll fetch the 30 second interval data points from the Air_Temp datastream of feed 12345 between 2013-04-01 00:00:00 and 2013-04-01 06:00:00, with a limit of 1000 points:

> csv_data = getURL(
+ "https://api.cosm.com/v2/feeds/12345/datastreams/Air_Temp.csv?start=2013-04-01T00:00:00Z&end=2013-04-01T06:00:00Z&interval=30&limit=1000",
+ httpheader = c('X-ApiKey' = api_key)
+ )

(Note that all times are in UTC.)

Now that the data has been loaded into R it needs to be converted into a usable format:

> data = read.table(textConnection(csv_data), sep = ",", col.names=c("at", "value"))

And then add a timestamp column with the Cosm timestamps converted into R Date-time object:

 data$timestamp <- strptime(data$at, "%FT%T", tz = "UTC")

The data is now in R and ready to use.  A simple plot:

> plot(data$timestamp, data$value)

Comments are closed.