Skip to content
M Hightower edited this page Dec 31, 2017 · 38 revisions

Notes for connecting Amazon Echo and X10 together.

The objective is to give Amazon Echo, Alexa, the ability to turn on and off X10 devices.

Amazon Echo has native support for the Philips Hue light system. BWS Systems' ha-bridge emulates a Philips Hue light system providing a path for Amazon echo to interact with devices it does not natively support. We leverage these features to create an X10 Bridge to Alexa. By connecting together a Raspberry Pi 3, USB Serial Adapter, X10 FireCracker, and essential software. With this we can build a path for Alexa to command X10 devices.

Ha-bridge supports many action methods for responding to Phillip Hue light commands. Of these methods we use its ability to run a bash script, x10ctrl.sh. This script buffers/queues and sends X10 RF commands via bottlerocket, br.

Bottlerocket is a linux program that drives the X10 FireCracker. The FireCracker is powered and signaled by toggling the RS232 control lines. From the computers perspective this is very slow process. So x10ctrl.sh manages a named pipe to queue the X10 commands and slowly feed them out with br.

Rather than having ha-bridge run with root privileges, The service habridge runs under its own user of same name, habridge. authbind is used to allow permission to port 80 from the non-root privileged user, habridge.

Prerequisite Reading and References:

Hardware required:

  • Raspberry Pi 3 or Raspberry Pi 2 with stable Power Adapter (5.1+ Volt)
    • Running 2017-11-29-raspbian-stretch.img.
  • USB Serial Adapter - has USB connector for the computer side and a 9 pin "D" style connector for the RS232 side that will connect to the X10 FireCracker. This needs full RS232 voltage levels, not the TTL stuff. RS232 drive levels are +/- 5.0 Volts minimum.
  • X10 FireCracker, CM17A

Setup on the Raspberry Pi

Perform general preparations your Raspberry Pi by installing and setting up with the latest Rasbian.

  • Remember to change the default password. Make note of it in a safe place.
  • If your preferred method for reading instructions and running them, is to copy from a browser screen and paste to an ssh connection, your will want to enable ssh service on the Raspberry Pi.

Need default Java to be set to Java 8 for ha-bridge.

With 2017-11-29-raspbian-stretch.img, this no longer appears to be needed.

This command will show what is running and available. (source)

pi@raspberrypi:~ $ sudo update-alternatives --display java
java - manual mode
  link currently points to /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/bin/java
/usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java - priority 1063
  slave java.1.gz: /usr/lib/jvm/java-7-openjdk-armhf/jre/man/man1/java.1.gz
/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/bin/java - priority 318
  slave java.1.gz: /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/man/man1/java.1.gz
Current 'best' version is '/usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java'.

If you need to switch the default Java version among available Java JREs.

Run the following command:

pi@raspberrypi:~$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                                   Priority   Status
------------------------------------------------------------
 *0            /usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java          1063      auto mode
  1            /usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java          1063      manual mode
  2            /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/bin/java   318       manual mode

Press enter to keep the current choice[*], or type selection number: 2

In this case enter 2 to select .../jdk-8-oracle-arm32-vfp-hflt/jre/bin/java

Dedicate an account to run ha-bridge

In the commands listed below we will: Create user, habridge, for ha-bridge to run under, and add users root and pi to the habridge group. Build habridge directory tree under /opt. Link the new tree under the user habridge's directory.

If you are doing copy/paste of the commands listed. Do the first two lines first. The adduser command will want to ask some questions.

sudo -i
adduser --disabled-login habridge

A copy paste of the commands listed here should work.

adduser habridge dialout
adduser root habridge
adduser pi habridge
chmod ug+s ~habridge
mkdir /opt/habridge
mkdir /var/opt/habridge
chown habridge:habridge /opt/habridge  /var/opt/habridge
chmod ug+s /opt/habridge
su habridge
ln -s /opt/habridge ~habridge/opt
cd /opt/habridge
mkdir bin etc src log data
ln -s /opt/habridge/log /var/opt/habridge/log

Download and set symbolic link to ha-bridge program

The commands shown in the block below will: Download the ha-bridge java program, install, and setup a link to the java program from the /opt/habridge/bin directory.

cd /opt/habridge/src
wget https://github.com/bwssytems/ha-bridge/releases/download/v5.1.0/ha-bridge-5.1.0.jar
ln -s /opt/habridge/src/ha-bridge-5.1.0.jar /opt/habridge/bin/ha-bridge.jar

Download and install some more files

These files will be added:

This block of commands will: Change to user habridge's home directory, create a link over to the /opt/habridge directory, and restore the downloaded files to /opt/habridge/src and /opt/habridge/bin. The symbolic link can be removed once finished. Or left for installing updates, when available.

cd ~habridge
wget https://github.com/mhightower83/x10ctrl/archive/master.zip
ln -s /opt/habridge ~habridge/x10ctrl-master
unzip master.zip
rm x10ctrl-master # optional, link may be useful for applying future updates.

As shown below, verify you have the following files in the /opt/habridge/src directory.

ls -l /opt/habridge/src

...
-rw-r--r-- 1 habridge habridge 8341874 Dec 12 13:54 ha-bridge-5.1.0.jar
-rw-r--r-- 1 habridge habridge     392 Dec 22 19:32 habridge.service

As shown below, verify that your files and permissions in directory /opt/habridge/bin look similar to this:

ls -l /opt/habridge/bin

...
lrwxrwxrwx 1 habridge habridge   37 Dec 16 19:01 ha-bridge.jar -> /opt/habridge/src/ha-bridge-5.1.0.jar
-rwxr-xr-x 1 habridge habridge  754 Dec 22 19:32 start.sh
-rwxr-xr-x 1 habridge habridge 5127 Dec 22 19:32 x10ctrl.sh

If needed, set permissions as shown:

chmod 744 /opt/habridge/bin/start.sh /opt/habridge/bin/x10ctrl.sh

Setup some system stuff

In this block of commands, we exit the user habridge's account and add some system links as needed.

exit
whoami                # this should report root
ln -s /opt/habridge/etc /etc/opt/habridge
ln -s /opt/habridge/src/habridge.service /etc/systemd/system/habridge.service

In this block we install and setup authbind so a non root user, habridge, can create a socket on port 80. If you are doing copy/paste, this must be done into two separate copy/paste steps. One with the apt-get command by itself, then the remainder as a second copy/paste.

apt-get install authbind

cd /etc/authbind/byport
touch 80
chmod 700 80
chown habridge:habridge 80

These commands will enable and start the habridge service

systemctl daemon-reload
systemctl start habridge.service
systemctl enable habridge

Use the command below to check the service's status.

systemctl status habridge.service -l

Install bottlerocket program

You need to install bottlerocket program br.

apt-get install bottlerocket

Create a Persistent name for your USB Serial Adapter

With USB, the device name assigned to a USB device may not always be the same. To make the name we reference be persistent, we create a udev rules file entry in /etc/udev/rules.d/99-usb-serial.rules. You can use udevadm info -a -n /dev/ttyUSB0 to gather the information needed for the rules file entry.

An alternative would be to try this utility to gather the information for your /etc/udev/rules.d/99-usb-serial.rules file: ls-ttyusb.sh.

Typical use example:

ls-ttyusb.sh --rules firecracker

A typical output result:

# /dev/ttyUSB0   Bus 001 Device 008: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
# SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="00000001", ENV{ID_USB_INTERFACE_NUM}=="00", SYMLINK+="firecracker"
#
# /dev/ttyUSB1   Bus 001 Device 009: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
# SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", ENV{ID_USB_INTERFACE_NUM}=="00", SYMLINK+="firecracker"
# *** This device does not have a serial number attribute. ***

Verify and edit the result as needed. Un-comment the rules line that matches your serial adapter. Add the results to your /etc/udev/rules.d/99-usb-serial.rules file.

But, first we need to find the current /dev/ttyUSB... device name for your USB serial adapter. This command can help generate a short list of the ttyUSB devices you have:

