Skip to content

Instantly share code, notes, and snippets.

@whoeverest
Forked from glisha/readout_to_cosm.py
Created November 19, 2012 10:12
Show Gist options
  • Save whoeverest/4109965 to your computer and use it in GitHub Desktop.
Save whoeverest/4109965 to your computer and use it in GitHub Desktop.
Хаклаб on/off switch to cosm
cosm = {
feed_id: 86779,
api_key: <COSM KEY>,
}
#!/usr/bin/env python2.7
"""
Reads out the temp sensors from serial and posts them to https://cosm.com/feeds/86779
"""
import serial
import json
import requests
import time
import sys
import cosm_credentials
#set up the serial and ask for data
ser = serial.Serial("/dev/ttyUSB0", timeout=10)
ser.flushInput()
time.sleep(10)
ser.write("a")
#current time will be the same for all uploads
curr_time = time.strftime("%Y-%m-%dT%H:%M:%SZ%z", time.gmtime())
#read the data
while True:
line = ser.readline()
if not line or line == '\r\n':
break
line = line.strip()
if line == "OPEN":
hacklab_status = 1
else:
hacklab_status = 0
print line
url="http://api.cosm.com/v2/feeds/%s/datastreams/%s/datapoints" % (cosm['feed_id'], "hacklab_status")
headers = {'X-ApiKey':cosm['api_key']}
payload = {'datapoints': [{'at': curr_time, 'value': hacklab_status}]}
print url
print json.dumps(payload)
requests.post(url, data=json.dumps(payload), headers=headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment