Chapter 5. Automate Your Coffee Pot Using X10

What good is a coffee pot if it can’t be controlled from the internet? By Dave Mabe

If you’re anything like most geeks I’ve met, you probably have a coffee pot that gets a lot of use catering to your caffeine addiction. I decided to play around with X10 technology and an open source software program called MisterHouse to automate my coffee pot and make it more user friendly.

I wanted to wake up to freshly brewed coffee, and I’m too lazy to remember to turn off the pot after a period of time to prevent burning. Sure, you could buy a fancy coffee pot that has some of the features I wanted built in, but even the most expensive coffee pot can’t touch the flexibilities you can create with a little Perl code.

MisterHouse (misterhouse.net) runs on Windows, Mac, and Linux, and lets you write simple Perl code to control a variety of hardware. To control your coffee pot, you’ll need to buy an X10 PC interface and an appliance module (less than $50 new — even less on eBay). You’ll plug the appliance module into the electrical outlet and the coffee pot into the appliance module. You’ll need a coffee pot with a mechanical switch — it needs to be able to be turned on and off simply by controlling the power supplied to it. The PC interface connects to a computer’s serial port and plugs into any electrical outlet.

Note

Add some Perl code to your morning brew and discover the difference it makes.

Once the hardware is connected, configuring MisterHouse is a breeze. There are detailed instructions in the misterhouse.ini file included with the software on the website.

Rather than schedule the time to start brewing, I wanted to schedule the time my coffee pot completed the brew so I could know the instant my coffee was ready. First, I timed how long my pot takes to brew our standard amount of coffee (8 minutes until the last drip). I created a file called coffeepot.pl in MisterHouse’s code directory on my PC with the following code in it (MisterHouse creates an event loop and executes your code a few times every second):

# C2 is the code I set on the appliance module
my $coffee_pot = new X10_Appliance("C2");
my $morning_coffee_time = "5:15 AM";
my $brew_duration = 8;
my $start_brew_at = time_add("$morning_coffee_time
– 00:$brew_duration:00");
my $coffee_timer = new Timer;
my $coffee_brew_timer = new Timer;
my $coffee_pot_on_duration = 40;

if (time_now($start_brew_at)) {
    set $coffee_pot ON;
    set $coffee_timer ($coffee_pot_on_duration * 60);
    set $coffee_brew_timer ($brew_duration * 60);
}

 if (expired $coffee_brew_timer) {
# do some optional notification here
}

 if (expired $coffee_timer) {
set $coffee_pot OFF;
}

This way, MisterHouse knows to start brewing at 5:07 a.m. if I want to have coffee at 5:15 a.m. A timer is started for the brew time ($coffee_brew_timer) and for the automatic shutoff ($coffee_timer). Once the coffee timer expires, the coffee pot is automatically turned off (Smokey the Bear would be proud).

I drink coffee twice a day — one cup in the morning and one in the afternoon, so I added code for the afternoon brew with some notification features as well. I’ve placed some motion detectors ($25 each) throughout my home and external home office. Depending on which room there has recently been motion in, MisterHouse turns a light on and off when the coffee’s ready. For the early morning brew when there’s no motion in the house, I turn on a light near my bedroom that acts as a gentle alarm clock. If the house is motionless at any other time of the day and the brew completes, I send an email to my BlackBerry with a “Turn Off Now” link, in case it’s been turned on in error.

MisterHouse comes with its own built-in web server that allows most any object to be accessed and modified using a browser. Along with a fairly comprehensive default web interface, there is full documentation on how to easily roll your own. I wanted to be able to schedule my coffee pot as well as turn it on and off from my BlackBerry, so I created a custom site that looks decent on a mobile browser.

I’ve also added some vacation logic. My morning brew is automatic — after I schedule it once, it happens daily. However, if there hasn’t been motion in the house during the previous evening, MisterHouse knows I’m probably on vacation, so the morning brew doesn’t kick off.

Well, there go the lights — it’s nectar time!

Dave Mabe is the author of BlackBerry Hacks from O’Reilly and lives in Chapel Hill, N.C.