How to: Log electricity usage from a CurrentCost EnviR – Part 1
After borrowing a CurrentCost electricity usage meter from my local library (if you’re in the area, then Eastleigh library will loan you one for free!), I decided to buy one, as I’d found it very useful in trying to reduce my electricity usage. The benefit of buying one as opposed to borrowing one was that I could start connecting it to a computer and logging data (the one I borrowed didn’t come with a connection cable).
So, I bought a CurrentCost EnviR meter on eBay for around £15:
I was impressed with how easy it was to get data from it to the computer. All I did was:
- Plug it in to a USB port on my Linux server with the cable (which was provided, but can also be bought separately if necessary – or even made yourself)
Check what serial device the USB-to-serial adaptor (which is built-in to the cable) is coming up as by running
dmesgand checking the final lines of the output. A similar command should work on OS X, and on Windows you should be able to check Device Manager and see what COM port number it has been assigned. In my case, it was
Test it using a serial communication program. For example, on Linux I ran
sudo minicom -D /dev/ttyUSB0 -b 57600(the last argument is essential as it sets the baud rate correctly for the device). On Windows something like Hyperterminal could be used.
You should find that you get a nice XML string back – something like this:
<msg><src>CC128-v1.48</src><dsb>00008</dsb> <time>21:36:48</time><tmpr>23.8</tmpr> <sensor>0</sensor><id>00077</id><type>1</type> <ch1><watts>00419</watts></ch1></msg>
That’s fairly self-explanatory, but just for reference:
src: the model number of the CurrentCost
dsb: days since birth (how long since you turned on the CurrentCost and it started collecting data)
tmpr: temperature (degrees C)
ch1: you can have multiple sensors attached to the CurrentCost, so this tells you which one it is
watts: current power usage in Watts
I’ve written the Python code below to read the XML over the serial connection, parse the data and return it. You’ll need to install
untangle (a handy XML parsing library for simple, small bits of XML) first using pip.
In the next part of this series, I’ll show how I linked this data up to Graphite to make lovely plots. As a quick taster, here is my current ‘dashboard’ (click for a larger view):