Phone, Tablet & Desktop PHP X10 control

X10 Bridge ControlThis post is the code I’ve written in PHP that allows me to control my X10 devices with a phone, tablet or desktop.  This requires the following:

  • Linux box, preferably a Raspberry Pi
  • HEYU installed
  • Apache installed
  • PHP installed

Step 1 – Raspberry Pi goodness?

RPiThe first step is to get your Raspberry Pi up and running. There are plenty of guides how to download and install either NOOBS, or RASPBIAN on a memory card for your RPi, so I won’t go into those details unnecessarily. Suffice it to say, you’ll need to have your RPi booted into Raspbian and configured to your location and language, including SSH enabled.

Once you have the RPi working you can SSH (remote) into it and log in as the user Pi. For PC you can use PUTTY, or MAC use the Terminal Window to SSH into the Pi. At this point I would also recommend you update and upgrade your RPi, by entering the following:

  • $ sudo apt-get update
  • $ sudo apt-get upgrade

I also recommend changing the default password.

Once updated and upgraded, power down your RPi, and make all the following connections:

  • Install batteries into the CM11A
  • Plug the control cable into the CM11A jack
  • Connect the Serial Adapter to the X10 CM11A’s serial cable
  • Plug the CM11A into a wall socket
  • Plug the USB to Serial Adapter into the RPi

Power your RPi back on, and SSH back into it.

Step 2 – Heyu turn on my lights!

heyu_logo_200x72Let’s install heyu, which is a text-based console program for remotely controlling lights and appliances in the home or office. I put heyu in a directory I created named ‘src’.

Here are the steps I completed:

  • $ ls -l /dev/ttyUSB0
  • You should get something like the following:
    crw-rw---T 1 root dialout 188, 0 Aug 10 20:44 /dev/ttyUSB0
    Make note* of the last part, you’ll need it later.
  • $ mkdir src
  • $ cd src/
  • $ wget http://www.heyu.org/download/heyu-2.11-rc1.tar.gz
    (Or current version)
  • $ tar xfvz heyu-2.11-rc1.tar.gz
  • $ cd heyu-2.11-rc1/
  • $ sh ./Configure.sh

The installer will run, make a cup of coffee.

  • $ make

More installation, drink the coffee you made.

  • $ sudo make install

You should then see the following:

I did not find a Heyu configuration file.
Where would you like the sample Heyu configuration file installed?
1. In directory /home/pi/.heyu/
2. In subdirectory .heyu/ under a different user home directory
3. In directory /etc/heyu (for system-wide access)
4. No thanks, I'll take care of it myself
Choice [1, 2, 3, 4] ?

Enter 1 and you’ll see:

Creating directory /home/pi/.heyu
The sample configuration file will be installed as /home/pi/.heyu/x10config

The next question the program will ask is the following:

I will add the TTY port for your CM11 to the config file
Specify /dev/ttyS0, /dev/ttyS1, etc., or the word dummy
To which port is the CM11 attached?

Enter /dev/ttyUSB0 (or whatever you noted* earlier).

Now you can test the install, enter $ heyu info, and you should get an output with details about the heyu install that look similar to the following:

Heyu version 2.11-rc1
Configuration at /home/pi/.heyu/x10config
Powerline interface on /dev/ttyUSB0
Firmware revision Level = 1
Interface battery usage = 0:00 (hh:mm)
Raw interface clock: Sat, Day 212, 14:07:35
(--> Civil Time: Sat 01 Aug 2015 15:07:35 EDT)
Uploaded schedule will expire in 154 days.
Housecode = M
0 = off, 1 = on, unit 16.......8...4..1
Last addressed device = 0x0002 (0000000000010000)
Status of monitored devices = 0x0042 (0000000000010001)
Status of dimmed devices = 0x1112 (1110000000010000)

Now let’s test to see that you can turn a light on and off. My housecode is M, but use your chosen housecode. Try the following: $ heyu on M2 and the module set to M2 should turn on, alternatively $ heyu off M2 and the module set to M2 should turn off.

You can configure your setup and upload schedules to your CM11A.

Step 3 – Are you being served?

apache_linuxNext we will install an Apache server and PHP5 on the RPi , here is what I did to accomplish that:

  • $ sudo apt-get install apache2 -y

After this install completes, you should be able to navigate to your RPi’s Apache server. Type the IP address of your RPi into any browser, for me it’s 192.168.1.19, and you should see an “It works!” page.
itworks

Let’s install PHP:

  • $ sudo apt-get install php5 libapache2-mod-php5 -y

The user that runs PHP exec commands on Apache installations is www-data. We’ll have to allow the www-data user permission to use the ttyUSB0 device (or what you noted* earlier in Step 2 as your USB to Serial Adapter device name). One way to do this is to modify the ttyUSB0 permissions on boot. NOTE: Due to security concerns, this should only be done on a Raspberry Pi that is not accessible from the internet!

  • $ sudo nano /etc/rc.local
  • Add chmod 666 /dev/ttyUSB0 & to the file on the line above the ‘exit 0’
  • Save and exit the file

We should also modify the ttyUSB0’s current permissions so we can move forward with our install by doing the following:

  • $ sudo chmod 666 /dev/ttyUSB0

