Skip to content

Instantly share code, notes, and snippets.

@glisha
Created November 17, 2012 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glisha/4101111 to your computer and use it in GitHub Desktop.
Save glisha/4101111 to your computer and use it in GitHub Desktop.
Хаклаб on/off switch to cosm
#!/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 ConfigParser
import os
import sys
pwd = os.path.dirname(os.path.realpath(sys.argv[0]))
config_file = os.path.join(pwd,"readout_tocosm.cfg")
config = ConfigParser.ConfigParser()
config.read(config_file)
cosm = dict(config.items("cosm"))
#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)
r = requests.post(url, data=json.dumps(payload), headers=headers)
[cosm]
feed_id=86779
api_key=<COSM KEY>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment