Pages

Thursday, January 3, 2013

a Python script to fetch one COSM datastream v0.1

This is a simple Python script to fetch one COSM / pachube datastream.



Cosm is a secure platform for Internet of Things devices and products. Its API and web service provide real-time control and data storage.

Unfortunately on these days cosm.com does not provide any software to download a datastream data, so i've build this script, which use cosm api to download datapoints of a datastream.

Before launch the script, you have to open the script with an editor and setup some parameters.

#setup your cosm account data here
#set cosm key
COSM_APIKEY="YOUR_COSM_APIKEY_HERE"
#set cosm feed id
COSM_FEEDID=YOUR_FEED_ID_HERE
#set cosm datastream
COSM_DATASTREAM=YOUR_DATASTREAM_HERE

#download datastream backward from DATETO to DATEFROM, with datapoint interval of INTERVAL (seconds), and backward step of DATEINTERVAL (hour)
#cosm history interval request
INTERVAL=0
#get backward from DATETO to DATEFROM
DATEFROM=datetime(2012,12,5,15,45,0)
DATETO=datetime(2012,12,8,15,45,0)
#interval between requested data (in hour)
DATEINTERVAL=1

#output file name, and conversion (date) to csv format
#set output file
OUTFILE="out.csv"
#convert cosm data
CONVERTDATA=1


Code

Notes
  • read risk disclaimer
  • excuse my bad english

2 comments:

  1. Hello,

    This is exactly the script I am looking for. However I can not seem to get it to work. Could you provide further documentation on DATEINTERVAL and INTERVAL. I have irregularly spaced data points that average to a 30 second spacing. I would like all points for the last month.

    Thanks

    ReplyDelete
  2. this script use the historical query api, here you should find all info about this query: https://cosm.com/docs/v2/history.html

    DATEINTERVAL is the interval between requested data, expressed in hour.
    if you are looking for data from 2012,12,5,15,45,0 to 2012,12,8,15,45,0 with a DATEINTERVAL of 1, the script will take point from 2012,12,8,14,45,0 to 2012,12,8,15,45,0 then 2012,12,8,13,45,0 to 2012,12,8,14,45,0 and so on.
    INTERVAL to 0 means that all datapoints are downloaded, if you have 60 seconds datapoint and you want 2 minutes datapoint to download, use INTERVAL 120.

    ReplyDelete