Control Your Home From Your iPhone 2.0 - Full Tutorial
Wednesday, July 2nd, 2008
To celebrate the release of the iPhone 2.0 this post is an expansion of my original tutorial and goes through everything you need to do, step by step, to control your lights, doors, etc… using x10 and PHP technology all from your iPhone 2.0/iTouch. This tutorial assumes that you have a working knowledge of HTML, CSS and PHP, and have downladed the iPHC v1.3 files.
Step One: Web enabled device - any device that can connect to the Internet will work once you have everything set up. (Note: The iphc1.3 package includes an iPhone/iTouch specific interface, but still looks good in a web browser on a PC.)
Step Two: Web Server with PHP - You’ll need a web server that has PHP installed on it. Most likely it will be an install on your home PC because you have to have the ActiveHome x10 USB module plugged into the computer controlling the house. The module comes with a driver download which will be necessary as well. My install is on Windows XP running Apache, PHP and MySQL. The interface can work without MySQL but it is cooler with it so I have included an SQL file in the iphc1.3 download. This is the tutorial I used for Apache, PHP, and MySQL install on a Windows PC. I also suggest installing phpMyAdmin to make working with your databse(s) and uploading the iphc1.3 SQL file easier. You will need to create a database (I called mine “fonhome” - haha) before uploading the SQL file btw.
If your install goes well you should be able to replace the contents of htdocs with the files from the iphc1.3 download, open your browser and type in localhost, and see the interface. Download the iPHC version 1.3 here (the code is not well commented yet but I will be adding more comments soon). If you are using a router you will have to configure it before you can connect to your interface from the Internet via your ip. I am not sure if this is possible for every isp but it works for me.
Look up your computer’s ip address in the command prompt by typing ipconfig. Allow that address on port 80 in your router and you should be able to get through to your interface through any web enabled device using the ip address that is currently provided to your by your isp (check your ip address here). Do this carefully! There are tons of tips on doing a safe and protected Apache install using the .htaccess and Apache’s htpasswd so check them out. Opening your computer to the public can be risky guy.
Step Three: The Hardware - There are many different modules out there that accept incoming x10 signals. For this tutorial you will need at least the ActiveHome Pro USB module plugged into your computer running PHP and into the wall, and one x10 module. I bought a package that included several modules (most importantly the USB one) that was perfect for testing. I also purchased a x10 Universal Module and Schlage Electric Door Strike for unlocking my door remotely (the code for which is included in iphc1.3).
Step Four: The Code - At this point you should at least be able to view “localhost” in your web browser and manipulate the pages in your main web folder.
The way the interface works is quite simple. There is an .exe file (ahcmd.exe) that is available in the ActiveHome SDK for the ActiveHome Pro that allows x10 signals to be sent through your house using PHP functions to trigger the events (I have included the exe file in the iphc1.3 package but not the full SDK documentation). This file will need to be included in your main web folder where your index is located (or at least somewhere that you can point to it).
The PHP: This is the line of PHP code that turns any x10 module set to address A1, off:
exec(”ahcmd.exe sendplc a1 off”);
ahcmd.exe accepts several commands, sendplc being one of them. There is more documentation on the accepted commands here but this system uses sendplc exclusively. The function is carries out by PHP’s exec command which you can learn more about here. In addition to the “off” function there is also “on” (duh), dim and bright.
The most efficient way to trigger an event using HTML and PHP is AJAX ( or AJAH more appropriately depending on who you ask). Either way, I decided to go with that in the iphc1.3 code and also featured it in my “iphc PHP and AJAX Breakdown” post which you can view here.
Macros: To make a series of x10 events, you really only need to copy the above code for as many events as is needed and change the addresses/statuses. I have found that using PHP’s sleep function to put a second or two (or more depending) between events makes for a more reliable macro. Once again, my full code is included in the iphc1.3 download.
The CSS, Javascript, HTML: I have used some Javascript to make the navigation buttons work, as well as to hide the navigation bar on the iPhone. The css provided is a “green” theme including several icons that can easily be swapped out. The HTML is well, HTML. If you think it is messed up, let me know. Everything is set up to look spiffy on the iPhone 2 interface.
That’s all folks! Let me know if you have any questions.
PS - I am still working on a little video to show the interface in action, and a iPHC version 2.0. Stay tuned.

Here is version 1.1 of the Home Controller project. In addition and revision to what I posted in “
Project 1