Hack 70. Control the Lights Using Your IP Phone
Using an X10 phone controller, you can turn your lights on and off from the comfort of your IP phone.
X10 home-control interface equipment has been a favorite pastime of geeks for decades. Since the early days of 8-bit hobby computers, you've been able to automate your home using your keyboard (and later, your mouse). X10 interface controllers connect to lights and other appliances in your house, and your computer can send serial commands to the controllers to turn them on and off and adjust voltage like a dimmer. Some X10 interfaces even offer telephone-based user interfaces, letting you control them by calling them with your phone.
There are a few ways to integrate X10 controls with an Asterisk phone system. The integration method depends on the type of X10 controller purchased. For this hack, I chose the X10 TR16A phone controller that operates by DTMF digits. Ordinarily, you would hook a phone line to it and then call that phone line with a standard phone to operate the TR16A. But, with Asterisk, you can connect directly to the TR16A as if you yourself are the phone company. Then, controlling the TR16A is as simple as a dial-plan hack in Asterisk.
The Asterisk system I used to connect to the TR16A contains a single Wild-card TDM400P using two foreign exchange station (FXS) modules and one FXO module. The system has one analog phone and one SIP-enabled Polycom IP500 phone. The SIP phone is the one I used as my "remote control"the phone from which I sent my commands to the X10 controller.
I performed the following steps to integrate the TR16A with the Asterisk system.
For the TR16A controller:
For the Asterisk system:
Here's the bit from /etc/asterisk/extensions.conf that you would use, assuming the FXS port is Zap/1:
exten => 100,1,Dial(Zap/1)
Here's the bit from /etc/asterisk/sip.conf:
 username=200 secret=200 type=friend dtmfmode=inband disallow=all allow=ulaw
Verifying if the setup is correct requires a few test calls. Use the Polycom IP phone to call the controller. Go off-hook on the IP phone and dial the extension number assigned to the controller (100 in this example). The line will ring for about 15 seconds before the controller will answer. When the controller answers, you will hear three beeps. Enter your PIN code, and then a second set of three beeps will confirm that you entered the proper PIN code. After the second set of three beeps, enter the module number you want to control, followed by * or # to turn the module on or off, respectively.
I mentioned that there are a few ways to integrate X10 controls with Asterisk. Using the TR16A, I demonstrated how you could connect to an Asterisk phone system using an FXS port and an IP phone to operate the X10 controller. X10 has another controller known as the CM15A. The CM15A connects to your local PC using a USB cable, and it boasts a free SDK that you can use to write custom scripts. Using the Asterisk system's IVR function, you can program Asterisk to run a script. In turn, that script can control the X10 modules, allowing for a pure software-based solutionthat is, no FXS interfacing necessary.
5.13.1. Hacking the Hack
To have Asterisk dial the PIN code for you automatically, add it to the extension definition that dials the controller (assuming your PIN is 1212), so that it dials your pin automatically after connecting:
exten => 100,1,Dial(Zap/1,30,D(1212))