CurrentCost Electricity Monitoring
3 minutes read •
For a while now, I’ve been using a Current Cost CC128 to monitor my electricity usage.
I hacked together a munin plugin to monitor both the electricty usage, and as the unit is in my lounge and coughs up temperature, it also provides the temperature reading.
For a number of reasons, the munin plugin doesn’t read the device directly. Only one device can open the serial port at a time, and there were a couple of things I wanted to do with the data, so didn’t want them fighting. I had a small piece of code grab the XML once a minute and dump it in a file instead.
Talking to a friend recently, they quite rightly pointed out (reminding me) that this method takes an instantaneous value for both the temperature and the electricity usage.
Whilst not a problem for the former as it won’t vary greatly within the 5 minute polling periods, the electricity usage clearly will. Think about boiling a kettle; if you were (un)? lucky enough to time the kettle just right, you could be between polls and not record the increased usage at all.
So, I’ve modified the way in which I record the data and write the files for the various things including munin to use.
As you’ll probably be aware, the CC128 outputs it’s data once every 6 seconds, so there’s clearly more data to make use of than just the single reading every 5 minutes.
I have a small daemon written in perl that listens on the serial port and every time it gets a full string of XML, it records the data to a database. Rather than re-write the munin plugin at the moment, along with anything else that uses the data file, I have a small bit of perl that runs from cron each minute that populates the expected XML into the file using averages of the values collected in the preceding 5 minute period. You could, of course, make this 95th percentile or whatever tickles your fancy.
So, the SQL table looks like this:
mysql> describe data;
+------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+-------------------+-----------------------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| dsb | int(10) unsigned | YES | | NULL | |
| time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| tmpr | decimal(6,4) | YES | | NULL | |
| sensor | int(10) unsigned | YES | | NULL | |
| unit_id | int(10) unsigned | YES | | NULL | |
| type | int(10) unsigned | YES | | NULL | |
| channel | int(10) unsigned | YES | | NULL | |
| data_units | varchar(128) | YES | | NULL | |
| data_value | int(10) unsigned | YES | | NULL | |
+------------+------------------+------+-----+-------------------+-----------------------------+
select sensor,dsb,unit_id,type,channel,data_units,avg(tmpr) as tmpr,avg(data_value) as value,now as now,
from_unixtime(300 * floor(unix_timestamp/300)-300) as start,from_unixtime(300 * floor(unix_timestamp/300)) as end
from data
where time between from_unixtime(300 * floor(unix_timestamp/300)-300) and from_unixtime(300 * floor(unix_timestamp/300))
group by unit_id,sensor,channel;
I should credit http://larig.wordpress.com/2012/02/08/time-rounded-to-five-minutes-in-mysql/ for the sql to round time to the current 5 minute window.
The bit that writes the XML file looks like this:
;
;
my $dsn = ;
my $dbh = DBI->($dsn, , );
my $sql = ;
my $sth = $dbh->($sql) || die ;
$sth->() || die ;
if($sth-> > 0) {
my $r;
;
while(my $row = $sth->) {
;
;
;
$r = $row;
}
;
;
;
;
;
;
;
} else {
die ;
}
$sth->;
$dbh->;
…and lastly, the bit that grabs the stuff and chucks it into the database looks like this (and also writes a backward compatible version of the XML file with the instantaneous values…:
$|++;
;
;
;
;
;
;
my $debug = $ENV || 0;
;
if(->()) {
if $debug;
exit;
}
my $port = $ENV || ;
my $baud = $ENV || 57600;
my $debug = $ENV || 0;
my $cc = ->($port) || die ;
$cc->($baud);
$cc->(0);
$cc->(1000);
$cc->;
my $dsn = ;
my $dbh = DBI->($dsn, , );
my $sql = ;
my $sth = $dbh->($sql) || die ;
while(1) {
my $timeout = 10;
my $chars = 0;
my $buffer;
LOOP: while($timeout > 0) {
my($count, $saw) = $cc->(255);
if($count > 0) {
$chars += $count;
$buffer .= $saw;
$buffer;
if $debug;
if($buffer =~ ) {
if $debug;
($buffer);
last LOOP;
}
elsif($buffer =~ ) {
if $debug;
$buffer;
}
} else {
if $debug;
$timeout--;
}
if $timeout == 0 && $debug;
}
}
{
my $xmls = ;
if($xmls =~ ) {
return;
} else {
= ;
my $xml;
{ $xml = ($xmls); };
return if $@;
($xml) if $debug;
for( %$xml) {
if() {
my $channel = $1;
my $name = .$channel;
for( ) {
my $unit = $_;
my $value = $xml->->;
(,$xml->,$xml->,$xml->,$xml->,$xml->,$channel,$unit,$value) if $debug;
$sth->($xml->,$xml->,$xml->,$xml->,$xml->,$channel,$unit,$value) || die ;
}
}
}
(XML,) || die ;
XML $xmls || die ;
XML || die ;
(, ) || die ;
}
}
I restart that from cron periodically, incase it dies. If it’s already running, it simply exits silently. It’s a quick hacky way to make it restart in the event something horrible happens. I did at least put the XML parse in an eval in case it barfs :-)
As usual, this blog post is as much for my personal notes of how I did it than anything else. Some of the code is awful, but it’s not life or death and so I’ve often been lazy with it. There’s no doubt this could be done prettier and less hacky if I had more time :-)