Skip to content

Instantly share code, notes, and snippets.

@russss
Last active January 1, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russss/8210896 to your computer and use it in GitHub Desktop.
Save russss/8210896 to your computer and use it in GitHub Desktop.
# Listen to a CurrentCost monitor on the serial port and forward data to Graphite
import serial
import socket
import time
from lxml import etree
ser = serial.Serial('/dev/ttyUSB0', 57600)
def send_metric(value):
sock = socket.socket()
sock.connect(("localhost", 2003))
sock.send("power.load_watts %d %d\n" % (value, time.time()))
sock.close()
while True:
text = ser.readline().strip()
doc = etree.fromstring(text)
if (doc is None or doc.find('sensor') is None or doc.find('sensor').text != '0'
or doc.find('ch1') is None):
continue
load = int(doc.find('ch1').find('watts').text)
print "Current power usage: %s watts" % load
send_metric(load)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment