Part I: Home Automation
(when the script creates a new fold-
er). So, to make sure everything is
working correctly, grab one of your
motion detectors and press the
“House/On” button. You should see
output that resembles the screenshot
shown in Figure 3-7.
You’ll probably see the script take
a picture immediately because you
triggered the motion detector since
connecting the X10 computer inter-
face. The X10 computer interface
keeps the last few messages in a
If things don’t work as expected, try moving your X10 computer interface and the
wireless transceiver to different electrical outlets. You might even try connecting
them to outlets right next to each other.
10. Schedule the script
Your script relies on your computer running. You dont want to have to
remember to restart the script each time the computer restarts, so you
should configure your script to run automatically at startup. To do this,
execute the following commands:
echo perl /home/
username/homehacking/pet-monitor.pl >> /etc/rc.d/
This adds the Perl command to run your new script to the rc.local file,
which Linux automatically executes at startup. Restart your system, and
the script will automatically run and snap pictures when your pet moves.
You’re done!
If you discover that the script doesn’t work when started automatically, you may
need to grant additional rights to the /var/www/cgi-bin/ids/ directory and its
subdirectories. To do this, su to root, and then run the command chmod 666
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
Figure 3-7. The output from the script lets
you know that it’s working.
Understanding the Script
hhpg.indb 56
11/24/2004 1:54:30 PM
Chapter 3, Remotely Monitor a Pet
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 com-
mand-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
The house code for your motion detectors, wireless transceiver,
wireless cameras, and lights.
The unit codes for your motion detectors, wireless transceiver,
wireless cameras, and lights.
The path to your albums IDS directory.
The path to the configuration file that the webcam tool requires.
This will be passed as a parameter to
A reference to the serial port device that must be provided to the
ControlX10 module when communicating with the X10 computer
The minimum image brightness that the script will accept.
Anything below this threshold will probably be too dark to see
anything. Possible values are between 0 and 1; a value of 0 will
save all images. If you find that the script is reporting values
greater than 1, change this value to 7.
The minimum image brightness (between 0 and 1) that the script
will save as a color picture. Anything below this threshold will
be saved as black and white. The wireless cameras have a great
deal of noise at low light levels, which is especially apparent after
equalization. Desaturating the picture makes the image a bit clear-
er, though at these levels the image will still appear very grainy.
Understanding the Script
hhpg.indb 57
11/24/2004 1:54:30 PM

Get Home Hacking Projects for Geeks now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.