BUY THIS BOOK
Add to Cart

Print Book $29.95


Add to Cart

Print+PDF $38.94

Add to Cart

PDF $23.99

Safari Books Online

What is this?

Add to UK Cart

Print Book £20.95

What is this?

Looking to Reprint or License this content?


Home Hacking Projects for Geeks
Home Hacking Projects for Geeks

By Eric Faulkner, Tony Northrup
Book Price: $29.95 USD
£20.95 GBP
PDF Price: $23.99

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Automate a Light
  • An X10 wall switch
  • An X10 motion detector
  • Two AAA batteries
  • An X10 controller (optional)
  • Replacement faceplate (optional)
  • Circuit tester
  • Screwdriver
  • Wire nuts
For a list of specific parts used in this project, refer to Exhibit A at the end of this chapter.
If you live in a home that's more than 20 years old, there's a very good chance that you have light switches in odd places, or that you need to cross a room in order to turn on a light. In my case, I have to walk across a dark and frighteningly cluttered basement, so I often end up leaving the light on and wasting energy. If I could automate this light switch so that it turns on when I enter the room and turns off when I leave it, I would save myself both money and frustration.
Figure 1-1 illustrates the project's simple design, which includes a motion detector and a control mechanism. The motion detector is triggered simply by someone entering the room, and will then send a signal telling the control mechanism (an intelligent light switch) to turn on. Just as when you manually flip a light switch, the intelligent light switch will close a circuit and provide electricity to the light.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Project Overview
This simple project will introduce you to basic concepts of electricity, wiring, and the X10 home automation communications protocol by replacing a wall switch to allow for remote control using X10. While the new switch could be controlled from anywhere in the house using a computer, remote control, or other device, you'll be automating it using a motion detector in the same room.
You will start by replacing the existing wall switch with an X10 wall switch. This is a fairly easy change, but the wiring can be complex, and you must follow safety precautions. You will then install an X10 wireless transceiver, which receives wireless signals from the X10 motion detector and sends commands across your home's power lines to the X10 wall switch. Finally, you will configure and position the wireless X10 motion detector. When you're done, the motion detector will trigger the light when you enter a room by relaying both wireless and wired X10 signals, as illustrated in Figure 1-2.
Figure 1-2: Both wireless and wired X10 signals are used to turn a light on when motion is detected.
This project does not require a computer. However, later projects will connect your computer to both the switch and the module and allow you to integrate both into complex applications.
X10 is a communications protocol, similar to network protocols such as TCP/IP. However, X10 works across home power lines and is extremely low-bandwidth. X10 devices send about one command per second, and the commands are as simple as "Device A1: Turn on". These commands require less than 1/1000 the bandwidth of a dial-up connection. Like a broadcast network, every command is sent through every wire in your house; it's up to each individual device to decide whether it needs to respond to a particular command.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Light Switch
In the first phase of this project, you will replace the existing conventional wall switch with a one-way X10 switch. The new switch is capable of receiving, but not sending, X10 commands. One-way X10 switches that can receive X10 commands may be turned on, turned off, or dimmed by an X10 transmitter. In this project, the X10 light switch will receive signals from a wireless X10 motion detector. Later projects will show you how X10 can be used to control almost any system in your house. For example, flipping a two-way X10 switch could dim the lights and play your favorite MP3s.
The light switch is the only piece of hardware required for the control mechanism component of this project. It will receive signals from the motion detector and apply power to the light.
First things first: you need to use care every time you interact with your home's electrical systems. Even though replacing a wall switch is much simpler than adding a new electrical circuit, there are still risks. You will be exposing high-voltage wires, so be sure that power is disconnected before you remove the existing switch. You must also take care when wiring to ensure that all connections are solid and there are no exposed wires.
In this step, you will disconnect power to the switch that you will be replacing. First, you should test the switch or outlet to verify that it is working properly. That way, if it's not working later, you know that it's because of a recent change you made. Turn the switch on and make sure that the light or outlet it controls works properly. If the switch controls an outlet, plug a radio into it and turn the volume up loud enough so you can hear it shut off when you've found the right circuit breaker. Warn the other people in your household that you're about to turn off the power at the circuit breaker—you don't want someone discovering that the lights are out, and then reconnecting the power while you're in the process of wiring—and place a piece of tape over the circuit breaker so that everyone knows the switch was intentionally flipped.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Motion Detection
In this second phase of the project, you will configure a motion detector to send signals to the light switch you installed in the first phase. However, the motion detector you're using can't communicate directly to the switch because it's wireless. Therefore, you now need to install a wireless transceiver to receive the signals from the motion detector and place them onto the power line where the switch can process them. This component of the project is the interface between the human and the light switch.
In this step you will configure an X10 wireless transceiver (also known as an RF receiver) and connect it to an outlet, as shown in Figure 1-9. The wireless transceiver receives X10 wireless commands from the X10 motion detector and transmits those commands over the power lines.
Figure 1-9: An X10 wireless transceiver is required to transfer signals from the motion detector to the light switch.
First, configure the house code of the wireless transceiver. Use a screwdriver to twist the dial to set the house code to C. The transceiver will respond to wireless signals only from other devices using the same house code. For example, if you later configure other devices using the E house code, the wireless transceiver will not respond to them.
If the transceiver allows another device to be plugged into it, it probably has a dial, a switch, and a button. These allow you to control the lamp or other device plugged directly into the transceiver, and do not affect how the transceiver passes on wireless communications.
Connect the wireless transceiver to an electrical outlet in a central location within your house. If it seems unreliable, try moving it closer to the motion detector. For best results, plug the transceiver directly into an outlet, not into a power strip, surge protector, or UPS.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Wrapping Up
Congratulations on taking the first step towards an automated home! Now, before you spend hundreds of dollars automating every device in your house, live with the motion detector for a few days. Think about how you use it, and what annoys you about it. What do the other people in your household have to say about the new switch? If it bugs you at times, chances are good you can fix it by connecting your computer to the X10 network and writing a simple script.
To determine if the project was worthwhile, disable the motion detector after about a week. Does anyone miss it?
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Extensions
The most basic and common use of X10 controllers is to turn lights on and off. However, X10 is extremely flexible, and the wide variety of X10 devices allows you to remotely control everything from dryers to window blinds to your home's thermostat. Here are some other common uses for X10:
  • If you have a switch in an illogical place, replace it with an X10-controlled switch and add a wireless controller to the wall where the switch should be.
  • If want a switch to control multiple electrical outlets, replace it with a two-way X10 controller and add lamp modules to allow it to control remote lamps.
  • If you hate getting up in the middle of the night to turn off a light somewhere in the house, replace all your light switches with X10 switches and use the All Lights Off switch on your controller.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exhibit A: Bill of Materials
The following list shows the parts that I purchased specifically for this project, though you can choose to substitute parts from other manufacturers. All parts are available from large hardware stores or home-automation web sites. The part numbers listed can be referenced at http://www.smarthome.com.
Item
Quantity
Approximate cost
Part number
X10 wireless transceiver
1
$20 and up
4005X or 400s
X10 motion detector
1
$20 and up
MS14A-C or MS13A
X10 switch
1
$13 and up
2031 or similar
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Automate Your Porch Light
  • A load-bearing X10 wall switch
  • An X10 motion detector (preferably an outdoor model)
  • X10 Powerlinc Serial Controller
  • Four AAA batteries
  • An X10 controller (optional)
  • Replacement faceplate (optional)
  • Circuit tester
  • Screwdriver
  • Wire nuts
For a list of specific parts used in this project, refer to Exhibit A at the end of this chapter.
I hate unlocking my front door in the dark. I'm never sure if I have the right key in my hand, if it's turned the right way, or where the keyhole is. The worst part is that I have a porch light right above the door. I can't unlock the door without the light, but I can't get to the light without unlocking the door. So, the light switch waits for me on the other side of the door, quietly mocking me. I'll teach that light to laugh at me.
From now on, porch light, I'm the boss, and these are the rules. You will:
  • Turn on at dusk.
  • Turn off at midnight.
  • Turn on the entryway lights inside the house when someone approaches the door after dusk but before midnight.
  • Turn on both the porch light and the floodlights when someone approaches the door between midnight and 6 A.M.
For the project to accomplish all this, we'll need a logic mechanism that can determine when to turn the lights on and off, and keep track of their state. The logic mechanism will control the light switch and receive input from a timer, motion detectors, and conventional light switches. You can buy or build standalone logic mechanisms, but a computer is the simplest and least expensive way to control everything. We'll also need a light switch that can be remotely controlled by the computer, and a motion detector that can tell our computer when someone approaches the door. Figure 2-1 shows the components of this project.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Project Overview
This project builds on Chapter 1, Automate a Light, by connecting a Microsoft Windows or Linux computer to your X10 network. This project includes simple scripts for controlling lights based on feedback from the X10 motion detector and the computer's clock. The computer's role is fairly simple; however, once the computer is connected to X10 and you understand how to control it using scripts, you can do much more exciting things than simply turning lights on and off.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Light Switch
The software you write in this project will turn lights on and off based partially on information provided by motion detectors and the current time of day. To enable your computer to control the lights, you must first install X10-capable light switches on all relevant lights (in this case, the porch light, the floodlights, and the entry light). You must then install one or more motion detectors. These will send a wireless X10 signal that will be forwarded to your computer when someone approaches, allowing your computer to make an intelligent decision about turning lights on. Finally, you must connect your computer to your X10 network using an X10 computer interface.
I should mention at this point that X10 isn't terribly reliable, and if you implement it extensively, it can get quite expensive. I would love to provide an alternative to using it, but I couldn't find another affordable way to enable a computer to control lights all around the house that didn't require running wires through the walls. With that said, the first step in implementing X10 control over your lighting for this project is to replace the existing switches for the porch light, floodlights, and interior light that will be computer-controlled. Each light will be independently controlled, and will therefore require a unique X10 address. For this project, we'll use the addresses in Table 2-1. Naturally, you may use different addresses, but you will need to adjust the addresses in the X10 script provided in Exhibit B.
Table 2-1: Suggested X10 addresses for the lights
Device
Address
Porch light
B1
Floodlights
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Motion Detection
The light switch is an admirable user interface. It's simple, requiring only the flick of a finger, and everyone knows how to use it. But motion detectors are even simpler, because they provide an entirely passive user interface. To control something via a motion detector, all you have to do is be present. In this phase of the project, you'll install a motion detector that connects to your X10 power network.
As described in Chapter 1, X10 wireless motion detectors will be used for this phase. The difference in this case is that the motion detector will be positioned to detect motion outside of the house.
We'll use C9 as the motion detector's primary address. This is the address the motion detector will transmit an ON signal to when motion is detected, and an OFF signal to when no motion has been detected for the time-out period. The motion detectors in this project can communicate when several different events occur, including dawn and dusk; for example, we need to detect dusk to turn the porch light on in the first place. By default, the X10 motion detectors I am using transmit a code one unit higher than its primary address, so in this case it will transmit an ON signal to the address C10.
Table 2-2 lists all the X10 addresses the motion detector will use when the primary address is set to C9. Again, you're free to use different addresses, but you will need to adjust them in the X10 script in Exhibit B. Note that although it's not used in this project, the motion detector transmits an OFF signal at dawn to the address one unit higher than its primary.
Table 2-2: Suggested X10 addresses for the motion detector
Event
Signal
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Logic and Timer
The intelligence for this project will be provided by a script you create and run on your computer, such as the one in Exhibit B. This script (the logic component) will receive input from the computer's internal clock and motion detectors. It will then issue commands to the control mechanism to turn the porch light on or off.
For the script described in this section to work properly, you should have a serial port on your computer and you should leave your computer running at all times.
Don't worry if you're not much of a programmer—setting up this script requires minimal understanding of scripting. The script has a few dependencies that need to be set up first: the X10 computer interface, the Perl scripting language, and some modules that add home automation functionality to Perl. After you configure these dependencies, you will type or download the provided home automation script, test it, and then schedule it to run automatically when your computer starts.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Understanding the Script
Unless your needs match mine exactly, you'll probably end up modifying the script to make it do what you want. There's a lot of code in that script, but the most interesting lines appear within the infinitely running while loop and the two subroutines. Everything before the while loop initializes variables, modules, and the serial port. This section of code contains variables that you may need to modify if you have used different house or unit codes:
	my $lights_hc = "B";
	my $porch_light_unit = "1";
	my $floodlight_unit = "2";
	my $interior_light_unit = "3";
	my $motion_hc = "C";
Table 2-3: Variable meanings for the x10-controls.pl script
Variable
Description
$lights_hc
The house code for your lights.
$porch_light_unit, $floodlight_unit, $interior_light_unit
The unit codes for your lights.
$motion_hc
The house code for your motion detector and wireless transceiver.
$porch_motion_unit
The unit code for your motion detector.
$porch_motion_on
The four-byte command string that the motion detector sends when it detects motion. This is created using the previously defined variables; by default it is "C9CJ".
$porch_motion_off
The four-byte command string that the motion detector sends when it hasn't detected motion for the time-out period. By default it is "C9CK".
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Wrapping Up
Armed with only an X10 switch, an X10 controller, a computer, and a Perl script, I have turned a conventional porch light into an intelligent porch light (Figure 2-7). I can still turn it on and off from the switch, but I rarely need to because my Perl script meets my needs perfectly.
You probably have a bunch of ideas about how you want your new light to behave, but before you go too crazy, just live with the intelligent light for a week or so. You'll then have a better sense of how you want to tweak the script or rearrange the setup to better suit the needs of your home. Some specific things to think about are:
  • Does the light turn on when nothing is there, or not turn on when you need it? If so, you may need to move the motion detector.
  • Does the dusk detection of the motion detector work as you hoped? If not, you might be better off turning the light on at a specific time of day. This would require you to modify only a couple lines of the script.
  • Has your electrical bill changed as a result? If it's changed for the worse, you might want to tweak the behavior to reduce the time the light is on.
  • Does the script run reliably? If it fails because your computer is often offline, you might need to run it on a dedicated computer. You can use really, really low-end computer hardware to run this script, and the software is entirely free if you use Linux, so the dedicated computer could actually end up costing less than the X10 parts!
To determine if the project was worthwhile, stop the script. Do the other people in your household miss it?
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Extensions
This project shows you the potential of the X10 computer interface to enable extremely intelligent lighting. It's only the beginning, though. Here are a couple ideas of how to extend the project:
  • Create a log of all motion detected, day or night.
  • Set up a camera and take a snapshot or video of people who approach the house.
  • Automatically ring the doorbell when someone approaches.
  • Create different schedules for different days of the week.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exhibit A: Bill of Materials
