• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 11. Keyless Entry Welcome Home > Exhibit B: Script Source Code

11.9. 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);        
 }
 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);
     }
     # 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");
}


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint