Last weekend I was digging through the attic to get Christmas lights. It was time to start fixing the ones that are not working anymore. In the container of lights I have these boxes that plug into the wall and allow me to turn the lights on and off with a remote control. On the outside of the box it says “Plug-N-Power”. I think I bought them over 20 years ago.
Plug-N-Power is still around today. You might know it as X-10. After all of these years they use the same protocol that works over your house wiring.
I always get a chuckle when I look at those boxes because I can remember when I built a circuit from a Radio Electronics magazine. It was supposed to send Plug-N-Power signals over house wiring from a Commodore 64. I wanted to build it so I could create a program on my Commodore to control my parents Christmas lights.
It was a dicey circuit since it was mixing 110 AC with the “user port” of the Commodore. I started working on it – I think around October of – hmm I think it was 1989. It took me until right before Thanksgiving to finish it. I was in a hurry, and I was busy with school at the time too. The production quality of the hardware…well…lets say it was pretty bad – and I knew it.
I had this rule with any 110 circuit I built. I would use a long extension cord, and hide inside the house – flip a switch to turn it on. That way if something went really bad, I was protected from shrapnel if the thing blew up.
Good thing I did it this time. I rolled out the extension cord, and plugged everything in (With the switch off of course). Got inside the house, and got my older brother in the room so he could watch too. I was hoping that when I flipped the switch nothing would happen.
I was wrong – flipped the switch and a huge arc came out of the circuit board, and then a giant bang….a ring of smoke went up into the air. My brother and I couldn’t stop laughing. When I looked at the board, half of the board was missing. I found resistors, capacitors all over the driveway in pieces.
Even though I was laughing I was too scared to try again. I figured if I was able to to get the thing working I would be afraid to leave it running in my parents house.
Back to today. I have been thinking hard about how the iPhone fits into our product line, and possibly creating some remote administration apps for it. Not because I like the iPhone, but we have actually received quite a few emails requesting different types of remote admin iPhone apps.
When I learn a new platform or language I like to come up with a fun project that is quick to build, but helps me learn the concepts required. I started thinking…hmm…I wonder what type of Plug-N-Power stuff is out there now…wonder how hard would it be to get my iPhone to send signals to them – and control my Christmas lights with it.
I did my research, and I came up with a plan. For it to work I would need to build three things:
1. A PHP script that would work with MySQL to keep track of the Christmas lights (Like Tree, Santa out Front, etc), and their status (On or Off).
2. A computer that would send the signals to the X 10 devices by polling PHP script on a web server, and take action on the XML generated by the php script.
3. An iPhone application that would send data to the PHP application, and be able to render the light descriptions, and allow me to change status by clicking an on or off button.
(The A-Team theme song was going off in my head at the time…and I could hear Hannibal saying, “I love it when a plan comes together”)
The first part was easy. PHP is a breeze to use, and I was able to put something together in an hour.
The second part was a little more difficult. I found that to do this I needed something called “FireCracker” from X10. It cost me $39, and came with a remote control, and another lamp module – a pretty good deal. Here is what it looks like:
Along with the lamp module, wireless receiver, and my iPhone:
The FireCracker device plugs into your serial port. It in turn sends wireless signals to an X10 wireless receiver (Included in the $39 deal) that plugs into the wall…and it actually sends the signals to the X10 device. A side note here – Notice how they are not allowing 110AC to come near the computer!?! Smart idea!
Normally you use an X10 App to control your devices and lights, but I wanted to control this with my own code. After searching I found a free API that let me turn a light on or off using a simple C++ call. After a few hours of coding I had an application that would poll my webserver. The webserver in turn would return an XML feed generated by my PHP script, and then finally my C++ app would turn a light on or off based on what was in the XML feed. After a few tries I got it to work. I did some testing by manually updating the database on the webserver…lights on….lights off…beautiful!
Now to the tough part. Writing the app on the iPhone. I had never written anything for the Mac or the iPhone. I have peeked at Cocoa (The main language used to develop for the iPhone & Mac)…and it is a very strange looking language. I went to the bookstore and got every book I could find on it. After a few days of reading and experimenting I was ready to write my app. I hated Cocoa at first, but after I was finished I was jealous that I couldn’t use this beautiful dev environment to create windows apps.
About 2 days later I had a fairly functional application. A couple more days and I had it nice and polished with an icon and all.
Here is the icon on my home screen of my iPhone (It is the blue Christmas light..sorry for the bad shots)
Once you click on the blue Christmas light, it hits a webserver that pulls down XML and fills out a list of lights to control:
I have my wreath on the door, the garland on the stairs, Santa out front…and our tree on the list. To turn one of them on or off, you simply make your selection:
As you can see my tree is turned off. I press the button, and boom
And now the tree is lit.
Pictures can’t show it all…so I took a video too:
There is some delay between the time I turn off the lights, but that is because I am polling every 15 seconds. If I spent more time on it I could create a solution that was almost instant.
Now I will have something really cool to talk about at Thanksgiving dinner.
One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools
{ 3 comments… read them below or add one }
I’d love to learn a little more about your PHP/MySQL solution. I dabble with both, but am really not proficient enough to set up something without seeing a sample in front of me from which to learn.
To tell you a little about my setup. I run an ActiveHome CM15A X10 device (USB interface versus the Firecracker’s serial interface) and modules from a Black And Decker FreeWire set (X10 compatible). I did some searching around the ‘Net and found X10 Dispatcher (http://x10dispatcher.com/) to replace the default ActiveHome software.
X10 Dispatcher allows you to program simple X10 commands or more complex macros, and allows the user to set up a locally-hosted web server that provides an interface to trigger commands and macros on demand, or set them on a schedule. The problem is that I can’t seem to find the pages served by the application, so I don’t know how they are triggered, whether it’s through some sort of PHP or scripting, or through Windows APIs.
I’ve set up simple PHP scripts to trigger the ahcmd.exe, which provides a command line interface for X10 commands, but can’t figure out how to query for the status of devices. Your solution of using MySQL is, I assume, to store the status of the device by recording the last command sent to each X10 address.
I’d love to hear about your methods, as I have thought about implementing something similar with a locally-hosted server and a Windows Mobile device.
Regards,
Aaron Vanderbye
On the MySQL side I have 1 table in a database…it is called LIGHTS.
This table has 6 entries:
NAME, DESCRIPTION, LOCATION, HouseID, DeviceID, Status
The first three are obvious, the HouseID and DeviceID are X10 House, and Device identifiers. Status is a 0 or 1 value 0=Off 1=On
My situation is a little different, since the webserver that has the PHP and MySql is about 5000 miles from my house…so it would not be possible for the webserver to execute an app that sends the signals.
So with the above database I simply have a PHP script that accepts POST data with authentication and commands…LIKE ADD_ENTRY, or UPDATE_STATUS. These are sent from the iPhone. They look like regular web page posts from the PHP side, but they are hand crafted requests built into my iPhone app.
Then on the server that sits in my house, it has the FireCracker, and runs a C++ application (That I wrote) does a GET from the webserver with an action called GET_XML. This C++ app parses the XML, and then will take action based on status values. If it sees a zero, then it sends an off command to that HouseID and DeviceID.
3 parts are not totally necessary…you could bring it down to just a webserver and the iPhone, but since I don’t have a public IP for the internet at my house…it is not workable.
I can’t post any code yet, since It looks pretty bad, and I have passwords hardcoded (Don’t want the entire net turning my christmas lights on and off 🙂 )
If there is more interest I might post another article with more info…Plus I just got approval from iPhone for their dev program…may be able to let others play with this if I get the time.
Thanks for the info. The MySQL table info gives me a nudge in the right direction, and some food for thought re: how to make it work. I’ve been using the built-in web server pages off the X10 Dispatcher program and accessing them on my WinMo device through the browser, but have been looking for some way to make the interface a little nicer and finger-friendly for the small screen. Also been bothered that I couldn’t seem to find a way to poll for status of the device, so the PHP/MySQL combo seemed an intriguing idea.
Best of luck to you, and happy holidays!