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) 1 #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 1 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 1 2 3 4 5 6 7 [ 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 1 2 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 1 2 3 4 5 6 7 8 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 1 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 1 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(); ?> 1 2 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):

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 1 2 3 4 5 / 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> 1 2 3 4 5 6 7 8 9 10 11 12 13 < 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 1 2 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 1 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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ; 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

Now choose Say a specific phrase for your 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.

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!



