Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

X10 Timer script...

by Clownburner (Monk)
on Mar 19, 2001 at 08:42 UTC ( #65361=sourcecode: print w/ replies, xml ) Need Help??

Category: Miscellaneous
Author/Contact Info James Mancini
Description: This is a simple Perl control script for the X10 Firecracker module (

Useful for simple crontab timer applications. The Options are overly wordy to make for easy crontab reading later on.

Requires the ControlX10::CM17 module and Device::SerialPort module from CPAN. Serial port dependancy means that this code is probably not portable to Windows platforms without some modifications; other portability unknown.
# master_timer
# Perl control script for X10 Firecracker module
# Useful for simple crontab timer applications. Options are overly wor
# to make for easy crontab reading later on.
# Requires the ControlX10::CM17 module and Device::SerialPort module f
# CPAN. Not portable to Windows platforms; other portability unknown.
# 18-Mar-2001 by James Mancini - released into public domain

use vars qw( $module $secure $action);
use Device::SerialPort;
use ControlX10::CM17 qw( send_cm17 0.05 );
use Getopt::Long;
use strict;

my %opt;

$opt{fullcmd} = "$0 ".(join " ", map {$_ =~ /[ \[\]\*\{\}\;\>\<\&]/ ? 
+"'$_'" : $_ } @ARGV);

my $port = $opt{port} || "/dev/ttyS0";
my $serial_port = Device::SerialPort->new ($port,1);
die "Darn! I Can't open serial port $port: $^E\n" unless ($serial_port

# These are for the pass-through port on the Firecracker
$serial_port->write_settings || die "Failed to set serial params.\n";

if ($opt{secure})
    my $sleeptime = int(rand (31) * 60);
    print "\nSleeping $sleeptime seconds..\n" if $opt{verbose};
    sleep $sleeptime;

my $action = "";
if ($opt{action} =~ /^off$/)
    { $action = "K" } 
elsif ($opt{action} =~ /^dim([+-][\d]+)$/)    #dim can be + or -
    { $action = "${1}" }
elsif ($opt{action} =~ /^bright([+-][\d]+)$/)    #really the same as d
    { $action = "${1}" }
elsif ($opt{action} =~ /^dim$/)
    { $action = "-14" }
elsif ($opt{action} =~ /^bright$/)
    { $action = "+14" }
elsif ($opt{action} =~ /^allon$/)          # All lights on
    { $action = "O"; $opt{module}=""; } 
elsif ($opt{action} =~ /^alloff$/)        # All lights off
    { $action = "N"; $opt{module}=""; }
elsif ($opt{action} =~ /^masteroff$/)        # All modules off
    { $action = "P"; $opt{module}=""; }
else     # Default action = turn module on 
    { $action = "J" }

$opt{module} =~ s/^[^0-9a-g]$//i;
$opt{hcode} =~ s/^[^a-zA-Z]$|^[\w].+//;
$opt{hcode} = "A" unless $opt{hcode};
$opt{hcode} =~ tr/a-z/A-Z/;
my $cmd = "$opt{hcode}$opt{module}${action}";

print "\nSending $cmd...\n" if $opt{verbose};
send_cm17($serial_port, $cmd);

$serial_port->close || die "\nclose problem with $port\n";
undef $serial_port;

sub options () {
   my $opt = shift;
   GetOptions( $opt,
   unless ($opt{module}) #print usage 
     print <<ECHO;


master_timer [options]

    *REQUIRED* Specify which module to send the command to. 
    16 modules are allowed per house code, numbered 1-9 then a-g
    (i.e, 10 = a, 16 = g).

    {optional} 'opt' must be one of the following: 
    'allon' and 'alloff' refer to all LIGHT MODULES,
    'masteroff' turns off ALL MODULES. Default action is ON.

    set housecode for command. Defaults to housecode "A".

    {optional} Add a random delay between 0 and 30 minutes before 
    executing command. Useful for timer applications. Set the command
    to run at the EARLIEST time you want it to happen.

    'device' should be the full path to the port, e.g. "/dev/ttyS1".
    Optional. If not specified, defaults to /dev/ttyS0 (COM1). 

    {optional} Print status messages.

     exit (1);

Comment on X10 Timer script...
Download Code
Re: X10 Timer script...
by myocom (Deacon) on Mar 19, 2001 at 11:45 UTC

    And, of course, no discussion of Perl and home automation would be complete without a mention of our good friends over at MisterHouse. I use it, and I'm pleased with it.

Re: X10 Timer script...
by jfroebe (Vicar) on Oct 10, 2004 at 03:05 UTC


    I was looking for a simple example perl script to get started on my home automation project. I type in X10 and walla! The answer arrives in under a second!

    I looked at MisterHouse as it was really a home based application server that a chunk of it was for X10. It was definitely overkill and not at all friendly for configuration. Maybe the developer will add user friendly configuration in the future. Looks good though once it is configured

    Jason L. Froebe

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Back to Code Catacombs

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://65361]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-09-01 21:47 GMT
Find Nodes?
    Voting Booth?

    My favorite cookbook is:

    Results (17 votes), past polls