Skip to content

Instantly share code, notes, and snippets.

@glisha
Created November 17, 2012 23:01

Revisions

  1. glisha revised this gist Nov 17, 2012. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions readout_to_cosm.py
    Original file line number Diff line number Diff line change
    @@ -34,16 +34,19 @@
    if not line or line == '\r\n':
    break

    if line == 'OPEN\r\n':
    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)
    r = requests.post(url, data=json.dumps(payload), headers=headers)
  2. glisha created this gist Nov 17, 2012.
    49 changes: 49 additions & 0 deletions readout_to_cosm.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    #!/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

    if line == 'OPEN\r\n':
    hacklab_status = 1
    else:
    hacklab_status = 0

    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)
    3 changes: 3 additions & 0 deletions readout_tocosm.cfg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    [cosm]
    feed_id=86779
    api_key=<COSM KEY>