Currently playing on my computer (tune in: pls, asx)
     
 
Mon, 07 Aug 2006

X10 Home Automation Using a CM11A Deivce on FreeBSD
A little over three years ago, I bought an X10 CM11A Serial Inferface Controller. Then using some very handy Perl modules I wrote a small piece of software that completely controls the outdoor lighting for my home. The lights come on at sunset and are all off by sunrise. It's a great little system and pretty easy to implement (on FreeBSD).

I chose the X10 CM11A Serial Interface Controller because there is a Perl module (ControlX10::CM11) explicitly written for the hardware. Because the CM11A connects to my computer via the serial port, ControlX10::CM11 is dependent on the Device::SerialPort Perl module. Consider the following sample code:

#!/usr/bin/perl

use Device::SerialPort;
$serial_port = Device::SerialPort->new('/dev/cuaa0',1);
die "Can't open serial port $serial_port: $^E\n" unless ($serial_port);

$serial_port->error_msg(1); # use built-in error messages
$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;
receive_cm11($serial_port);
send_cm11($serial_port, 'A1');
send_cm11($serial_port, 'A1');
send_cm11($serial_port, 'AJ'); # J is on; K is off
send_cm11($serial_port, 'OJ'); # J is on; K is off

When executed, this code will send an X10 signal that will turn on any X10 modules with an address of 'A1'. Not very interesting since there isn't any code logic to determine when the "on" signal should be sent. In order to determine when I should send "on" and "off" signals to my different X10 lighting modules, I use the Astro::Sunrise Perl module. It is a handy little module that will quickly determine the sunrise and sunset time for a latitude and longitude on any given day. Here is small snippet of code that generates sunrise and sunset times for my general location:

#!/usr/bin/perl

require Astro::Sunrise;

$long = -122;
$lat = 47.5;

$date = localtime(time);
print "$date\n";
$sunrise = Astro::Sunrise::sun_rise($long, $lat);
print "sunrise at $sunrise\n";
$sunset = Astro::Sunrise::sun_set($long, $lat);
print "sunset at $sunset\n";

Nice and simple and easy.

For my home automation software, I turn on all of the lights at sunset. Then I have different sets of lights turn off at specific times as the night progresses. Furthermore, I wanted to add a bit of randomness to the off events so that it didn't just look like the lights were going off at the same exact time every night. The lights that I choose to stay on all night are turned off at sunrise (or more specifically 1 hour before sunrise).

Here is the code that runs my home lighting; I call it from cron every minute of the day. Have fun with it!

(Update Thu Aug 10 00:08:53 PDT 2006 // fixed some punctation mistakes)

:: Posted by rus on Mon, 07 Aug 2006 8:23 pm
:: Filed under /contrib/x10



       

November 2008
Sun Mon Tue Wed Thu Fri Sat
           
23 24 25 26 27 28 29
30            

About
The Daily Biff
Rus Berrett's weblog

Contact Me
Email: rus at berrett dot org
AIM: biffordtdavis

Search 'The Daily Biff'



Proclamations
Exaggerated opinions of my own importance. Proceed with caution.

Buy Me Stuff
My wish list is my gift to you (yes, shameless, I know).

Subscribe
Subscribe to a syndicated feed of my weblog, brought to you by the wonders of RSS.

Categories
You can isolate posts by category using the following links.

Archives
Past entries are available for review.

Blogroll
These are a few blogs run by my esteemed friends and colleagues. My personal comments about the blog (and its author) can be accessed by clicking on the "wtf?" graphic to the immediate right of each entry (wtf = "What the flip?" as in "What the flip is grandma doing at the sand dunes?").

Family
What the flip is "Yatyk's Musings"?  And who the flip is Mark Berrett?

Friends
What the flip is "The Improvist"?  And who the flip is Dan Brian?
What the flip is "The Borel-Cantelli Lemma"?  And who the flip is Norm Jones?