Monday, October 29, 2012

X10 Home Automation with Raspberry Pi

I have a confession to make.

Ever since I ordered my Raspberry Pi and finally received it in the mail I've been obsessed with it.  Probably not in a healthy way.  My wife calls herself a 'Pi widow'.  

I get more and more amazed every day at what this little box can do.

First, I put RaspBMC on it and set it up with my Network Storage to stream my entire movie and music library to my TV.  That worked great, but I since decided to go with Roku box (which is great) to free up the Pi box for other projects.

And this is where this post comes in as the current project was to setup home automation using Raspberry Pi.

Please note, that you should have some basic Linux experience or ability to Google for how to do stuff in Linux.  I'm not a Linux expert by any means so I cannot provide any expertise on how to troubleshoot anything if you run into issues.

Here is what you will need:
  • Raspberry Pi
  • SD card (I used 4 GB SanDisk Ultra II 15MB/s)
  • CM19A X10 controller (eBay, Amazon)
  • TM751 X10 Module (Get it here)
  • Any other X10 modules you want to use
  • Network cable
  • USB hub with Micro USB cable (or a dedicated Micro USB power supply - 5V)
  • HDMI cable
  • TV or Monitor with HDMI input
  • USB Mouse
  • USB Keyboard
Software I used:
Step 1: Install Linux on the Raspberry Pi:
Step 2: Install Apache web server:
  • sudo apt-get install apache2
  • Configure your router to assign a static IP address to your Raspberry Pi
Step 3: Install Java Runtime Environment:
  • sudo apt-get clean
  • sudo apt-get update
  • sudo apt-get install openjdk-6-jre
Step 4: Install Libusb:
  • sudo apt-get install libusb-1.0-0-dev
Step 5: Install Mochad:
  • wget http://sourceforge.net/projects/mochad/files/mochad-0.1.15.tar.gz
  • tar xf mochad-0.1.15.tar.gz
  • cd mochad-0.1.15
  • ./configure
  • make
  • sudo make install
Step 6: Install X10 Java Server:
  • Download it from here
  • Extract X10.class and put it in your /home/pi directory
  • Download the startup script here
  • Extract x10 and put it in your /etc/init.d directory
  • Run this command to make script executable: sudo chmod 755 /etc/init.d/x10
  • Run this command to launch server on Linux boot: sudo update-rc.d x10 defaults
