After finding the instructions at http://www.jibble.org/currentcost/ to create a basic perl script that read the Currentcost datastream, parsed the XML and updated a RRD file I was able to create some power usage graphs. Using the RRDTool CGI application on a local linux box with the apache webserver I’ve got graphs for the last 10 minutes, 60 minutes, 6 hours, 24 hours, 7 days, month and year. However as tis is not internet facing, using an updated perl script I can now update a pachube.com feed. The pachube updates are once per minute so as to not exceed the api posting limits.
With the CC128 there is a history output that is generated every 2 hours, this includes the total kWh used on a daily basis. With an addition to the perl script I can now get a daily usage value that is used to update my Home automation twitter feed.
The script used is as follows: (Please note, this has been updated on 3rd September 2009 to include a daily cost calculation and to fix an issue with the history information attempting to update the rrd file)
#!/usr/bin/perl -w # Reads data from a CurrentCost CC128 device via serial port. # Based on script at http://www.jibble.org/currentcost/ # # Heavily updated to include pachube.com updates every minute # and a daily tweet at twitter.com for previous days power usage # # Andrew Lindsay http://blog.thiseldo.co.uk # use strict; use Device::SerialPort qw( :PARAM :STAT 0.07 ); # Alter these values to suit your configuration # Serial port CC128 connected to my $PORT = "/dev/ttyS0"; # Pachube.com values my $pachubeApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; my $pachubeFeed = "NNNN"; # twitter.com values my $twitterUser = "USERNAME"; my $twitterPass = "PaSsWoRd"; # Set your own values here my $unitCharge = "0.115"; # 11.50p my $standingCharge = "0.1441"; # 14.41p # End of configuration # Setup the serial port. my $ob = Device::SerialPort->new($PORT); $ob->baudrate(57600); $ob->write_settings; print "Reading currentcost datastream\n"; open(SERIAL, "+>$PORT"); # Keep track of how many sample and total for updating pachube once a minute my $sampleCount = 0; my $totalWatts = 0; while (my $line =) { # History received every 2 hours, look for last day, d001 and limit to whole house sensor for now. if ($line =~ m! .* !) { # process history output print "$line\n"; my $histSensor = "0"; my $kwatts = $1; my $cost = sprintf("%.2f",$standingCharge + ($unitCharge * $kwatts) ); # Tweet yesterdays usage, only need do this once per day if hour is 0 my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(); print "Got a match for day 1, sensor $histSensor Watts $kwatts kWh Hour $hour\n"; if( $histSensor == "0" && $hour < "2" ) { print "Tweet\n"; print "Yesterdays usage was $kwatts kWh at an approximate cost of £$cost Hour: $hour\n"; system( "curl -s -u $twitterUser:$twitterPass -d 'status=$kwatts kWh used yesterday at an approximate cost of £$cost' http://twitter. com/statuses/update.xml"); } } elsif ($line =~ m!0 .*(\d+\.?\d*) 1 .*(\d+).* !) { # Regular updates received from CC128 approx every 6s, total to create average over 1 minute my $sensor = $1; my $watts = $2; # Only deal with sensor 0 - Whole house sensor now if( $sensor == "0" ) { $sampleCount++; $totalWatts += $watts; # Update RRD system( "/usr/local/rrdtool/bin/rrdtool update currentcost.rrd N:$watts:0"); # Update pachube once a minute with a minute's worth of averaged data if ( $sampleCount > 10 ) { # Do a little formatting on the output my $averageWatts = sprintf("%.2f",$totalWatts / $sampleCount); #system("curl --request PUT --header 'X-PachubeApiKey: $pachubeApiKey' -D tmp.txt --data $averageWatts 'http://www.pachube.c om/api/$pachubeFeed.csv'"); system("curl -s --request PUT --header 'X-PachubeApiKey: $pachubeApiKey' --data $averageWatts 'http://www.pachube.com/api/$pa chubeFeed.csv'"); # Reset the counts $totalWatts = 0; $sampleCount = 0; } } } } 0*(\d+)
The script currently runs from a shell and outputs to a log file:
# ./readCurrentCost.pl >currentcost.log 2>&1
This is not a permanent application yet, more a learning exercise to determine what information comes out of the CC128 device while I develop an Arduino based version. Another aspect is tha we ar also identifying how much power we use on a daily basis and when it peaks.
The pachube feed is here. The twitter feed is currently restricted to my main account.