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).

8 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