Last active
December 14, 2015 04:59
-
-
Save ZogStriP/5032246 to your computer and use it in GitHub Desktop.
script used to publish currentcosts' temperature and wattage data to cosm through a raspberry pi
This file contains 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 python | |
# -*- coding: utf-8 -*- | |
import sys | |
import serial | |
import re | |
from datetime import datetime | |
import requests | |
temp = re.compile('<tmpr>(.+)</tmpr>') | |
watt = re.compile('<watts>(.+)</watts>') | |
impulse = re.compile('<imp>(.+)</imp>') | |
FEED_ID = #feed | |
API_KEY = #api_key | |
def post_watt(watts): | |
post_to_cosm('watts', watts) | |
def post_impulse(impulse): | |
post_to_cosm('impulse', impulse) | |
def post_temperature(temperature): | |
post_to_cosm('temperature', temperature) | |
def post_to_cosm(stream, value): | |
url = 'http://api.cosm.com/v2/feeds/%s/datastreams/%s/datapoints.csv' % (FEED_ID, stream) | |
headers = { 'X-ApiKey': API_KEY } | |
data = '%s,%s' % (datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'), value) | |
r = requests.post(url, data=data, headers=headers) | |
while 1: | |
try: | |
s = serial.Serial('/dev/ttyUSB0', 57600) | |
while 1: | |
message = s.readline() | |
temperature = temp.search(message) | |
watts = watt.search(message) | |
impulses = impulse.search(message) | |
if temperature is not None: post_temperature(float(temperature.group(1))) | |
if watts is not None: post_watt(int(watts.group(1))) | |
if impulses is not None: post_impulse(long(impulses.group(1))) | |
except: | |
print "unexcepted error:", sys.exc_info()[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment