Skip to main content

Home, smart home

Linux + MisterHouse + Perl + X10 = home automation

Mark Komarinski is a columnist for LinuxWorld magazine.

Summary:  An intruder is detected in the back yard by infrared sensors, setting off the automatic sprinklers -- or even dialling 911. Not that he'd have been able to get in anyway, as the house is protected by Java-based iButton keys.... In the 1960s, MI6 or U.N.C.L.E. might have paid millions for the things you can do today on your Linux box with MisterHouse -- without installing any new wiring.

Date:  01 Dec 1999
Level:  Intermediate
Activity:  682 views

Most everyone, when asked, will venture an opinion on what the Next Big Thing will be -- and two such opinions rarely match. Until recently, I was convinced that the next big thing would be wireless PDAs, so that Linux geeks like me could check their email or the price of their Red Hat stock from anywhere.

A few days ago at lunch, however, I was speaking with a hardware engineer who changed my mind about the next great technology. At first I wasn't convinced by his vague arguments about "intelligent appliances." But after I pressed him for more detail -- and after we agreed that neither of us are much interested in having automatic toasters (who will put the bread in? and why?) -- we decided that the idea of higher-grade commercial appliances would be great.

The best example we could come up with was the idea of home automation. To have one central location that can control lights, or the temperature -- and to be able to access that from anywhere (including your wireless PDA) would definitely be a Cool Thing.

The best part is that you can have this kind of home automation today: the X10 protocol allows for the home automation part, and Linux and a piece of software called MisterHouse can take care of the rest. Until recently, X10 had mostly been used to turn lights on or off at some particular time, or from two rooms away. But new advances allow you to interface an X10 network to your Linux box via a serial port.

There are a series of handheld remotes available for X10 that use RF (radio frequency) instead of IR (infrared) waves. IR is typically used by standard TV remotes and by the Palm III (and up) -- but RF can pass through walls and generally has a much longer range than IR does. RF has other advantages as well, as I'll show later.

What's X10?

The X10 protocol defines a language for sending information over your existing power wiring. To take advantage of this, you need to have two main types of devices: senders (devices, such as a remote control, which transmit X10 information) and receivers (which turn the lights on or off or the like). Some devices may be able to act as both.

Each item in your house that you want to control has to have a house code (A-P) and an item code (1-16) -- which gives you a total of 256 devices that can be controlled.

Signals over electrical wiring degrade fairly quickly. As a result, X10 will work fine in an ordinary-sized house, but it probably won't in a particularly large home, and signals can't be counted on to travel between two buildings (e.g., from a house to a detached garage or workshop).


Getting started

First, you need to get the ActiveHome kit from X10.com. The cost is about $50 or so, depending on the deal of the day. The main things you'll need from the kit are the CM11a computer interface and a light fixture controller. Once your pack has arrived, you'll need to open the box, ooh and ahh, and then follow the included instructions to get it set up. The included instructions are for Windows 9x machines, but X10 has documented its protocol to talk to the CM11a so that you can write your own applications in whatever OS you want.

You may want to start up Windows (if you have it) to make sure the CM11a is working properly before setting up MisterHouse. Once you are sure it's functional, you can switch over to Linux and install the software. You should also make sure that you can turn the light on and off using the handheld remote.

So long as this acid test is passed successfully, we can move on to the software. Download your copy of MisterHouse (see Resources for links to this and other resources mentioned in this article).

After downloading the code, throw the zip file in some common location (I'm using /mh as an abbreviation for MisterHouse). Be sure to use the -a switch when you unzip, so that the files unzip in Unix format and perform the CR-LF translation for you.

The software has a nice Web-based interface to the CM11a, interfaces for text-to-speech (using Festival), and -- best of all -- it's written in my most favorite of all languages, Perl. The MisterHouse program comes with a detailed manual for programming X10 and other events, allowing anyone who knows Perl to write his own programs.

The package includes all other sorts of code for home automation, like getting a TV program listing or getting local weather information. If you have call waiting, you can hook that up as well, and discover the identity of your last few callers. All of this is available as part of the MisterHouse package through a browser interface (so long as your browser supports frames).

Assuming you unzipped into /mh , go to /mh/bin and chmod +x configure , then ./configure . This will remove some of the files that are required only for Windows use.

Edit the file /mh/bin/mh.ini for your particular settings. The file is self-explanatory and may change over time, so your best bet is to look at the file to see how you need to edit it. Probably the only thing I need to mention is that you definitely want to set the cm11_port entry to be the serial port that's connected to the ActiveHome interface.

Since MisterHouse is highly modular, you can start up the main program and have it search for modules in /mh/code/test/ . Check there for the kinds of modules available. For our testing, we'll use the test_x10.pl module, which changes the status of light A1 every 30 seconds (the default setting for the transceiver module).

To run the test, plug a light into the ActiveHome interface, and then plug the ActiveHome interface into the wall. Start the MisterHouse program from /mh/bin with ./mh test_x10.pl . Expect to get some nonsense messages across the screen as it reads and parses the configuration file. At the same time, it's also reading the modules you asked it to load (in our case, the test_x10.pl ). You'll know that the program has loaded when you see the lines:

Good code saved

Do nothing pa_stub: rooms=

After you see this message, know that light A1 will be turned off within 30 seconds. And that, 30 seconds later, the light will be turned back on. And so on and so on. By itself, this isn't all that interesting. In fact, it's downright annoying! So let's try and get tricky.


Show us the code

First, let's take a look at the code that makes up test_x10.pl , with the disclaimer that this is as it appeared as we went to print -- your mileage may vary:

# Category=Test

my $light_states = 'on,brighten,dim,off';
my $state;

$test_light1 = new X10_Item('A1');
$test_light2 = new X10_Item('B1');
$v_test_light1 = new  Voice_Cmd("Test light 1 [$light_states]");
set $test_light1 $state if $state = said $v_test_light1;

# Set up a Group
$test_lights = new Group($test_light1, $test_light2);
$v_test_lights = new  Voice_Cmd("Test lights [$light_states]");
set $test_lights $state if $state = said $v_test_lights;

# Toggle the light on/off every 30 seconds
if ($New_Second and !($Second % 30)) {
	my $state = ('on' eq state $test_light1) ? 'off' : 'on';
	set $test_light1 $state;
	my $remark = "Light set to $state";
	print_log "$remark";
#   speak $remark;
	}

# Respond if the A2 button is pushed
$test_button = new Serial_Item('XA2');
if (state_now $test_button) {
	my $remark = "You just pushed the A2 button";
	print_log "$remark";
	speak $remark;
	}

To download this code sample, please see the Resources section of this article for a URL.

In my frequent conversations with others about the value of object-oriented programming, I find few real-life examples of why this programming model is any good for the rest of us. Perl 5 added object-oriented programming capabilities, and MisterHouse makes full use of them. If you can read Perl code, you can see that we created two objects: $test_light1 and $test_light2 .

Our two test lights are defined as X10 items A1 and A2, respectively. With these objects defined, you can now see what object-oriented programming can do for you. All the code for turning on and off the lights is already defined by MisterHouse, along with other functions, such as timing. So we can now do things like toggle the light on and off -- which accomplishes our goal in a total of 7 lines (including comments and logging information). In 5 lines, we can monitor the status of A2 and find out if we did something with that code. Objects are excellent.


Navigating MisterHouse

Assuming that your browser and MisterHouse are on the same machine, fire up the former and take a look at http://localhost:8080/.

You should have access to the tk windows that will pop up, and see a screen similar to Figure 1 below. The leftmost of the 5 frames is the main listing, and the bottom two frames have the spoken text log and the printed log information. The top-left frame contains information specific to the menu item you've selected on the left (if you selected X10, this screen will show which X10 objects have been defined, along with the various states you can set, like on, off, +10, -10 , and so on). The top right window allows you to search for information. I like to use this last feature to search through a day's worth of TV data to tell me if a particular show is on that day (or any day).

Figure 1. Mister House control page


Finding out when X10 events happen is rather important, mostly because of an X10 item I have not mentioned yet. The Hawkeye is a motion/light sensor that really uses two X10 codes. One code is turned on when there's motion, the other code is activated by light.

One problem that I have solved with MisterHouse is that, when I get up at night to get a glass of water from the kitchen, I usually have to stumble around the bedroom, den, and kitchen itself. Unfortunately, I'm the kind of person that will wake up completely if the light is too bright.

With MisterHouse and a bit of coding, my problem is solved. I can have the motion sensor in the den watch for someone to pass through. If it sees someone, it can turn the lights on, then dim them to 50 percent brightness. After 5 minutes, it can have the lights turn off.

To make my system work even better, I've got it set to only do this between the hours of 11 p.m. and 5 a.m., so that it doesn't get confused with normal traffic. I don't have the sensor in my bedroom, because I don't want it to catch me tossing and turning.

# Category=X10

my $light_states = 'on,brighten,dim,off,-50';

$livlight = new X10_Item('A1');
$den_light = new X10_Item('A5');
$bedroom_light = new X10_Item('A3');

$den_sensor = new Serial_Item('XAJ', ON);
$den_sensor -> add ('XAK', OFF);

$den_sensor_unit = new Serial_Item('XAG', 'den');
print_log "Bedroom light off" if ('off' eq state_now $bedroom_light );
print_log "Den Light Off" if ('off' eq state_now $den_light );
print_log "Bedroom Light on" if ('on' eq state_now $bedroom_light );
print_log "Den Light on" if ('on' eq state_now $den_light );

if ('on' eq state_now $bedroom_light) {
	print_log "Turned on Bedroom Light";
}

$den_movement = new Timer();

# $Save{sleeping_parents}

if ((state_now $den_sensor eq ON) and ($Save{sleeping_parents})) {
	if (inactive $den_movement) {
		print_log "Den is inactive";
		}
	set $den_light 'on';
	set $den_light '-10';
	set $den_movement 600;
#	set $bedroom_light 'off';
#	set $bedroom_light '+10';
	print_log "Night on";
}

if (state_now $den_sensor eq ON) {
	print_log "Sensor tripped";
}

if (expired $den_movement) {
	set $den_light 'off';
#	set $bedroom_light 'off';
	print_log "Night off";
}


This translates, roughly, to:

If Hawkeye is tripped
AND ( time is after 11 p.m. and before 5 a.m. )
AND ( Hawkeye hasn't been tripped in the past five minutes )
THEN
	Turn on the den light
	Set den light to 50 percent brightness
	Turn on kitchen light
	Set kitchen light to 50 percent brightness
END

To download this code sample, please see the Resources section of this article for a URL.

This is an excellent example, by the way, of how object-oriented programming kicks in to make the code shorter than it would otherwise be.


Intruder alert!

Your limitless imagination and limitless Perl programming skills are all that you need to program MisterHouse to be the perfect cook, valet, secretary, entertainment planner -- and security guard. Here is sample code to get those creative juices flowing. In this scenario, MisterHouse's infrared sensors detect an intruder; your home's floodlights are activated so that your security Webcam can get a good photo of the intruder for your (and the police department's) files. The whole incident is, of course, logged.

Disclaimer: This code was written for this article and is not actually in production. We welcome fixes or improvements.

#Intruder detection and logging code.

#This is an example (untested) of using MisterHouse to keep an eye on
#your property.  This will take a picture using a quickcam and turn the
#device at house code A2 on.
#If the code itself isn't perfect, the logic should be correct.  Change
#the below code to suit your situation.  Possible changes include:
# - When this code should be active.  Set it up so that this is only
#   active when you're not home.
# - Email the captured images to you at work.
# - Turn on a microphone and start a voice-over-IP connection so you
#   can hear what's going on
# Category=X10

my $light_states = 'on,off';

#define the lights and sensor codes.
$outside_light = new X10_Item('A2');

$intruder_sensor = new Serial_Item('XAJ', ON);
$intruder_sensor -> add ('XAK', OFF);

$intruder_sensor_unit = new Serial_Item('XAG', 'den');

#Set up a timer event (we use this later on)
$intruder_movement = new Timer();

if (state_now $intruder_sensor eq ON) {
	#Turn on the outside light
	set $intruder_light 'on';
	#Pull the time out of localtime().  You could use Date::Time for this
	#as well, and it's probably easier.
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	$date= $hour.$min.$mday.$mon.$year;
	#Pull the image using qcread (http://www.fhttpd.org/pub/qcread)
	@args = ("qcread", "> /logs/".$date);
	system (@args);
	#Start a 5 minute timer.  After that, we turn the lights off
	#unless the intruder is still outside.
	set $intruder_movement 600;
	#Log the event
	print_log "Sensed movement outdoors.  Picture taken for logs";
}

if (expired $intruder_movement) {
	#After 5 minutes (600 seconds) of inactivity, turn the lights off.

	set $outside_light 'off';
}

To download this code sample, please see the Resources section of this article for a URL.

As I mentioned above, there is more than X10 information that you can use with MisterHouse. Among other things, it can:

  • Download things like weather and Letterman's top 10 lists on demand from the Internet. If you're not on a permanent connection to the Internet, you can have MisterHouse do the dialing for you.
  • TV information. Not only can you get information for your local broadcast, cable, or satellite stations, you can customize the channel listing and other information. The data for this is actually pulled from Clicktv.com, so you'll need to create a free account to use this.
  • More TV information. Find out which of your favorite shows are on tonight, tomorrow, or next week by entering your list of favorite programs, then get a notification every half hour if one of your favorite shows is starting. Never miss Red Dwarf again!
  • Slashdot news. Need I say more? Wake up in the morning to the most important stuff that matters and got posted in the night.
  • Local weather information (temperature, wind speed, and so forth). This requires the use of some more expensive equipment (namely, a WM918 from Radio Shack, which costs about $200 to $300). On the upside, it reports temperature, humidity, barometric pressure, and wind speed and direction. All of this information can be graphed to provide a history of weather data.
  • Text-to-speech (TTS) using the Festival TTS processor. You can have your computer talk to you. At the time of this writing, speech-to-text (STT) works only on the Windows platform -- but with IBM's ViaVoice being ported to Linux, it probably won't be long before you see that functionality under Linux as well. The code for speaking is actually integrated into MisterHouse, so that if you have a TTS processor, you'll get the speech, and if you don't have a TTS processor, the text gets sent to a log file that can be monitored (as will the speech that would normally get translated into text via STT). Commands like "turn on the bedroom light" can either be interpreted from the STT processor or listed as a command on the Web site.
  • Generic serial interfaces. If you have a device that isn't supported under MisterHouse, and it has serial input, you can get the data from within MisterHouse and work from there. The example given in the MisterHouse features page uses MisterHouse and a ham receiver to track the position of a car using GPS (Global Positioning System) and a ham radio transmitter. Thus, your house even knows where your car is.

From what's included to what's possible to program in, the features of MisterHouse are almost limitless. The only thing stopping you is your imagination -- and if you have none, you need only to wait for new functionality to be announced on the MisterHouse mailing list. Among other projects, these interesting ideas have recently been discussed on that list:

  • Using iButtons (Java-based rings or key fobs) for security
  • Using a TINI board for remote monitoring of serial ports
  • Controlling WinAmp using a standard IR remote control (That would be for the Windows version, of course.)

What will you use MisterHouse for? Do please let me know via email. And don't forget to submit your controllers back to the MisterHouse team, so that your code can benefit everyone in the community.

Reprinted with permission from LinuxWorld magazine. Copyright Web Publishing Inc., an IDG Communications company.

Resources

About the author

Mark Komarinski is a columnist for LinuxWorld magazine.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Linux
ArticleID=10981
ArticleTitle=Home, smart home
publish-date=12011999
author1-email=mark.komarinksi@linuxworld.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers