Monday, March 8, 2010

Adventures in X10 Home automation with iphone and JQTouch

This year my tax return was big. It let me afford to purchase some things that I've wanted to play with for a while now, not the least of which the cm19a X10 RF controller like this one here

http://www.thehomeautomationstore.com/cm19a.html

I think this device is probably anyone's best bet for this sort of thing as it does not interface directly with any sort of power outlet so you don't have to worry about some sort of freak occurrence happening and destroying your precious, precious computer.

This means though that you will also need to purchase the transceiver module

http://www.thehomeautomationstore.com/tm751.html

then of course you will want at least one or more lamp/appliance modules

X10 is sort of the old man at the table of home automation, so there's plenty of documentation on how it does what it does, not to mention hard ware and modules are pretty reasonable compared to the newer standards like Z-wave.

the beauty of this little device is it is supported in linux with control via the command line.

to achieve this we first get the most recent driver from here

http://lemaymd.com/drupal/node/3

extract the files to your home directory
(I'm using Ubuntu 9.10)

go ahead an plug in the device to an open USB port and confirm that the computer sees it

$ lsusb

...
Bus 005 Device 002: ID 0bc7:0002 X10 Wireless Technology, Inc. Firecracker Interface (ACPI-compliant)
...


To build the modules make sure you have the build-essential package installed

$ sudo apt-get install build-essential

then

$ cd x10-cm19a-0.1.1/

$ sudo make
$ sudo make install

if everything went well, it should compile with out any major complications or errors

if it didn't, or you feel like you need to recompile the module, be sure to run sudo make clean before trying again.


but now we are faced with some kernel confusion. Stock builds of Ubuntu will more then likely think the cm19a is some sort of ir receiver device or other kind of remote control. To set it straight we add these modules to the kernel module blacklist

$ sudo gedit /etc/modprobe.d/blacklist.conf

add these lines

blacklist lirc_atiusb
blacklist ati_remote

save and close
then remove the modules from the current state of the kernel

$ sudo rmmod lirc_atiusb
$ sudo rmmod ati_remote

then to add the cm19a module

$ sudo modprobe x10-cm19a

There may be some plugging and unplugging but you should see your new device show up in /dev

$ ls /dev/c*

/dev/cm19a0

if you don't see it here now, a reboot may solve the issue, or you can add some rules to udev to make sure it gets inserted with the right permissions and or other custom parameters. Check this post out for more on that

http://pr0gr4mm3r.com/linux/getting-your-x10-cm19a-module-working-in-linux/

if everything worked you should now be able to write commands directly to the device in this manner to turn off the X10 module with house code A unit code 5

$ echo -A5 > /dev/cm19a0

this does not give you any direct feedback though, so for debugging dmesg and syslog are your friend

open up a new terminal of your choice and run either

$ dmesg | tail

or

$ tail -f /var/log/syslog

(passing the -f flag to tail will let tail follow the file as new lines come in)

you should see something like


Mar 8 11:02:08 ubunbox kernel: [164153.964339] x10-cm19a: devf_write called with buffer length 4
Mar 8 11:02:08 ubunbox kernel: [164153.964346] x10-cm19a: got off command
Mar 8 11:02:08 ubunbox kernel: [164153.964348] x10-cm19a: house code A
Mar 8 11:02:08 ubunbox kernel: [164153.964351] x10-cm19a: unit = 1
Mar 8 11:02:08 ubunbox kernel: [164153.964354] x10-cm19a: Created HA command: off a 1
Mar 8 11:02:08 ubunbox kernel: [164153.964357] x10-cm19a: Transmitting 5 byte command:
Mar 8 11:02:08 ubunbox kernel: [164153.964360] x10-cm19a: buf[0] = 20
Mar 8 11:02:08 ubunbox kernel: [164153.964363] x10-cm19a: buf[1] = 60
Mar 8 11:02:08 ubunbox kernel: [164153.964365] x10-cm19a: buf[2] = 9F
Mar 8 11:02:08 ubunbox kernel: [164153.964368] x10-cm19a: buf[3] = 20
Mar 8 11:02:08 ubunbox kernel: [164153.964370] x10-cm19a: buf[4] = DF
Mar 8 11:02:08 ubunbox kernel: [164153.964376] x10-cm19a: wrote command
Mar 8 11:02:08 ubunbox kernel: [164153.964379] x10-cm19a: read back interrupt data
Mar 8 11:02:08 ubunbox kernel: [164153.964382] x10-cm19a: devf_write: back from x10_transmit_cmd
Mar 8 11:02:08 ubunbox kernel: [164153.966694] x10-cm19a: freeing buffer in callback (0xf685e020/0x3685e020)


So now that you have command line access to any X10 enabled appliance in your house, the next thing you want to do is set up some server side script to make access from mobile devices possible

this is a quick and dirty PHP file that takes arguments via http GET requests
By reading further you acknowledge that I am in no way responsible for any undesired effects!!!
but feel free to use it to whatever legal ends you see fit

Put this in some protected web directory in whatever php capable server you are running.

see .htaccess for more on using basic authincation to protect files and directories under apache

Some might also notice that I am using the command line backticks here. You can read up about this on the official PHP.org site to get more info on alternative methods for running cli commands and the potential security risks

<?
####
#Scott Cheezem
#03052010
###
#h - the house code
#u - the unit code
#s - the state 1 for on 0 for off
function check($ch){
#echo "checking $ch";
if (isset($_GET[$ch]) && strlen($ch) == 1){

return true;
}
}


if (check('h') && check('u') && check('s')){
$h=$_GET['h'];
$u=$_GET['u'];
$s=$_GET['s'];
if($s=="0"){
$s="-";
}else if($s=="1"){
$s="+";
}
$ts=`date`;
$command="echo ".$s.$h.$u." > /dev/cm19a0";
#echo $command;
`$command`;
$log=`echo "$ts $command" >> x10.log`;
}

?>

after that is done check to make sure everything is working as it should up to this point...

try going to

http://ip.address.of.your.computer/path/to/file/whateverYouCalledIt.php?h=A&u=1&s=1

and it should turn on house code A, unit code 1


and now the pay off!!!!

the next layer in this delicious cake of home automation is your interface.... Your iphone interface!!!

I have a working version of a webapp I call liveHouse. feel free to check it out here on your ipod touch/iphone/Safari (even windows safari works! YAY!)

http://user.theroyalwe.net/liveHouse









if you want to download the code to use on your own server go here

http://user.theroyalwe.net/liveHouse.zip


click the plus in safari and then add to home screen to install on your device











(note: in the case of webapps like this one, all the javascript CSS images and html are cached locally to your phone. when ever you run the app, it will check automatically for any updates to the code and download them. really,who needs the app sotre!?!)

this app was developed with jquery and the jqtouch extension.

more info on jqtouch here

http://code.google.com/p/jqtouch/

NOTE: this is just the first working draft. The control script url (the php file we made earlier) and optional login credentials are stored in a cookie in plain text. For anyone planning on using this from behind their home network, this should be fine. I would not recommend using this script from outside your local area network if security is an issue!!!


Cheers and Happy home-atuomating!!!

2 comments:

  1. Hey Scott,

    Thanks for the fantastic post it has given me some insight on getting the cm19a working on my linux box. The issue I am running into is that when I echo a command to the device it will work for X hours but after a certain time I get the following

    $ echo +A1 > /dev/cm19a0
    bash: /dev/cm19a0: No space left on device
    $

    Just wondering if you have any insight or have run into this before? Please let me know as it would help greatly. Thanks again

    ReplyDelete
  2. Terribly sorry for the looooong delay getting back to you.

    incase you are still experiencing the issue:

    make sure that the disk partition where syslog lives is not full. This happened to me due to my firewall, took me a few hours to figure out that was the case...

    barring that

    it kind of sounds like your system thinks your cm19a is some sort of storage device. Either that, or potentially, that the permissions might not be set correctly.

    if you do lsusb while your device is plugged in you should see a line that looks like

    Bus 005 Device 002: ID 0bc7:0002 X10 Wireless Technology, Inc. Firecracker Interface (ACPI-compliant)

    I would use the Bus number and device number to find it in /dev

    for mine I would
    ls -l /dev/bus/usb/005/002

    you want it to look something like

    crwxrwxr-- 1 user users 189, 513 2011-01-07 14:26 /dev/bus/usb/005/002

    the writeable bits are the ones you want to make sure of.
    chmod ug+w /dev/bus/usb/005/002


    all this being said, your best bet is probably to upgrade to the latest driver from lemay's site.

    I'm actually in the process of writing a new post on mike's new driver and updating the livehouse webapp. the new one is pretty cool if I do say so myself.

    ReplyDelete