Last active
January 1, 2016 22:38
-
-
Save russss/8210896 to your computer and use it in GitHub Desktop.
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
# 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