We will also have to allow www-data permission to run heyu commands. NOTE: Due to security concerns, this should only be done on a Raspberry Pi that is not accessible from the internet!

  • $ sudo visudo
  • Add the following to the last line of this file and save: www-data ALL=(pi) NOPASSWD: /src/heyu-2.11-rc1/heyu

Step 4 – Get Control!

The code goes in the your Raspberry Pi’s Apache server /var/www/html/ folder, so it’s the default served page. NOTE: The directory was /var/www in Raspbian Wheezy but is now /var/www/html in Raspbian Jessie. Clone the latest release from my Git repository by doing the following:

$ cd /var/www/html
$ sudo git clone https://github.com/audiofreak9/PHP-X10-Control .

YES, the period at the end is needed so only the files are cloned, not the containing directory.

The code gets your configured heyu devices and displays their state and gives control of each. Configure your X10 devices in your HEYU config file. To locate your config file enter the command $ heyu info and note where your config file is located. You’ll see something like, “Configuration at /home/pi/.heyu/x10config”.

Enter $ sudo nano /home/pi/.heyu/x10config (or whatever your config file address is).

Find the “Aliases:” section of the config file, and enter your devices with name and module address like this:

  • ALIAS kitchen M3 StdLM
  • ALIAS front M4 StdLM
  • ALIAS hallway M5 StdLM
  • ALIAS railing M6 StdLM
  • ALIAS lockers M7 StdLM

The word ALIAS lets HEYU know this is a device, give them unique names “kitchen” for example, then list the module address “M3” for example, then StdLM for dimmable modules OR StdAM for non-dimmable modules. Once you have entered all your modules, click ‘CTRL + X’ and save the file when prompted. You can also refer to my example x10config file I posted on GitHub here: https://github.com/audiofreak9/HEYU-config-file-example/blob/master/x10config.

It also gets your configured scenes and displays them as well.

13 thoughts on “Phone, Tablet & Desktop PHP X10 control

  1. Thank you for all of this. I have almost everything up and running, however, how do I add devices to the index.php page? Echo sees and controls my x10 devices but when i load the index page it looks correct but does not have any devices listed. Thanks again

    1. Hey Dan,

      You’ll need to add your devices to the HEYU config file. Enter the command $ heyu info and note where your config file is located. You’ll probably see, “Configuration at /home/pi/.heyu/x10config”. Enter $ sudo nano /home/pi/.heyu/x10config (or whatever your config file address is). Find the “Aliases:” section of the config file, and enter your devices with name and module address like this:

      • ALIAS kitchen M3 StdLM
      • ALIAS front M4 StdLM
      • ALIAS hallway M5 StdLM
      • ALIAS railing M6 StdLM
      • ALIAS lockers M7 StdLM

      The word ALIAS lets HEYU know this is a device, give them unique names “kitchen” for example, then list the module address “M3” for example, then StdLM for dimmable modules OR StdAM for non-dimmable modules. Once you have entered all your modules, click ‘CTRL + X’ and save the file when prompted. Your done, let me know how you make out.

  2. Hi Corey. I’d like to run this package in a lower level directory on my webserver so I can keep it out of public view and password protect it.

    I know I can mess with the rewrite functions of apache, but I’ve not had much luck at that so far. I’d like to be able to change where your software thinks it lives. I’m learning php, but I don’t have a good enough grasp yet to modify your code. Any suggestions or pointers?

    I’m running an old package called BlueLava right now, but I like the way yours looks (and it doesn’t require WAP for mobile!)

    1. What device are you running the bridge and PHP server on that you want to run PHP on “a lower level directory”? The PHP server needs to be on the same device as the HEYU program so that the PHP can run exec() shell script commands.

      However you may not need to use PHP at all now, this is really exciting news. Yesterday BWS Systems released version 2.0.0: https://github.com/bwssytems/ha-bridge/releases/tag/v2.0.0 of the bridge was released, and it now allows the “…ability to run shell scripts or programs…” among many other vast upgrades. I am in the middle of writing a bash script that will take direct calls from the ha-bridge to directly control the HEYU program removing the need for any PHP. Today a bug fix was issued, verison 2.0.1: https://github.com/bwssytems/ha-bridge/releases/tag/v2.0.1 so update to this version, I’ll keep you posted.

      1. I guess that didn’t make much sense, now that I go back and read it…

        I’d like to run the php code from /var/www/control, as /var/www already has a site in it. That would allow me to have the main site open to the world but password protect /control.

        I tried this simply putting your code in /control, but when any submission is made (pressing a button,) it jumps back to the document root (/var/www) instead of returning to your code.

        1. Ok, now I understand, also based on your reply, I think you are using a version of the index.php file that posts to itself and uses PHP to exec() the HEYU command.

          If the first few lines of your index.php file in your “control” folder have the lines:

          < ?php
          extract($_POST);
          $heyuvar = "heyu -c /home/pi/.heyu/x10config ";

          then find all the following:

          header("Location: /");

          and change them to:

          header("Location: /control/");

          This changes where the PHP redirects to after the exec() command is run.

          1. Again, I developed a ‘better’ way to execute control of all the configured ha-bridge devices through an updated index.php file and interface. I’ll post the particulars and the ha-bridge version required to work with my new code asap.

    2. BTW, I developed an alternative index.php code page that is driven from the bridge’s own device state database and uses javascript to post changes. I’ll post that code asap to github and reply with the link.

Leave a Reply

Your email address will not be published. Required fields are marked *