All parts are available from large hardware stores, or home automation web sites such as http://www.smarthome.com.
Item
Approximate
Part num-
Serial Powerlinc computer inter- 1
$35 and up
CM11A
X10 wireless transceiver 1
$20 and up
4005X or 400s
X10 motion detector 1
$20 and up
MS14A-C or MS13A
X10 switch 1
$13 and up
2031
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exhibit B: Script Source Code
	#!/usr/bin/perl
	use lib './blib/lib','./lib';

	# Configure user-modifiable codes. "hc" means "housecode"
	my $lights_hc = "B";
	my $porch_light_unit = "1";
	my $floodlight_unit = "2";
	my $interior_light_unit = "3";
	my $motion_hc = "C";
	my $porch_motion_unit = "9";

	# These variables just increase readability
	my $porch_motion_on = $motion_hc.$porch_motion_unit.$motion_hc."J";
	my $porch_motion_off = $motion_hc.$porch_motion_unit.$motion_
	hc."K";
	my $dusk_detected = $motion_hc.($porch_motion_unit + 1).$motion_
	hc."J";
	my $dawn_detected = $motion_hc.($porch_motion_unit + 1).$motion_
	hc."K";

	my (@before, @now, $dusk, $night, $porch_light_on);
	my ($floodlight_on, $interior_light_on, $data);
	my ($OS_win, $serial_port);

	# Load the proper SerialPort module based on platform
	BEGIN { $| = 1;
	   $OS_win = ($^O eq "MSWin32") ? 1 : 0;
	   if ($OS_win) {
			 eval "use Win32::SerialPort";
			 die "$@\n" if ($@);
			 $serial_port = Win32::SerialPort->new ("COM1",1);

		}
		else {
		     eval "use Device::SerialPort";
			 die "$@\n" if ($@);
			 $serial_port = Device::SerialPort->new ("/dev/ttyS0",1);
		}
	}
	die "Can't open serial port: $^E\n" unless ($serial_port);
	$serial_port->error_msg(1);
	$serial_port->user_msg(0);
	$serial_port->databits(8);
	$serial_port->baudrate(4800);
	$serial_port->parity("none");
	$serial_port->stopbits(1);
	$serial_port->dtr_active(1);
	$serial_port->handshake("none");
	$serial_port->write_settings || die "Could not set up port\n";

	use ControlX10::CM11;
 
	while () {
		# If it's midnight, turn the lights off
		@now = localtime;
		if (($now[2] == 0) && ($now[2] != $before[2])) {
			   print "Turning porch lights off because it is midnight.\n";
			   light_off($porch_light_unit);
			   $dusk = 0;
			   $night = 1;
		}
		@before = @now;

		# Grab the data from the X10 controller
		$data = $data.ControlX10::CM11::receive_buffer($serial_port);

		# Motion detected on porch.
		# Turn on porch and interior light, and possibly floodlight
		if ($data =~ $porch_motion_on) {
			   print "Porch motion detected. Received: $data.\n";
			   $data = "";
			   light_on($porch_light_unit);
			   light_on($interior_light_unit);

			   # It is between midnight and dawn, so turn on the floodlights
			   if ($night) {
				  light_on($floodlight_unit);
			   }
		}

		# Motion detector on porch timed out. Turn off lights if necessary
		if ($data =~ $porch_motion_off) {
			   print "No porch motion detected. Received: $data.\n";			   # Turn off floodlights
			   if ( !$floodlight_on ) {
				  light_off($floodlight_unit);
			   }

			   $data = "";
			   # Turn off porch and interior lights if it's not dusk.
			   if ( !$dusk && !$porch_light_on ) {
				  light_off($porch_light_unit);
			   }

		if ( !$interior_light_on ) {
			   light_off($interior_light_unit);
		}
	}

	# Dusk was detected. Turn on porch lights.
	if ( $data =~ $dusk_detected ) {
			print "Dusk detected. Received: $data.\n";
			$data = "";
			light_on($porch_light_unit);
			$dusk = 1;
	}

	# Dawn was detected. Turn off porch lights.
	if ( $data =~ $dusk_detected ) {
			print "Dawn detected. Received: $data.\n";
			$data = "";
			light_off($porch_light_unit);
			$night = 0;
			$dusk = 0;
	}

	# If someone manually turns the controlled lights on or off, makenote of it
	if ( $data =~ ($lights_hc.$porch_light_unit.$lights_hc."J") ) {
			$porch_light_on = 1;
			$data = "";
	}
	if ( $data =~ ($lights_hc.$floodlight_unit.$lights_hc."J") ) {
			$floodlight_on = 1;
			$data = "";
	}
	if ( $data =~ ($lights_hc.$interior_light_unit.$lights_hc."J") ) {
			$interior_light_on = 1;
			$data = "";
	}
	if ( $data =~ ($lights_hc.$porch_light_unit.$lights_hc."K") ) {
			$porch_light_on = 0;
			$data = "";
	}
	if ( $data =~ ($lights_hc.$floodlight_unit.$lights_hc."K") ) {
			$floodlight_on = 0;
			$data = "";
	}
	if ( $data =~ ($lights_hc.$interior_light_unit.$lights_hc."K") ) {
			$interior_light_on = 0;
			$data = "";
	}
}

# Release the serial port
$serial_port->close || die "\nProblem closing serial port\n";
undef $serial_port;

sub light_on {
	 print "Turning on: ".$lights_hc.$_[0]."\n";
	 sleep 1;
	 ControlX10::CM11::send($serial_port, $lights_hc.$_[0]);
	 ControlX10::CM11::send($serial_port, $lights_hc."J");
}
sub light_off {
	 print "Turning off: ".$lights_hc.$_[0]."\n";

	sleep 1;
	ControlX10::CM11::send($serial_port, $lights_hc.$_[0]);
	ControlX10::CM11::send($serial_port, $lights_hc."K");
}
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exhibit C: Software Versions
Description
Version
Operating System
Windows XP SP1
Mandrake 9.1
Red Hat 9.2
Perl
5.8.0
ControlX10::CM11
2.09
Win32::SerialPort
0.19
Device::SerialPort
0.22
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: Remotely Monitor a Pet
  • Three X10 lamp modules or wall switches
  • Three indoor X10 motion detectors
  • Three wireless cameras
  • A wireless video receiver (compatible with wireless cameras)
  • An X10 wireless transceiver
  • X10 Powerlinc Serial Controller
  • Eight AAA batteries
  • A stable Internet connection that allows inbound connections (broadband helps)
  • A computer running Linux
  • A video capture card with composite video input
  • Sticky poster adhesive
  • A screwdriver
