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

Chapter 2. Automate Your Porch Light > Exhibit B: Script Source Code

2.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 $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");
}


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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