Last active
January 3, 2016 14:09
-
-
Save davecoutts/8473870 to your computer and use it in GitHub Desktop.
Collect temperature and battery voltage data from ciseco thermistor temperature sensors and push collected data to Graphite and https://xively.com
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
import re | |
import serial | |
import logging | |
from time import sleep | |
import statsd | |
statsd_client = statsd.StatsClient('localhost', 8125, prefix='ciseco') | |
import xively | |
import requests | |
import datetime | |
XIVELY_API_KEY = "BLAHBLAH" | |
XIVELY_FEED_ID = 1234 | |
CELSIUS = xively.Unit(label='Celsius', type='derivedSI', symbol=u'\xb0C') | |
VOLT = xively.Unit(label='Volt', type='derivedSI', symbol='V') | |
api = xively.XivelyAPIClient(XIVELY_API_KEY) | |
feed = api.feeds.get(XIVELY_FEED_ID) | |
#----------------------------------------------------------------------- | |
LOGFILE = '/home/dave/var/ciseco/ciseco_log.csv' | |
logger = logging.getLogger('ciseco') | |
logger.setLevel(logging.DEBUG) | |
fh = logging.FileHandler(LOGFILE) | |
formatter = logging.Formatter('%(asctime)s,%(message)s','%Y-%m-%d %H:%M:%S') | |
fh.setFormatter(formatter) | |
logger.addHandler(fh) | |
#----------------------------------------------------------------------- | |
DEVICE = '/dev/ttyACM0' | |
BAUD = 9600 | |
NAMEMAP = {'AATMPA': 'lounge_temp', | |
'ABTMPA': 'shed_temp', | |
'ACTMPA': 'spareroom_temp', | |
'AABATT': 'lounge_batt', | |
'ABBATT': 'shed_batt', | |
'ACBATT': 'spareroom_batt'} | |
logger.info('Starting') | |
ser = serial.Serial(DEVICE, BAUD) | |
def send_xively(key, value): | |
"""""" | |
now = datetime.datetime.utcnow() | |
if 'temp' in key: | |
unit = CELSIUS | |
elif 'batt' in key: | |
unit = VOLT | |
feed.datastreams = (xively.Datastream(id=key, unit=unit, current_value=value, at=now),) | |
try: | |
feed.update() | |
except requests.HTTPError as e: | |
logger.error("HTTPError({0}): {1}".format(e.errno, e.strerror)) | |
pass | |
return | |
def store_result(key, value): | |
"""""" | |
statsd_client.gauge(key, float(value)) | |
statsd_client.incr('{key}_count'.format(key=key)) | |
logger.info('{key},{value}'.format(key=key, value=value)) | |
return | |
while True: | |
n = ser.inWaiting() | |
if n != 0: | |
serial_message = ser.read(n) | |
for key, value in re.findall("a([A-Z]{6})([-+]?\d+.\d+)", serial_message): | |
store_result(key, value) | |
if key in NAMEMAP: | |
store_result(NAMEMAP[key], value) | |
send_xively(NAMEMAP[key], value) | |
sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment