Skip to content
M Hightower edited this page Jan 1, 2019 · 38 revisions

Accessing X10 Modules with Amazon Echo

Overview

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

The earlier generation Amazon Echo devices use a local LAN based protocol to control 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. Note this method may not work with later generations of Amazon Echo due to reliance of Cloud based control methods. 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 computer's perspective, this is a very slow process. So x10ctrl.sh manages a named pipe to queue X10 commands and slowly feed them out with br. This gives us the ability to handle burst of X10 commands, that can be generated by an Alexa group command.

BWS Systems' instructions calls for running the habridge service as root so that it can use port 80. Rather than having ha-bridge run with root privileges, we will have the service habridge runs under its own user of the same name, habridge. NGINX or authbind can be used to access to port 80 from the non-root privileged user, habridge.

Hardware required:

  • Raspberry Pi 3 or Raspberry Pi 2 with stable Power Adapter (5.1+ Volt)
    • Running 2018-11-13-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 Raspbian.

  • 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, you will want to enable ssh service on the Raspberry Pi.
  • Make sure your Raspbian is up to date. Something like this:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

Dedicate an account to run ha-bridge

In the commands listed below we will: Create a 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 src log data etc
ln -s /opt/habridge/log /var/opt/habridge/log

Download and set a symbolic link to ha-bridge program

The commands shown in the block below will: Download the ha-bridge java program, install, and set up 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.2.1/ha-bridge-5.2.1.jar
ln -s /opt/habridge/src/ha-bridge-5.2.1.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 master.zip

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 9311637 Mar 16  2018 ha-bridge-5.2.1.jar
-rw-r--r-- 1 habridge habridge     637 Dec 20 21:26 habridge.service
-rw-r--r-- 1 habridge habridge     635 Dec 20 21:26 habridge.service-nginx

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 20 21:35 ha-bridge.jar -> /opt/habridge/src/ha-bridge-5.2.1.jar
-rwxr-xr-x 1 habridge habridge 2195 Dec 20 21:26 start.sh
-rwxr-xr-x 1 habridge habridge 6628 Dec 20 21:26 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/data /etc/opt/habridge
cp /opt/habridge/src/habridge.service /etc/systemd/system/habridge.service

Allowances have been made to support using NGINX instead of authbind; however, that process is not detailed at this time.

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

Problems with Network not up when habridge start

On my Network, habridge service was starting before the IPv4 interface was up. To work around this, I installed a service that stalls network-online, until an IPv4 interface is active. For details follow this link

Install bottlerocket program

You need to install bottlerocket program br.

apt-get install bottlerocket

Sometime the bottlerocket install tries too hard to be helpful and create a symbolic link named firecracker back to the serial port on /dev/ttsS0. Unfortunately the Raspberry Pi does not have a /dev/ttyS0. You must remove this link before proceeding. It will interfere with the udev rules for setting up the symlink to the ttyUSBn device.

Check for symbolic link:

ls /dev/firecracker -l

Example results:

lrwxrwxrwx 1 root root 5 Dec 15 22:12 /dev/firecracker -> ttyS0

Cleanup:

rm /dev/firecracker

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 could use udevadm info -a -n /dev/ttyUSB0 to gather the information needed for the rules file entry; however, We will use the script ls-ttyusb.sh to handle the drudgery of gather the information and composing the /etc/udev/rules.d/99-usb-serial.rules file. See Wiki page for ls-ttyusb.sh details.

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.

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

udevadm control --reload-rules && udevadm trigger

We are done with root level access. You should exit now.

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 fills 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 - Optional, I select Switch.
  • Map Type (Legacy) - Optional, "You can select Execute Command/Script/Program if you like. Does not appear to serve any purpose at this time.
  • Target - Optional, I like to enter X10 as a reminder of the module family.
  • Map ID - Enter you House/Unit code for your X10 Module. eg. A1
  • On Items
    • Type - Select Execute Command/Script/Program
    • Target Item - Type in /opt/habridge/bin/x10ctrl.sh ${device.mapId} on
    • Manage - Click the Add button
  • Dim Items
    • Type - Select Execute Command/Script/Program
    • Target Item - Type in /opt/habridge/bin/x10ctrl.sh --dim ${device.mapId} ${intensity.math( (255 - X ) / (255/6) )} ${intensity.percent}% ${device.name}
    • Manage - Click the Add button
  • Off Items
    • Type - Select Execute Command/Script/Program
    • Target Item - Type in /opt/habridge/bin/x10ctrl.sh ${device.mapId} off
    • Manage - Click the Add button.

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

Next, near the top 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 the living room light." If everything is working, your light will come on.

APPENDIX

Other reading and references material that provided inspiration and background information: