#! /usr/bin/python """ Simple script for parsing probee co-ordinator data and passing it on to Emoncms. Donal Morrissey http://donalmorrissey.blogspot.com/ """ import re # Used for parsing the data string. import sys # For exit import serial # Serial port interfacing import requests # To send html post requests to Emoncms import time PORT = '/dev/ttyACM0' BAUD_RATE = 115200 MY_EMONCMS_API_KEY = 'fa' URL_TO_MY_EMONCMS = 'http://emoncms.org' # Open serial port try: ser = serial.Serial(PORT, BAUD_RATE) ser.open() time.sleep(1) except: print "Could not open serial port: ", sys.exc_info()[0] sys.exit(2) # Continuously read and print packets while True: try: line = ser.readline(); #Check the line is valid if not "++" in line: continue line = line.rstrip() #print line # Split the line up, we know our raw temperature data will be located in location 2. r = re.compile('[|,]+') split_line = r.split(line) # Extract the raw value as an int (our data is in location 2). raw_value = int(split_line[1]) temp = split_line[2] #print raw_value #Get the measured voltage from the raw ADC value. #vout_mV = raw_value / 10 #temperature_degC = (vout_mV - 500) / 10 #print temperature_degC # Now there is where the magic happens... # We send the new value to emoncms as a post request. #print (URL_TO_MY_EMONCMS+"/api/post?apikey=" + MY_EMONCMS_API_KEY +"&json={light:"+str(lit)+"}") if raw_value < 1000: try: requests.post(URL_TO_MY_EMONCMS+"/api/post?apikey=" + MY_EMONCMS_API_KEY +"&json={light:"+str(raw_value)+",outsidetemp:"+temp+"}") except: time.sleep(5) time.sleep(5) except KeyboardInterrupt: break ser.close()