Control your X10 Devices with the Amazon Echo using IFTTT

You can control X10 devices with the Amazon Echo using IFTTT

Are you one of those Amazon Echo users that have the house full of X10 devices and you’re sad you can’t use them with the Echo? There’s a way you can control them if you have a Linux server around, even if it’s something as simple and cheap like a Raspberry Pi!

NOTE: You can control the X10 natively following these steps additional to the steps on this post.

By the time I wrote this post, the Amazon Echo supports a decent range of Smart Home devices like Wink, Samsung SmartThings, Insteon and the Phillips Hue. I’m sure that if you don’t have any of these devices at your home you have been tempted to buy one of these but they definitely aren’t cheap.

But if you are a long time Smart Home aficionado like me and still use those old cheap X10 devices you can still control them with the Amazon Echo with the If-This-Then-That free web service and a Linux server.

This is all you need:

  • CM19A X10 USB PC Transceiver. It’s like $14.99 on eBay. The CM15A also works but it’s like $80.
  • An X10 Wireless Transceiver module for the CM19A. You don’t need it for the CM15A. You might already have this if you have an X10 wireless remote control. The TM751, for example, is just $19.99.
  • A Linux server that will be on 24/7.
  • A Web Server like Apache.
  • A little software called mochad to control the CM19A/CM15A on a Linux machine. It’s free.
  • A domain name for your server. I suggest FreeDNS. It’s free.
  • An account on IFTTT.com. Also free.

As usual in this blog all commands here are for Debian, and they should work in Debian-based distributions like Ubuntu, Mint and Raspbian for the Raspberry Pi.

Installing the X10 Gateway: mochad

The first thing we need to do is to install mochad, a TCP gateway daemon for the X10 interfaces CM15A and CM19A. It’ll run on ports 1099 – 1101 so you need to be sure those ports are free in your server. You can change the ports by editing the file mochad.c and changing the line:

#define SERVER_PORT     (1099)

With the starting port that you want. To build and install the mochad server (no surprises here) use the following very familiar sequence of commands after you have uncompressed the package:

root@htpc ~/mochad-0.1.16 # ./configure ; make ; make install

And that will install the binary /usr/local/bin/mochad and the udev rules in /etc/udev/rules.d/91-usb-x10-controllers.rules.

Testing the Installation

Now plug your X10 module to the Linux server and check the logs. You should see something like this (I have a CM19A):

[16140.737104] usb 1-2: new low-speed USB device number 7 using xhci_hcd
[16140.926146] usb 1-2: New USB device found, idVendor=0bc7, idProduct=0002
[16140.926152] usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[16140.926155] usb 1-2: Product: USB Transceiver
[16140.926158] usb 1-2: Manufacturer: X10 Wireless Technology Inc
[16140.926440] usb 1-2: ep 0x81 - rounding interval to 64 microframes, ep desc says 80 microframes
[16140.926449] usb 1-2: ep 0x2 - rounding interval to 64 microframes, ep desc says 80 microframes

Instantly the udev should kick in and execute the mochad binary. You can check for that using the process status command:

naikel@htpc ~ $ ps aux | grep mochad
root 10888 0.0 0.0 95340 1948 ? Ssl Mar12 0:00 /usr/local/bin/mochad

And you can now telnet to the 1099 port and send some commands. Use rf for the CM19A and pl for the CM15A like this:

naikel@htpc ~ $ telnet localhost 1099
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
rf a2 on
03/24 12:16:03 Tx RF HouseUnit: A2 Func: On
rf a2 off
03/24 12:16:09 Tx RF HouseUnit: A2 Func: Off

I turned on the lamp module on with the X10 house code A and the unit code 2, and then off. Read the README file for mochad for the list of commands available.

Installing Apache

If you don’t have the Apache Web Server installed (and that would be very weird if you’re reading this blog) you can install it using apt-get:

naikel@htpc ~ $ sudo apt-get install apache2

Since I’ll be writing my scripts with PHP you’ll need that too:

naikel@htpc ~ $ sudo apt-get install libapache2-mod-php5

To test that everything is working you can do this:

naikel@htpc ~ $ echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test.php  
<?php phpinfo(); ?>

And then try to open http://localhost/test.php. If it shows all the PHP Configuration then you’re ready.

Create a Dynamic Domain Name for your Server

I will not go deep into this, but you need a domain name for your server. Chances are your IP isn’t static and it changes from time to time, so you can’t specify a URL with an IP address on the IFTTT website because eventually it will stop working.

Create an account in FreeDNS and go to Subdomains and click on Add. Select any domain name you want (there are way too many to choose), choose a subdomain and type the IP of your Linux server there. If you’re reading this from that server you can find what your IP address is just by asking that to google.

Here’s an example for echo.echoparklake.com (echoparklake.com is a free domain in FreeDNS):

FreeDNS

Finally and I won’t cover it here, choose a Dynamic DNS client to keep your IP address updated. There’s a big list here. If you run Debian you can install inadyn using apt-get.

Create a Virtual Host Configuration

Now that you have your domain name let’s create a Virtual Host configuration in the Apache Web Server. I’m going to use the folder /opt/alexa to store my script. The tree will look like this:

/opt/alexa
├── etc
│   └── x10.ini
└── ifttt
    └── index.php

We will have a configuration file named x10.ini where all our devices are defined. We’ll get into that later. Let’s create the file /etc/apache2/sites-available/echo.echoparklake.com:

<VirtualHost *:80>
	ServerName echo.echoparklake.com
	DocumentRoot /opt/alexa/

	<Directory /opt/alexa/ifttt/>
		Options FollowSymLinks
		AllowOverride None
		Require all granted
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error-echo.log
	CustomLog ${APACHE_LOG_DIR}/access-echo.log combined
</VirtualHost>

Now create a symbolic link in /etc/apache2/sites-enabled to this file and restart your Apache:

root@htpc:/etc/apache2/sites-enabled# ln -s ../sites-available/echo.echoparklake.com
root@htpc:/etc/apache2/sites-enabled# apache2ctl graceful

Create the PHP Script

Create the folders /opt/alexa/etc and /opt/alexa/ifttt like this:

root@htpc:/opt# mkdir -p /opt/alexa/etc /opt/alexa/ifttt

Design the Configuration File

We are going to need a configuration file where we can define words for our X10 devices. For example, when I say “lamp” the script will know the X10 code of that lamp is X10 House Code: A, X10 Unit Code: 6, or just A6. When I say “air conditioner” or just “ac” for short, the script will know the X10 code is A4.

Since PHP has already builtin functions to read a .INI configuration file, we are going to use that format. We will use the sections of the INI file to define an appliance and the keys to define the X10 code and optionally how many “steps” we want to dim or brighten a light (I’m sure you already found out that some X10 light modules don’t dim much when you press the dim button just once, and you need to press it like four times to see a change).

Also we will define a special section called general to configure general things like the server and the port.

Create the configure file at /opt/alexa/etc/x10.ini. Here’s mine:

; Alexa X10 configuration file

[general]
hostname = localhost
port = 1099

[lights]
code = a2
dim = 1

[bathroom_lights]
code = a3
dim = 1

[air_conditioner]
code = a4

[ac]
code = a4

[lamp]
code = a6
dim = 4

I have four devices configured in this example. The script will read this configuration file, create a TCP connection to the hostname and port configured in this file, and then send a command. You can read the script here.

Install and Test the PHP Script

Finally copy the script to /opt/alexa/ifttt/index.php. Test it with opening a URL like this:

http://echo.echoparklake.com/ifttt/index.php?action=on&device=lights

You have to change echo.echoparklake.com with the domain name you created in FreeDNS. If that turned on the lights you defined in the file x10.ini (in my case the A2 code) then it worked.

Create a new IFTTT recipe

IFTTT supports the Amazon Echo and you can do some things with it. You first have to connect your IFTTT account with your Amazon Echo clicking here.

Basically we are going to use the trigger command to trigger an IFTTT action (hence the name!). This way you can say “Alexa trigger lights on” to the Echo, and the Echo will trigger this action in IFTTT; then IFTTT will send a request to your Web Server, and your Web Server connects to the mochad X10 gateway which sends an RF signal to your X10 transceiver and turns on the lights.

This part is very simple. The idea is to create a recipe for each action (on / off / dim / bright) on each device. It will take some time of course but you have to do it only once.

In the IFTTT website, click on My Recipes and then on Create New Recipe.

Choose Amazon Alexa as the Trigger Channel

IFTTT_AmazonAlexa

Now choose Say a specific phrase for your trigger.

IFTTT_Trigger

Then you have to choose the phrase. Let’s do the “lights on” phrase so go ahead and type lights on.

Add the Maker Action Channel to create requests to web servers.

IFTTT_Maker

And then just type the URL you need to turn the lights on. It would be like this:

