April 20th, 2011

Last year, I went to the Rewired State Carbon & Energy hack day (#rscarbon), which was a great event. I went along with an idea, and even better actually got to build it! We got a great team together, and despite initial technical teething problems, had a demo running in time for the show and tell at the end (by the skin of our teeth). I’ve been meaning to write it up for ages, but instead, I decided to make a video.

The code is, as ever, available on Github, and there are other writeups on Paul Tanner’s blog and the Rewired State site.

December 17th, 2009

In early September, Betavine organised a 24-hour hack event called EcoMo09, with an environmental theme. Unfortunately I couldn’t stay the whole time, but I went along as an AMEE representative to support other people using our API during the event. In the quiet periods, I managed to come up with my own hack, which used GreaseMonkey and AMEE to add carbon emissions to Google Maps.

You can even get the code and try it yourself. It’s very basic, and actually most of the work was dealing with Google Maps and making Greasemonkey behave properly - the AMEE bit, getting the carbon value, was dead easy.

October 28th, 2009

IBM DeveloperWorks articles

Over the summer, I was asked to write an article for the IBM DeveloperWorks site about energy monitoring and so on. Eventually, that turned into a series of articles centered around AMEE and how you can track energy consumption and calculate carbon emissions with it.

So, now that they’re all published, here’s a bit of a linkdump to the various articles and other things that came out of them.

Introduction to AMEE
This is a technical introduction to the AMEE platform, how it works, and what you can do with it. It covers things like signing up, creating profiles, storing data, and getting results. It was also adapted into a screencast for the AMEE developer site.
Calculate your computer’s carbon footprint using AMEE
This one is an introduction to the wide variety of ways in which you can track energy data using AMEE, using the monitoring of computer energy use as a case study. It also shows some code examples for the AMEE ruby gem.
Monitor home energy with AMEE
This is my favourite of the three - a long tutorial, with lots of code, which shows a complete implementation of an online energy-monitoring system, using a CurrentCost display, AMEE, and a little Rails code. This uses a lot of the open source software I’ve been writing over the last year, which regular readers will have seen in previous posts, but it’s really nice to tie it all together and tell the world how it works!
James Smith on embedding environmental intelligence
Scott Laningham was nice enough to interview me for the DeveloperWorks podcast when the first article went out, so if you want to hear me blathering on about it in person, you can do so!
March 28th, 2009

Publishing CurrentCost data to the world

A while ago, I started hacking around with a CurrentCost real-time energy monitor. This is a very nice little device that measures your electricity use, but more importantly has a serial output on the bottom so you can get data out of it.

Well, eventually I decided that while being able to get data off the meter was nice, it would be better if I could publish it somewhere. So, I wrote a program in Ruby to do exactly that. It reads data from the meter, and sticks it on the web. I’ll go into a bit more detail in a minute, but first, you can download it (and get the source code) from github.

The program operates as a system daemon, which runs in the background on your Linux box (Mac should work as well) and uploads to various places. Adding new publishers is dead easy, so if you want to publish your data somewhere else, you can easily add it.

The Carbon Diet

First up, my own site, The Carbon Diet. This uploads your daily usage history from the meter into your carbon diet account so that you don’t have to take so many meter readings to get an accurate graph. This is still pretty experimental and needs more work, but it’s pretty useful already. You can see how it looks on my profile.

AMEE

Next, what I think of as the important one. AMEE, if you’re not aware, AMEE is a neutral aggregation platform for sharing energy data and carbon calculations (disclaimer: I work for them these days). Think OpenID for your energy identity. Anyway, we have a nice “smart meter” demo which uses my currentcost app as the data source. Every minute, it uploads into an AMEE profile, and then another app makes a nice graph of the CO2 produced. In theory, the Carbon Diet could pull that data from AMEE instead of me publishing it directly, but that’s still to come.

Pachube

Another energy data sharing service is Pachube (pronounced “patch bay”). As far as I can tell, this is more geared at art and design than rigorous data, but it’s fun to play with. They’ve done a bunch of stuff with the CurrentCost, and now my app joins the throng. My pachube feed updates every 6 seconds - every time the meter sends data out.

Twitter

Finally, what would be the point of a web energy publisher if it couldn’t tweet? If you really want to, you can see my minute-by-minute energy usage on Twitter by visiting @james_energy.

August 31st, 2008

Mocking Kernel#require

The other day, I remembered to add coverage analysis to my AMEE-Ruby tests, using rcov. The results were pretty good - most of the code was already tested, except a few failure cases, and I quickly wrote some new tests to make sure those were working properly. One little bit of code stood out though. Because AMEE talks XML and JSON, my gem can use JSON, but only if the JSON gem is available on the system (XML support is built into core Ruby). Problem is, require calls throw errors if they fail, so I had to write some code to handle the load failure and carry on regardless. This is the code inside amee.rb:

begin require 'json' rescue LoadError nil end

Problem with this is that the gem is installed on my system, so the require never fails, and the rescue is never used. Rcov notices, and I can’t get to 100% coverage, which is annoying.

So, mocking to the rescue. We have to make Kernel#rescue throw an error if we try to require ‘json’, even if the gem is installed. At first, I tried to use flexmock to do this, but couldn’t make it work - if anyone knows how, please tell me. My final approach was to monkeypatch Kernel#require inside my test so that require ‘json’ (and only json) would fail:

it "should cope if json gem isn't available" do # Monkeypatch Kernel#require to make sure that require 'json' # raises a LoadError module Kernel def require_with_mock(string) raise LoadError.new if string == 'json' require_without_mock(string) end alias_method :require_without_mock, :require alias_method :require, :require_with_mock end # Remove amee.rb from required file list so we can load it again $".delete_if{|x| x.include? 'amee.rb'} # Require file - require 'json' should throw a LoadError, # but we should cope with it OK. lambda { require 'amee' }.should_not raise_error end

We have to make a new require function, then use alias_method to rename the old one and install our new one in it’s place. Rails includes alias_method_chain to make this easier, but that’s not available in pure Ruby. Never mind. Once we’ve done the monkeypatch, we take amee.rb out of the $” array, which lists all the currently-required files, to make sure we can require it again, then simply run the require and make sure it catches the error that is thrown by our patched require. And the most satisfying part? The rescue is executed, the test works, and we get to 100% coverage. I’ve got a nice warm fuzzy feeling inside… mmmmmm.

August 6th, 2008

Hacking your energy usage with the CurrentCost

The other day, I managed to get hold of a CurrentCost energy monitor (available to buy from here, or maybe from your electricity supplier). Now, the nice thing about this particular monitor (apart from the ton of information on-screen) is the fact that it has a serial output on the bottom, which you can (with a bit of hacking) plug into your PC, and - bingo - lovely XML data!

However, once you have it connected and spewing XML at you, you really need something to do with all that data. I don’t have anything big written yet, but my first step towards making something useful is a Ruby gem, which is available from GitHub. So far it can only parse the XML data from the meter - direct access to the serial port is hopefully coming soon.

In other news, the AMEE gem I started a while ago is still coming on. It can now use the XML and JSON interfaces, parse the whole Data API, and retrieve a list of Profiles. Not a bad start.

July 10th, 2008

AMEE for Ruby

In a fit of why-the-hell-not, I’ve just started writing my first gem for Ruby, which is going to be a wrapper around the AMEE carbon calculation engine. It’s still in a very early incarnation, but more will be forthcoming soon. At the moment it can authenticate, and parse DataCategory nodes. DataItems and DataItemValues will be following after a short (UK-based) holiday. Source code and installation instructions are available from GitHub.