Created
November 17, 2012 23:01
-
-
Save glisha/4101111 to your computer and use it in GitHub Desktop.
Хаклаб on/off switch to cosm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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