Skip to content
Oct 1 12

Home Power Monitor

by Riscy

I’ve wanted to do a home power monitoring project for some time. I was using a Clipsal Cent-a-meter a while ago to track power usage when I lived in Victoria but I didn’t have much luck with the newer model. It didn’t have a good range and seemed to lose connection between the base and sending unit. So I thought I would go DIY.

I looked at lots of different systems including PIC microcontrollers but I decided to give the JeeNode and JeeLink combination a try. It seemed like a good choice for the following reasons:

  • Arduino compatible
  • Low power (3.3V)
  • Integrated wireless
  • Easy interfacing with a PC (JeeLink)
  • Expandable

In our house we have a smart meter which has a blinking red light for every Watt Hour of power used. So by measuring the length of the pulse you can have an indication of instantaneous power usage. This saves using clip on CTs which need to be installed by an electrician. The other advantage of using the smart meter is that this is the actual power usage I am being charged for. The only problem with this is that in Tasmania we have two different tariffs, one for normal usage and one for heating (cheaper). My system doesn’t take into account the tariffs and can only measure total usage. It also could not be used with electricity generation because the smart meter only blinks for imported power, if you are generating then the actual power usage will be higher than the imported.

So I went to Modern Device and ordered the following:

The JeeNode will be used to monitor the blinking light, which will send data to the JeeLink (plugged into my HTPC running Ubuntu) and the USB BUB II is required for programming the JeeNode.

I soldered the JeeNode and JeeLink up when they arrived on the coffee table while watching something mindless on TV. It had been a while since I had done proper soldering and with a good temperature controlled iron it was pretty easy. I should get some good quality solder though!

I tested the JeeNode and JeeLink and they worked first time. Now to look for some code.

I ended up using this code I modified it to suit the 1Wh blinks and using a LDR.

Here is my version of the transmitter code:

// Reading Comparator input

#include <Ports.h>
#include <RF12.h>
#include "kWh.h"

class Port;
Port inputPort(1);
static unsigned long last;

