#!/usr/bin/perl # Upload data from a CurrentCost Envi stored in a mysql database to Google PowerMeter. use strict; use LWP::UserAgent; use POSIX qw(strftime); use DBI(); use Data::Dumper; # userID is your 20-digit google powermeter id my $userID = ""; # variableID is the combination of device manufacturer, device model and device # id and variable name, as when you activated the device. # Separate the fields with a full stop: mfg.model.id.varname e.g. CurrentCost.CC128.1234.c1 # If you used cvars=1 then variable is probably c1, dvars then d1 etc. # This code assumes you're using one dvar. my $variableID = ""; # The auth token you received on activating your device (32 chars) my $authtoken = ""; my $subjecturl = "https://www.google.com/powermeter/feeds/user/$userID/$userID/variable/$variableID"; my $posturl = "https://www.google.com/powermeter/feeds/event"; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(POST => $posturl); $req->header('Authorization' => "AuthSub token=\"$authtoken\""); $req->content_type('application/atom+xml'); my $dbname = "cc128"; my $dbhost = "localhost"; my $dbusername = "cc128_ro"; my $dbpassword = ""; my $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost", "$dbusername", "$dbpassword", {'RaiseError' => 1}); my $now = time - 602; my $query = "SELECT * FROM cc128 WHERE timestamp > $now LIMIT 100"; my $sth = $dbh->prepare($query); my $rc = $sth->execute(); my $msg = "\n". "\n"; my $batchid = 1; while(my $ref = $sth->fetchrow_hashref()){ my $timestamp = $ref->{'timestamp'}; my $reading_time = strftime("%Y-%m-%dT%H:%M:%S.000Z", gmtime($timestamp)); my $power = $ref->{'ch1'}; my $kwh = ($power/600)/1000; # http://currentcost.posterous.com/calculating-kwh-from-watts $msg = $msg . "\n". "\n". "$subjecturl\n". "A.$batchid\n". "$reading_time\n". "6.0\n". "$kwh\n". "\n"; $batchid++; } $sth->finish(); $dbh->disconnect(); $msg = $msg . "\n"; $req->content($msg); my @res = $ua->request($req); print Dumper(@res);