247
Chapter 11, Keyless Entry Welcome Home
Exhibit A: Bill of Materials
Item Quantity Part number
KwikSet TitanOne keyless remote
deadbolt lock
1 Model 886
X10 keychain remote 1 Smarthome #4004
X10 plug-in RF base 1 Smarthome #4005X
X10 CM11A PC interface 1 Smarthome #1140
X10 lamp modules Optional, as needed Smarthome #2000
X10 wall switches Optional, as needed Smarthome #2031
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 $entry_lights_unit = "4";
my $kitchen_door_lights_unit = "5";
my $downstairs_hallway_lights_unit = "6";
my $uptairs_hallway_lights_unit = "7";
# Events generated by door lock
my $events_hc = "A";
my $door_lock_event_unit = "2";
# Key hours of the day (24 hour time format)
my $daytime_hour = "8";
my $evening_hour = "18";
my $bedtime_hour = "22";
# Door lock events
my $door_locked_event = $events_hc.$door_lock_event_unit.$events_
hc."K";
my $door_unlocked_event = $events_hc.$door_unlock_event_unit.$events_
hc."J";
my ($door_locked, $data)
my (@before, @now);
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);
}
Exhibit B
hhpg.indb 247
11/24/2004 1:56:23 PM
248
Part III: Home Security
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 () {
# Store the current time
@now = localtime;
# Grab the data from the X10 controller
$data = $data.ControlX10::CM11::receive_buffer($serial_port);
# Door lock event detected.
# Locking...
if ($data =~ $door_lock_event) {
print "Door locking event detected. Received: $data.\n";
$door_locked = "1";
$data = "";
# Flash the entry lights
light_on($entry_lights_unit);
sleep 1;
light_off($entry_lights_unit);
}
# Unlocking...
If ($data =~ $door_lock_event) {
print "Door unlocking event detected. Received: $data.\n";
$door_locked = "0";
$data = "";
# If it’s during the day, flash the entry lights
if (($now[2] >= $daytime_hour) && ($now[2] < $evening_hour)) {
light_on($entry_lights_unit);
sleep 1;
light_off($entry_lights_unit);
}
# If it’s evening, turn on the kitchen and entry lights
if (($now[2] >= $evening_hour) && ($now[2] < $bedtime_hour)) {
light_on($entry_lights_unit);
sleep 1;
light_on($kitchen_door_lights_unit);
}
Exhibit B
hhpg.indb 248
11/24/2004 1:56:23 PM
249
Chapter 11, Keyless Entry Welcome Home
# If it’s after bedtime, light a path to my bed
if (($now[2] >= $bedtime_hour) && ($now[2] < $daytime_hour)) {
light_on($entry_lights_unit);
sleep 1;
light_on($kitchen_door_lights_unit);
sleep 1;
light_on($downstairs_hallway_lights_unit);
sleep 1;
light_on($upstairs_hallway_lights_unit);
}
}
}
# 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");
}
Exhibit B
hhpg.indb 249
11/24/2004 1:56:23 PM
hhpg.indb 250
11/24/2004 1:56:23 PM

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

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