void setup() {
    inputPort.mode2(INPUT); // Set AIO mode as input
    inputPort.digiWrite2(1); // Activate pull-up resistor for AIO
    rf12_config(); // Apparently this is necessary
    rf12_easyInit(3); // Send value at most every 3 seconds
    last = millis();

void loop () {
    static boolean ledOn = false; // Variable to indicate LED status
    int data = inputPort.anaRead();
    if (!ledOn && data > 750) {        // After testing I found the switching point was 750
        ledOn = true;
    } else if (ledOn && data < 750) {
        ledOn = false;

void ledBlink() {
    static int nBlinks = 0;
    unsigned long time = millis();
    unsigned long interval = time - last;

    if (interval < 0) { // millis() overflow
        last = time;
        nBlinks = 0;
    } else if (interval > 1000) { // 1+ sec passed
        // Blinks are 1000 per kWh, or 1 Wh each
        // One hour has 3.6M milliseconds
        long watts = nBlinks * 1 * 3.6E6 / interval;

        last = time;
        nBlinks = 0;

static void wattSend(long watts) {
    Packet_t packet;
    packet.lang = LANG_ELECTRICITY;
    packet.mesg = MESG_ELEC_CURRENT; = watts;
    rf12_easySend(&packet, sizeof packet);

And here is my version of the JeeLink receiver code

#include <Ports.h>
#include <RF12.h>
#include "kWh.h"

void setup() {

void loop() {
    if (rf12_recvDone() && rf12_crc == 0 && rf12_len == sizeof (Packet_t)) {
        Packet_t packet = *(Packet_t *) rf12_data;

        if (packet.lang == LANG_ELECTRICITY) {
            if (packet.mesg == MESG_ELEC_CURRENT) {
    else {

static void wattShow(long watts) {
    Serial.print("Usage: ");
    Serial.println(" W");

I connected 4 AA NiMh batteries as the power supply to the JeeNode and sat it on top of the smart meter. The LDR is connected between the AI pin and ground of one of the ports.

Pretty isn’t it?

The LDR is right next to the LED

The JeeLink is connected to my PC running Mythbuntu, which is my HTPC.

Ubuntu is running Perl code to read the serial port and dump it to a file which timestamps the incoming data.

use strict;
use warnings;
use Device::SerialPort;
use POSIX qw/strftime/;

my $port = Device::SerialPort->new('/dev/ttyUSB0');
my $time = time;

if( ! defined($port) ) {
        die("Can't open /dev/ttyUSB0 $^E\n");

my $outfd;
open ($outfd, ">>", "log.txt") or die "Failed to open output file - $!n";

my $output = select(STDOUT);
select $output;


while(1) {
    my $char = $port->lookfor();
    if ($char) {
        $char =~ s/\xd//g;
        my ($watts)= $char =~ /(\d+) W/;
        print strftime('%d-%m-%Y %H:%M:%S',localtime);
        print ",$watts\n";
        print $outfd strftime('%d-%b-%Y %H:%M:%S',localtime);
        print $outfd ",$watts\n";

It works and the next step is to make a nice graphing utility. I’ll post again when I have some pretty graphs. At the moment I’m just dumping all the data into Excel.

Oct 4 11

MythTV Dynamic Range Equalisation

by Riscy

I found this on the net recently after trying to work out how to fix problems watching movies with very low dialogue volume levels and loud sound effects:

After following the instructions I am now able to watch all my movies using MythTV without having to change the volume once, and Claire doesn’t complain about the movie being too loud when she is in bed. Win win!


P.S. Saved from the Internet Archive because the original page is dead


MythTV: Volume Leveling

Back at the other end of this year, I decided to retire my aging TiVo, and become a full-time user of the magic that is MythTV. For those of you who don’t know, MythTV is a software package you can run on a computer (Linux based. I won’t swear that there isn’t a Windows port, but of this, I know little) which does more or less what a Digital Video Recorder does — it can record TV to its hard drive so you can watch it whenever you like. But because you’ve got a whole computer which is under your control, it can also do, well, anything else you want. For my purposes, the most useful thing that it does is to act as a sort of video jukebox: I can back up all my DVDs to a network hard drive, and thereby avoid all the hassle of (a) having to keep piles of DVDs in the living room, (b) risking scratches, and (c) dealing with temperamental DVD players. Another of its nice features is that, with an add-on called MythNetTV, you can subscribe to video podcasts via MythTV, and it will deliver new episodes to you just as if they’d been broadcast over the air, allowing you to watch grainy, low-resolution YouTube quality video of cats doing amusing things on your 40 inch HDTV. I’ve long found it ironic that as TVs get bigger and resolutions increase, we’re increasingly willing to huddle around a laptop monitor to watch a 320×200 viral video. Well, suck it, losers, because I’m watching The Spoony Experiment and The Nostalgia Critic on the big screen.

Now, like most Linux projects, it’s not all sunshine. I’ve got two cheap TV tuner dongles, which don’t work with it (There’s several very nice tuners which work with it, but I really wanted just a cheap one to use as a secondary tuner). And the usability is not nearly as polished as, say, TiVo (That said, it’s miles beyond most cable box DVRs in the UI department). There’s a few annoyances that I have yet to be able to overcome (The size at which subtitles render is hard-coded, which means that it displays at a size which was plainly selected for a Standard Definition screen, making it slightly microsocopic at 1080p), but, like I said, it’s a whole computer, and you can bring to bear all that implies.

I’d been meaning for some time to write a series of articles about the cool things I’ve written to bend the Mythtv to my will, but actually banging any of my hacks into a presentable state has required a bit more time than I’ve been willing to invest. But this week, I found something so handy and so elegant that I thought it was time to share it.

So, MythTV trick Number One:

One problem with playing back video from various disparate sources is the volume level. You know how when you’re watching regular old-fashioned TV, more often than not, the commercials will be about a million decibels louder than the show? The volume will be different from one channel to the next. When you’re also downloading New Media from The Intertubes, those too will be at radically different levels from TV, and from each other. DVDs are usually at a much lower level than TV (I think this may be caused by the downmix from 5.1 to stereo). And if, say, you’re watching a third generation rip from a grainy VHS of a film so rare that no one involved in it will even admit to having heard of it, you’re talking borderline inaudible.

With months of training, I’ve got Leah to the point where she’ll actually give me a fair chance to reach the remote control and turn the volume down before she yells at me to turn it down the instant the sound starts, but it’s still not really an optimal solution for me to keep having to adjust the volume from one video to the next.

If you are a modern person who keeps all your music in digital format, you may be familiar with the concept of volume normalizing, which analyses a whole song and works out how to adjust the overall volume to the song so that you don’t blow out your ear drums if Shuffle Play puts a John Tesh song right after one by Alice in Chains (Which is not to say that you don’t deserve deafness for your taste in music).

But the tools for doing this to video are less mature, and besides, you might be willing to spend 2 minutes preprocessing a 4 minute song you’re going to keep for the rest of your life, but I’m not willing to spend 30 minutes processing an episode of Stargate Universe which I’m going to delete as soon as I’ve finished watching it.

As it turns out, though, since you’re running a whole computer, and it’s Linux, the Magical World Where You Can Basically Do Anything You Want So Long As You’re Willing To Carve It From the Solid Granite of the OS With Your Bare Hands, it’s possible to just order your sound card to do that normalization for you as it plays — in this case, it’s called Compression and Limiting.

I could just about muddle through the science of how it works, but probably not well enough to explain it to anyone in detail. The general gist of it is that a “compressor” squishes audio such that it reduces the difference between the loudest sounds and the softest. When a sound is louder than some threshhold, it reduces the volume, but it does it in a very smooth way that sounds good. This is something radio stations do so that you can turn the volume up loud enough to hear the soft bits without blowing out your speakers for the loud bits. A “limiter” is the same basic process, but it’s much more powerful and lacks the subtlety of a lower-rate compressor. Basically, the purpose of the compressor is to make the audio all “fit” within a certain range of loud-to-soft, and then the limiter boosts the gain (ie. “Turns the volume up”) while keeping it from exceeding a certain threshold.

In Linux’s ALSA sound system, you can create plugins which (long story short) basically act like virtual sound devices. You tell an application to use that sound device, and any audio the application tries to put out will be sent through the plugin before it’s turned into sweet delicious audio. Here’s an audio compressor that I threw together based on some stuff I found on the ALSA wiki:

pcm.ladcomp {
      type plug
      slave.pcm "ladcomp_compressor";
 pcm.ladcomp_compressor {
      type ladspa
      slave.pcm "ladcomp_limiter";
      path "/usr/lib/ladspa";
      plugins [
              label dysonCompress
              input {
                  controls [0 1 0.5 0.99]
 pcm.ladcomp_limiter {
      type ladspa
      slave.pcm "default";
      path "/usr/lib/ladspa";
      plugins [
              label fastLookaheadLimiter
              input {
               controls [ 15 0 0.8  ]

This code can be put in your /etc/asound.conf, then just tell MythTV to use the sound device ALSA:ladcomp (It’s under Utilities / Setup -> Setup -> General if you’re using the default menus. I’ve hacked mine up a bit, so it took me longer to find it). It should have defaulted to something like ALSA:default. Restart MythFrontend, and voila: all your audio should play at around and about the same level. To use this, you’ll need the ladspa plugins. If your MythTV is running on Ubuntu Linux (I use Mythbuntu, a version of Ubuntu oriented toward MythTV (For the non-Linux experienced, you can basically run any Linux software on any Linux box. The major difference between various Linux distributions is basically which software it installs by default, as opposed to which ones you have to download and install on your own. Ubuntu is a distribution which takes the radical step of assuming that its users may include actual human beings and might want to spend more time actually using their computer than assembling it.)), you can get them by running “sudo apt-get install ladspa-sdk swh-plugins”.

If you want this trick to apply to other applications, you can tell them to use ladcomp as their audio device too. For instance, with mplayer, try mplayer -ao alsa:device=ladcomp.

If you want to do some fine tuning, you can try changing that 15 to other numbers to change the range for the final audio (You have to restart MythFrontend before the changes will be honored). I haven’t found quite the right setting for me personally yet — 15 is a bit higher than I want, I think, since it makes the “comfortable” position on my stereo’s volume dial around 6 out of 30 — I think somewhere in the 10-15 range would be better. But, at least for me, it does put MythTV in about the same volume zone as the Nintendo Wii, so I’m not racing to turn the volume down when we turn the game consoles on.

So, with any luck, and a little bit of work, you too can bend the sound system to your will, and watch whatever you like without fear of getting yelled at by your fiancee for having the TV turned up too high.