For a list of specific parts used in this project, refer to Exhibit A at the end of this chapter.
My wife, Erica, is an overprotective mother. We don't actually have any kids, though—just a cat. Sammy's not even the kind of cat that you really need to worry about; he's healthy, young, and built like a goat. But Erica still worries. Erica worries so much that we can't take an overnight trip without spending half the time wondering if the cat is okay.
Erica and I got married a couple of years ago and went on a honeymoon cruise. We hired a pet sitter to come and feed the cat on a daily basis, and to reassure Erica that everything was okay, I set up a cheap webcam and some motion detection software. It worked terribly.
Of course, it was still better than nothing—we were able to connect to the Internet from the very slow satellite link on the ship, and browse through a directory of webcam pictures to verify that Sammy was alive and that the pet sitter was feeding him. However, the motion detection software was very unreliable. The video from the webcam had so much noise that the motion detection software triggered almost constantly. It would even take pictures throughout the night, when there was total darkness and the camera couldn't capture an image. Worst of all, I actually
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Project Overview
This project builds on the preceding projects by leveraging X10 switches, motion detectors, an X10 computer interface, and a custom Perl script. It also introduces several new components: wireless cameras, a video capture card, photo album software, and the Apache Web server software. Note that this project runs only on the Linux platform—just about any major Linux distribution should do fine.
The first step in this project is installing the hardware. If you want to be able to view pictures after the sun goes down, you'll need to install X10 light switches or modules to provide additional lighting for the cameras when a picture is going to be taken. An additional benefit of this is that it helps to simulate activity in the house—as your pet moves from camera to camera, the lights in different rooms will turn on, and your home will seem occupied (by more than a cat).
You'll then need to place three X10 cameras around your house. These wireless cameras transmit on the 2.4 GHz frequency, the same frequency used by 802.11b and 802.11g networks. The cameras don't seem to have any impact on the performance of my wireless network, though. Only one camera can transmit at a time, and fortunately, they are smart enough to turn themselves off when a different camera turns on. The cameras also turn off when there's no motion, so if they do interfere with your network, it will be only occasionally.
The three motion detectors will be installed in the rooms where the cameras are. When a motion detector is triggered, it will send an X10 signal that will be processed by three separate devices: the wireless camera in the same room, the light in the room, and the X10 computer interface (and the Perl script). This has the effect of changing the video feed into our video capture card to the active camera, turning on a light, and notifying our script that it's time to take a picture.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Motion Detection
You want to take a picture only when your pet is active, so you need to know when your pet is moving and which room he is in. As you're probably well aware of by now, this feat can be easily accomplished using motion detectors.
For this project, you have two main considerations when choosing a motion detector: it needs to connect to your Linux computer (which is located in a different room) and it needs to be cheap. After looking around on the Web, I decided the best option was a wireless X10 motion detector. They cost only $20 each, they're wireless and battery powered, and a Linux computer can intercept their signals using a simple script.
The webcams you'll be using in the Image Capture phase of this project can't see in the dark. In fact, they produce terrible pictures in anything less than bright sunlight. So, if you want to capture pictures of your pet moving around in the evening or in a room without bright natural light, you'll need to add some X10-enabled lighting.
If you've already replaced all the light switches in your house with X10 light switches, or if you're using night-vision cameras, you can skip to the next step. If not, and you want to capture pictures 24 hours a day, you'll need to replace the light switches in the relevant rooms with X10-enabled switches. For detailed instructions, refer to the "Replace the switch" section of Chapter 1.
If you don't have built-in lighting in the room where you plan to put the camera, or if you just want to avoid wiring, you can use X10 lamp modules. To configure a lamp module, follow these steps:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Image Capture
In this phase, you will configure the image-capture and archiving mechanism. This is the most complex phase of the project, and involves configuring wireless video cameras, a Perl script, and a handful of supporting tools.
Unlike traditional webcams, the wireless video cameras you'll be using for this project do not connect to your computer's USB port. They're actually intended to connect to a TV or VCR, so you'll need to add an old-timey analog video input interface to your computer. Fortunately, these are cheap and well supported by Linux. After all, lots of people use video capture cards to record TV shows, so quite a bit of energy has gone into making sure the software works well.
The wireless receiver has a composite connection, so make sure the card you choose supports that. I used the Hauppauge WinTV-GO-FM card just because I happened to have it lying around. This card uses the BT878 chipset, which is well supported by Linux; any card with that chipset should work equally well. There's no need to get an expensive video capture card, as it's not going to improve the performance or quality of your pet monitor, and you don't need sound support because the wireless cameras don't have microphones. Bottom line: don't spend more than $50 on your video capture card.
Once you've got your card, install it according to the manufacturer's instructions. Generally, this is as simple as turning off your computer, opening the computer case, and inserting the card into an empty PCI slot. Once Linux starts, install the drivers for the card. For more detailed instructions, refer to "Install the TV tuner card" in Chapter 7.
You're now ready to install the three cameras around your house. When positioning the cameras, follow these guidelines:
  • Position the camera as close as possible to the place where your pet will be.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Understanding the Script
Unlike in some of the other projects, the script in this project will work for most people without any modification. However, you could extend it a great deal if you wanted to, so I'll describe what the different sections of code do and what the variables are. There's a lot of code in this script, but the most interesting lines appear within the infinitely running while loop and the subroutines. Everything before the while loop initializes variables, modules, and the serial port. This section of code contains variables that you may wish to modify if you have used different house or unit codes:
	my $motion_hc = "C";
	my @motion_ucs = ("2", "3", "4");
	my $album_path = '/var/www/cgi-bin/ids/albums';
	my $webcam_config = '/etc/petcam.config';
	my $serial_port = Device::SerialPort->new("/dev/ttyS0",1);

	my $min_bright = 0.07; # Minimum brightness acceptable
	my $min_color = 0.15;		# Minimum brightness before forcing B&W
	my $min_raw = 0.25;		# Minimum brightness to not equalize
	my $apix = 200;			# Number of pixels to analyze (squared)
	my $crop = 10;			# Number of pixels to crop off each side
If you were creating a script that would be used by many people, you could store that information in a separate file or pass it to the script using command-line parameters. For a hack that runs only in your house, however, it's easiest to store that information directly in the script itself. Table 3-1 describes the meanings of the different variables used in the script.
Table 3-1: Variable descriptions for the pet-monitor.pl script
Variable
Description
$motion_hc
The house code for your motion detectors, wireless transceiver, wireless cameras, and lights.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Image Retrieval
The last phase of this project will enable you to retrieve your pictures from across the Internet. You will set up a web server on your computer, configure your software and hardware firewalls to forward web traffic, and finally install IDS.
Many of the projects in this book rely on being able to remotely access your computer. For example, in order to monitor your home security system while on vacation, you'll need to be able to pull up a web browser at an Internet café and connect to your home computer. Connecting your home system to the Internet has a lot of potential: you could fire up your air conditioning if you're coming home early from work; you could listen to your home music collection over the Internet; or you could even schedule a show to record from your desk at work if you forget to set the VCR.
To enable this, you need to harden your system and then install and configure web server software. If you're an Internet geek, you probably already know how to do this. If you're some other type of geek, though, you may need some guidance. This section will teach you the basics of self-hosting a web server, including understanding DNS, dynamic IP addressing, and security. As in previous chapters, all the software and services are free, so you won't have to spend a dime!

Section 3.5.1.1: 1. Harden your system

Large-scale attacks on Linux web servers are not as common as those on Windows web servers, but there are still a few widespread worms that will infect an unpatched Linux web server connected to the Internet. And of course, your computer isn't even safe if it's not a server—worms and viruses infect client-only PCs all the time. Nonetheless, before you set your system up as a server, you should mentally acknowledge that you're increasing your vulnerability. That said, I'll do my best to make sure you connect your system to the Internet safely.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Wrapping Up
In addition to allowing you to enjoy your vacation without worrying about your pet, this project has accomplished several other useful things:
  • Implemented intelligent, wireless video monitoring throughout your home.
  • Connected your home (and your pet) to the Internet.
  • Enabled remote monitoring of your home.
  • Enabled your computer to respond to movement throughout your house.
If you've positioned the motion detectors and cameras well, you can safely leave the script running even when you're at home, though of course you'll want to change the X10 settings on your lights so that they're not turned on and off by motion. Remember to keep an eye on your disk space, though, especially if your cat turns out to be active just about the whole night. (Even though I can't see pictures taken at night without the lights, it's interesting to use the motion detectors to track my cat's movements.)
Additional content appearing in this section has been removed.
Purchase this book now or read it o