Energy monitoring with a Raspberry Pi

A lot of energy companies have given away free electricity meters.  These have a clamp you put round the house supply, and a wireless link to a display.  They are generally branded by the energy company, but a lot of them are Current Cost EnviR meters.  If you look on the back, there is what appears to be an ethernet port, but is actually a serial connection in disguise.  You can get a usb data cable that connects to the Pi, and enables you to read an XML string from the meter, with the current power and temperature.  You’ll need to hunt around the interwebs for this, search  “Current Cost Data Cable” to try and find one on amazon or ebay.

Once you have the cable and it’s connected up, you can use a few lines of python to read the data.

import serial
serialObj = serial.Serial("/dev/ttyUSB0", 57600, timeout=6)
xml = serialObj.readline()
print(xml)

Running this should output something like:

<msg><src>CC128-v1.29</src><dsb>00484</dsb><time>20:16:49</time><tmpr>17.8</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00270</watts></ch1></msg>

You can then use a regular expression, or XML library to extract the data and do something useful with it. The example below sends it to a Graphite instance that is running on the Pi.

e.g.

import serial
import re
import urllib2
import time
import socket

serialObj = serial.Serial("/dev/ttyUSB0", 57600, timeout=30)
xml = serialObj.readline()
print "xml: " + xml
m = re.search('(.*)',xml)
power = m.group(1)
print 'Power: ' + power

# This conditional exists because occasionally the energy monitor returns an incorrectly low number.
if (int(power) &amp;gt; 10):
# Send the data to graphite
sock = socket.socket()
sock.connect( ("localhost", 2003) )
sock.send("house.power %d %d \n" % (int(power), time.time()))
sock.close()

See the next blog post for details on how to configure Graphite on a Raspberry Pi

If it all works, you’ll be able to end up with graphs like this:

Advertisements