Skip to content

Instantly share code, notes, and snippets.

@insom

insom/cctable.py

Created Oct 23, 2011
Embed
What would you like to do?
Export Current Cost power meter readings to a file readable by collectd
#!/usr/bin/python
import os
import serial
import re
s = serial.Serial('/dev/ttyUSB0', 57600)
FILENAME = '/tmp/cctable'
TMPNAME = FILENAME + '.new'
while True:
data = s.readline().strip()
rx = re.compile(r'<tmpr>([0-9\.]+)</tmpr>.*<watts>([0-9]+)</watts>')
bits = rx.findall(data)
if bits:
first, = bits
temp, watts = first
f = open(TMPNAME, 'w')
f.write("sensor0\t%s\t%s" % (temp, watts))
f.close()
os.rename(TMPNAME, FILENAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment