Calculate carbon footprint using Pachube + AMEE

We hope to work with AMEE further down the line to make this a lot more smooth, but in the meantime here is a quick way to calculate the carbon footprint of a Pachube feed that is monitoring some kind of energy consumption (e.g. as in the electricity meter tutorial from here: http://community.pachube.com/?q=node/65 ). See an example Pachube carbon footprint feed here: http://www.pachube.com/feeds/549

You will need access to your own server, and this tutorial uses php, since AMEE provides really easy php sample code.This is a very basic tutorial which assumes that you know a tiny bit about php and just want to get it going quickly. Doing so means your carbon footprint gets stored and graphed as with all Pachube feeds, and of course also means that others can build things that respond accordingly.

1. Sign up to AMEE

AMEE
In their words, AMEE - The world's energy meter "is a neutral aggregation platform to measure and track all the energy data on Earth". Their API makes it pretty straightforward to calculate the amount of CO2 generated from a given range of input criteria (including country, electricity provider, device, appliance), etc. While larger organisations have to pay for this facility, as a developer you can sign up for free access here: http://www.dgen.net/amee/signup.php

2. Upload basic AMEE php functions to your server

Download a zip file containing AMEE's very basic functions from here: http://trac.amee.cc/attachment/wiki/AmeeExamples/apiexamples_php_sockets...

This extracts to 3 files.

You don't need to change xml_functions.php.

However, amee_functions.php needs you to edit your AMEE login and password (which you should have received when you signed up).

3. Replace index.php

As it stands, index.php shows you how to make a basic call to the API and prints out the response text.

Essentially, we need to rewrite this to send AMEE the relevant stream of our Pachube feed and receive the conversion as kgCO2. In this example I am using data stream 5 of http://www.pachube.com/feeds/504 because that stream is measuring 'watt hours'. We don't use the Pachube API here because we're just extracting values from the 24-hour history CSV, eg. http://www.pachube.com/feeds/504/datastreams/5/history.csv

Instead of using the existing index.php, download this edited version: http://community.pachube.com/files/amee.zip.

You can edit any of the code, but the only bits that you have to edit are right at the top: $pachubeFeed (which is the feed number containing the stream you want to convert); $pachubeStream (which is the stream number of that feed which contains an electricity amount in Wh); and $supplier.

This last one, $supplier, is an AMEE id number that denotes what the energy value is actually for. Here I am using the code for my own electricity supplier (since electricity consumption is what is being measured in the Pachube feed). In case you are based in the UK, I've made a quick look-up table that takes electricity companies stored by AMEE and returns their AMEE id code: http://haque.co.uk/pachube/amee/suppliers.php.

The bit does all the AMEE work is this:

function get_amee_value($s, $v){
$ameePath="GET /data/home/energy/uk/suppliers/$s/?kWhPerMonth=$v HTTP/1.0";
$body="";
$xml = sendRequest($ameePath,$body);
preg_match("/<AmountPerMonth>(.*?)<\/AmountPerMonth>/", $xml, $match);
$value = $match[1];
return $value;
}

4. Dig down into the code (optional)

If you're not in the UK, or you want to measure something different you can look up the relevant AMEE ID for that item by using some of the details from your AMEE welcome email. First, go to http://stage.co2.dgen.net/ and log in. Click "data" to view data category details; then navigate to the item you're measuring; e.g. home > energy > Ireland > suppliers > Airtricity; or home > heating > detached bungalow, 1, coal, 1930-1995, boiler-manual feed. The id you are looking for is labelled UID. You will then also need to edit the $ameePath variable in the php code to match.

You might also want to edit the php if you want your Pachube feed to show different streams. As it stands it is serving kWh in the last 15 minutes, 1 hour and 24 hours, as well as the converted amounts of kgCO2. AMEE currently returns values per month, but in the future resolution will be increased.

5. Add feed to Pachube

Upload all 3 php files to your server (it's probably best to make a new directory just for this, e.g. mydomain.com/amee/), check that it returns values that look right (i.e. by browsing to mydomain.com/amee/index.php) and then add the feed to Pachube as normal. Over the next few hours you can watch your carbon footprint grow and grow and grow......!

AttachmentSize
amee.zip856 bytes