Step 7: Install X10 Web Client:
  • Download the zip here
  • Extract it to X10 folder and copy the whole folder to the apache web directory /var/www
  • Edit index.htm file to modify your devices
  • In index.htm, update mochadServer and controllerServer ip address (replace current IP with your Pi's IP address.
After all the steps are done, unplug your mouse and plug in the CM19A X10 Controller (or use a USB hub to have it all plugged in).  Restart your Pi.  Set your TM751 on house code A, open your web client and try it out by going to http://192.168.1.10/X10 (replace the IP address with the one of your Pi).

25 comments:

  1. Thank you Yuriy, I am looking at doing the same thing. I have a Pic 18F8722 running my home automation system now, but the Pi will eliminate a lot of hardware.

    I will try it out in the next couple of days and let you know how it goes.

    ReplyDelete
  2. This could be nice, but I can`t get sudo chmod 755 /etc/init.d/x10 command to work

    ReplyDelete
    Replies
    1. Make sure you are logged into the Pi as root user.

      Delete
  3. Hi Yuriy,
    I am working on something very similar for a school project, and ran across this post while looking for raspberry pi x10 resources. Is there any chance you would be willing to elaborate a little further on how the Java Server is used, or how you actually call the mochad commands? If you have time, a follow up would be much appreciated! My email is skobovm@gmail.com if you would rather not post a long follow up.
    Thanks,
    Mikhail

    ReplyDelete
    Replies
    1. Hi Mikhail. The Java Server is listening on a specific port then gets the parameters passed to it by the web client and then executes a shell script. Here is the block of code that calls mochad. Essentially you are calling a shell script and passing parameters that mochad expects. Hope this helps.

      try {
      String[] cmd = {
      "/bin/sh",
      "-c",
      "echo \"rf " + address + " " + command + "\" | nc " + server + " 1099"
      };
      System.out.println("Command received: " + address + " " + command);
      Runtime.getRuntime().exec(cmd);
      Process process = Runtime.getRuntime().exec(cmd);
      int returnCode = process.waitFor();
      } catch (Exception e) {
      e.printStackTrace();
      }

      Delete
    2. I think so, thank you. I will give your server a try, and see if I can get that working first.

      Delete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This works for about 15 minutes, then it quits. It will start working again if I reboot. Any ideas?

    ReplyDelete
  6. This is really cool. Ive had my pi for a while and I was trying to find something useful for it to do full time. Is there a simple way to use the CM11 (Serial) instead of the usb one?

    ReplyDelete
  7. Awesome. I'm definitely trying this.

    ReplyDelete
  8. Hey i got all the way to the end, and keep getting an error with the :

    Run this command to launch server on Linux boot: sudo update-rc.d x10 defaults

    every time i run it, it says the /exe/init.d/x10 does not contain the proper file... was wondering if anyone else had this issue...

    ReplyDelete
    Replies
    1. I actually figured it out by removing the ".sh" from the x10.sh file and now all is running great. (it did have a little error saying that it was missing some type of file, but now its running.. (I have very little Linux experience, sorry)

      Thanks!!

      Delete
  9. I downloaded, installed and my X10 website is running. Yay. I see a list of 4 devices, which I can check/uncheck. I see two buttons (on/off) which I can click. When I click a button, it unchecks any devices I have checked, but my CM19A does not send any commands to my modules (light stays on). I am trying to troubleshoot, but is it slow going as I am new to Jquery, javascript, css, html and linux. Is there a way I can send a command to the CM19A "manually" (without using the X10 web site running on my raspberry).

    In the syslog I see kernel recognize Product: USB Transceiver. If I unplug the CM19A from the usb, I see mochad report detaching CM19A and terminating, so I think it is ok at that end. How can I send a command to CM19A going through the least amount of software possible? Can I do it from the command line? This would give me another starting point for troubleshooting/debugging my index.htm and the scripts and css's it calls.

    ReplyDelete
    Replies
    1. John, basically, if you want to bypass the web piece, the mochad command you should execute in the command is something like this:

      sh -c echo "rf A5 on" | nc 192.168.1.2 1099 (replace A5, on, and ip with your own device address, command and mochad server ip). Make sure you keep the quotes - I don't think it will work without them.

      Delete
  10. Thanks for your reply. The command you suggest (sh -c echo "rf A1 off"|nc 192.168.1.4 8089, as modified for my location) elicits no response (light stays on). But I also get no error using another port (e.g., 192.168.1.4 1099). How can I confirm what port the mochad server is listening on? In syslog, I see mochad[3159]: Found CM19A and mochad[3159]: In endpoint 0x81, Out endpoint 0x02. Can this information tell me the port or is there another way to find it out?

    ReplyDelete
    Replies
    1. Got it working! Thanks for your help and thanks for this project Yuriy.

      Delete
    2. hmm, May I ask how you got it working? Im having the same problem. This ( reverse engineerd from the html ) works
      curl -X GET 'http://192.168.1.55:8089/?address=b1&command=on'
      But I can't get anything with nc to do anything
      I've tried
      sh -c echo "rf b1 on" | nc 192.168.1.55 1099
      and every combination of capitalizations of the commands! I've done them as user 'pi' and 'root'... Any idea what I might be missing that the Java server is getting right?

      Delete
    3. all I did at the command prompt was the nc command to set up a connection to the mochad server:
      $ nc 192.168.1.4 1099
      then (there is no prompt from the mochad server):
      rf a1 on
      then the mochad server performs the command and responds with its own ackowledgement, like
      04/09 12:12:12 Tx RF HouseUnit: A1 Func: On

      Ctrl-C ends the server session and gets back to the command prompt.

      In what context does your curl -X GET 'server/arguments' work? I'm stuck unable to get a simple javascript in the browser to issue a command to mochad server.

      Delete
    4. update - found on sourceforge this command line that works in one line:
      $ echo "rf A1 on" | nc 192.168.1.4 1099
      and stays on the command line

      Delete
    5. droping the 'sh -c' from my echo ... | nc... command worked for me with my TM751. However as I stated below, i still can't get my WS467 to do anything...

      My curl command basically just does the same thing as the html/javascript page Yurly wrote. It's my understanding that the html/javascript sends parameters to xxx.xxx.xxx.xxx:8089 which is where the java server lives and that simply reformats the parameters and sends them along to mocad at xxx.xxx.xxx.xxx:1099 Since you seem to have the mochad bit working via nc I would suggest the problem might be with your java server? maybe try reinstalling that bit? My HTML work right out of install with my TM751, so I think the html/js should be good to go...

      Delete
  11. Hi, I love this! I got my setup working, however in addition to the TM751 I also bought a WS467 http://www.thehomeautomationstore.com/ws467.html. I can't seem to get the WS467 to respond to anything? Are there other possible commands that can be send related to dimming besides "OFF" and "ON"? Other ideas why the WS467 might not work or be different?

    ReplyDelete
    Replies
    1. Here is mochad reference on the commands you can use: http://sourceforge.net/apps/mediawiki/mochad/index.php?title=Main_Page#Use_it

      Delete
  12. Hi , how can i use it with CM17A aka FireCracker?

    ReplyDelete
  13. I don't have one available to test - it may work but will probably need some modifications. I believe there is an easier way to setup Firecracker - I remember it being a lot more complicated to use the USB version.

    ReplyDelete