stat -c"%F %n" /dev/* | grep -E ttyUSB[0-9]+$ | grep -E ^character | cut -d\  -f4

If there are multiple /dev/ttyUSB... present, unplug/leave the USB Serial adapter you plan to use with the firecracker disconnected. Run the above command to get a list of the existing ttyUSB devices. Now plug your USB Serial adapter in. Run the command again. You should now be able to spot the current/new /dev/ttyUSB... device name for your USB Serial adapter. You will use this device name in subsequent commands to gather data for creating the udev rules entry.

For a simple USB environment the serial number may be enough to uniquely identify the serial port, you are connecting the firecracker to.

Example rules entry:

SUBSYSTEM=="tty", ATTRS{serial}=="YOURSERN", SYMLINK+="firecracker"

A command like this can help generate a line for your rules file. Be sure to replace /dev/ttyUSB0 with that of the device you are using.

echo $( udevadm info -a -n /dev/ttyUSB0 | grep -f <( echo -e "SUBSYSTEM=\n{serial}" ) | head -2 | tr '\n' ',') SYMLINK+=\"firecracker\">>99-usb-serial.rules

If the serial number does not exist, is not unique, or the device has multiple serial ports, more qualifiers will be needed.

Example rules entry for single serial port device:

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{serial}=="YOURSERN", ATTRS{idProduct}=="6001", SYMLINK+="firecracker"

This command may work for single serial port device:

echo $( udevadm info -a -n /dev/ttyUSB0 | grep -f <( echo -e "SUBSYSTEM=\n{idVendor}\n{idProduct}\n{serial}" ) | head -4 | tr '\n' ',') SYMLINK+=\"firecracker\">>99-usb-serial.rules

For a USB Serial Adapter with multiple serial ports the ENV{ID_USB_INTERFACE_NUM}=="00" qualifier will be need to be added. Adjust "00" to "01", "02", ... until you are able to select the serial port you want to use.

Example rules entry for a multiple serial port device:

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{serial}=="YOURSERN", ATTRS{idProduct}=="6010", ENV{ID_USB_INTERFACE_NUM}==01, SYMLINK+="firecracker"

This command may work for multiple serial port device. Evaluate and review results carefully:

echo $( udevadm info -a -n /dev/ttyUSB0 | grep -f <( echo -e "SUBSYSTEM=\n{idVendor}\n{idProduct}\n{serial}\n{bInterfaceNumber}" ) | head -5 | tr '\n' ',' | sed s/ATTRS{bInterfaceNumber}/ENV{ID_USB_INTERFACE_NUM}/ ) SYMLINK+=\"firecracker\">>99-usb-serial.rules

This command should cause updated rules to be applied without a reboot. You may need to unplug/plugin your device for the new results to occur.(ref)

udevadm control --reload-rules && udevadm trigger

Your X10 HA Bridge should now be ready for browsing

Connect from a browser to your new X10 HA Bridge. First add just one X10 device so you can confirm everything is working.

Select the Add/Edit Button.

On the Edit/Copy a device page fill in the following minimum fields. (Note, not all fields are used, when running a script):

  • Name - Type in the name you will speak to identify the device eg. living room
  • Device Type - Select Execute Script/Program
  • On Items
    • Type - Select Execute Commands/Script/Program
    • Target Item - Type in /opt/habridge/bin/x10ctrl.sh A1 on (replace A1 with your device's House/Unit code)
    • Manage - Click the Add button
  • Off Items
    • Type - Select Execute Commands/Script/Program
    • Target Item - Type in /opt/habridge/bin/x10ctrl.sh A1 off (replace A1 with your device's House/Unit code)
    • Manage - Click the Add button.

Now click on the Add Bridge Device button. (toward the top of the screen)

Now go to the top and click on the Bridge Devices button. You should now see the device you just added. Toward the right are some action buttons. Click on Test ON and Test OFF to verify your configuration works.

Now you can say "Alexa, discover devices." If all goes well, she will report finding 1 device. You can then say: "Alexa, turn on living room light." If everything is working, your light will come on.

Clone this wiki locally