http://echo.echoparklake.com/ifttt/index.php?action=on&device=lights

Select method GET and that’s it. Press the Create Action button and now you can go ahead and tell your Echo Alexa trigger lights on”  and your lights should turn on.

If you have any questions or thoughts don’t hesitate to leave a comment below!


15 thoughts to “Control your X10 Devices with the Amazon Echo using IFTTT”

  1. Awesome job with this article! I just got this setup working. One minor comment, I believe the if statement inside index.php should have the condition set to ‘!isset…’ as shown below to get dimming working properly. Thanks.

    Line 28: if (($action == “dim” || $action == “bright”) && !isset($ini_x10[$appliance][‘dim’]))

    1. Thank you for your comment! I think I should have elaborated a little more about the dim and brighten. The problem is that “dim” and “bright” only work with lights so you should have a dim value in the INI file. You can’t dim an appliance module obviously. If it’s present it will represent that this is a light switch or module and second how many “steps” you want to dim or bright. Example:

      [lamp]
      code = a6
      dim = 4
      name = lamp
      description = Night stand light

      Since the “dim” value is present, that line works with isset($ini_x10[$appliance][‘dim’], and also it will send 4 dim (or bright) signals every time you command to do it.

      Amazon has released a native Smart Home Skill API so you don’t need to say “trigger” and do this via IFTTT. You now can do it directly and say natural things like “Alexa turn on the lights”. I wonder if I should write an article about it. It’s pretty simple and works better.

      1. I would appreciate an article about Smart Home Skill API or at least a few pointers. I’m about ready to try this. Thanks!

        1. Yes, I’ve been thinking about writing a post for the native Smart Home Skill API. I’ll try to write it this weekend. You need to have all of this ready though (except the IFTTT part) since it will use the same interface (HTTP requests to a Web Server you control).

  2. Is there a way to specify in the ini file and php file whether to send the command over rf vs pl? I have been editing and trying to figure this out myself to no avail. I’ve got several devices, and they all work from the terminal in the pi when I manually input it, but via the html only the rf devices will work, unless I change this line in the php from rf to pl “$command = “rf ” . $ini_x10[$appliance][‘code’] . ” ” . $action . “\n”;” but when I edit that to pl then my rf devices obviously don’t respond.

    Anyone have any suggestions I’d appreciate it. I’m in the process of upgrading to newer tech, but I’ve got a ton of x10 that I’ve been using for over a decade and the newer tech is expensive, so I’ve only been upgrading a device or two at a time when I’ve got the money.

    1. This is extremely easy. Just add a new line in the INI file, let’s say method and change this line:

      $command = $ini_10[$appliance][‘method’] . ” ” . $ini_x10[$appliance][‘code’] . ” ” . $action . “\n”;

      Here’s the whole PHP file for you: http://blog.thescorpius.com/wp-content/uploads/2016/03/index2.php_.txt

      But you have to define the method for every single device you have. Example:

      [air_conditioner]
      code = a4
      method = rf

      I’m writing a new article to configure the Native Smart Home API. This way you say “Alexa turn on the lights” without IFTTT! (But you need to do all the stuff in this article first so don’t worry, it’s not wasted work).

      1. Awesome, thanks. I really appreciate it. Turns out I was actually pretty close, but I was missing some much needed punctuation on my edited script. Also, I found that using the ubi portal instead of IFTTT works much faster. IFTTT at imes took as long as a minute to actually execute the http request, but the ubi portal can make requests on your local network, so it happens almost immediately. Also, an added side bonus of the ubi portal is Alexa will respond with custom voice replies that you can setup. I’ve got her using some GlaDos portal quotes when she has confirmed the voice request.

      2. Hi, did you finish the article to configure the Native Smart Home API? IFTTT doesn’t work here in the UK with Alexa so looking to control my Pi X10 setup using something else from IFTTT.

  3. I got x10 working with Smartthings and it was easier than anything I have seen so far.. I am going to post this on a few of the places that asked about x10 and smartthings (so some of you may see this post twice) Enjoy my first Smartthings video https://youtu.be/yApSfnPbZpI

  4. I am stuck. When I try to test the server, I get an error that the requested URL /ifttt/index.php was not found on this server. I am using a pi running apache2.4.25. I have checked and double checked the files and the symbolic link and they all look correct. On the 404 error screen, it prints my domain name correctly. If you can point me to something to try, I would surely appreciate it.

Leave a Reply