2020-08-11
md
HA Bridge on Armbian Working with Domoticz and Alexa
HA Bridge and Alexa Device Discovery-> <-Using HA Bridge with the Echo Dot 3rd Generation
<-New Hardware (Episode 2): IP Camera, Itead Sonoff Goodies, and Amazon Echo Dot

As I speculated, HA Bridge is a very good proxy between Amazon Alexa and IoT devices defined in Domoticz. Installation was straightforward, aside from a little bit of fumbling adding Java which is a prerequisite. The details are provided below.

It would be just as viable to install HA Bridge of the same Raspberry Pi that is hosting the home automation server. However, that Pi has a relatively anemic single core ARMv6 CPU so spreading the load may be a good strategy.

Table of contents

  1. Installing Java
  2. Installing HA Bridge
  3. Importing Domoticz Devices
  4. Using HA Bridge Devices with Alexa
  5. UPNP
  6. HA Bridge Database Backup
  7. Conclusion

Installing Java toc

Begin with the usual update and upgrade before attempting to install the required Java SDK.

zero@opi:~$ sudo apt update && sudo apt upgrade -y ... The following packages will be upgraded: armbian-config armbian-firmware armbian-tools-stretch linux-dtb-next-sunxi linux-image-next-sunxi linux-u-boot-opi-next 6 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 26.4 MB of archives. After this operation, 793 kB disk space will be freed. ... Unpacking armbian-config (5.76) over (5.74) ... ... Unpacking armbian-firmware (5.75) over (5.73) ... ... Unpacking armbian-tools-stretch (5.75) over (5.69) ... ... Unpacking linux-dtb-next-sunxi (5.75) over (5.73) ... ... Unpacking linux-image-next-sunxi (5.75) over (5.73) ... ... Unpacking linux-u-boot-opi-next (5.75) over (5.73) ... ... Setting up linux-image-next-sunxi (5.75) ... update-initramfs: Generating /boot/initrd.img-4.19.20-sunxi update-initramfs: Converting to u-boot format Setting up armbian-tools-stretch (5.75) ...

After the update, Armbian is using version 4.19 of the Linux kernel.

zero@opi:~$ uname -a Linux opiz 4.19.62-sunxi #5.92 SMP Wed Jul 31 22:07:23 CEST 2019 armv7l GNU/Linux

There is an "untrusted" PPA (Personal Package Archive) belonging to the "WebUd8" team which contains the Oracle Java 8 runtime environment JRE. The instructions on that page do not include details about importing the GPG signing keys, while the page entitled Install Oracle Java 8 (both JDK8 and JRE8) in Debian contains the missing information.

zero@opi:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886 ... gpg: Total number processed: 2 gpg: imported: 2 zero@opi:~$ sudo add-apt-repository ppa:webupd8team/java Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK8). There are no actual Java files in this PPA. ... zero@opi:~$ sudo apt-get install oracle-java8-installer Reading package lists... Done ...

This is not an Open source package, and it is necessary to accept the licence agreement with two clicks.

After the last installation message is displayed, test for the presence of the run time interpreter and the compiler.

#####Important######## To set Oracle JDK8 as default, install the "oracle-java8-set-default" package. E.g.: sudo apt install oracle-java8-set-default On Ubuntu systems, oracle-java8-set-default is most probably installed automatically with this package. ###################### zero@opi:~$ java -version java version "1.8.0_201" Java(TM) SE Runtime Environment (build 1.8.0_201-b09) Java HotSpot(TM) Client VM (build 25.201-b09, mixed mode) zero@opi:~$ javac -version javac 1.8.0_201

Since javac, the compiler, is installed, the Java Development Kit (JDK) was installed. The JDK is a superset of the JRE capable of creating and compiling Java programs.

As can be seen, the version of Java that was installed was not open source which is why the Oracle licence had to be accepted. There is an open source version. I did not test it on Armbian, but I did install it on a Raspberry Pi 3 running Raspbian Stretch.

pi@berry:~ $ sudo apt install default-jre Reading package lists... Done ... 0 upgraded, 121 newly installed, 0 to remove and 0 not upgraded. Need to get 78.1 MB of archives. After this operation, 381 MB of additional disk space will be used. Do you want to continue? [Y/n] y ...

That did not go very smoothly. There was a problem with getting the source from the repository, so I retried a few minutes later. The whole package was downloaded but there were problems. I had to repeat yet again the install command and found that dpkg was having problems setting up ca-certificates. Others had bumped up against this problem and some clever people found the solution and posted it on the Raspberry Pi Forum topic missing jvm server. Either of the last two replies (as of the time of writing this) by gasi12 or by amal.barman give a working solution. I applied the last one.

pi@berry:~ $ cd /usr/lib/jvm/java-8-openjdk-armhf/jre/lib/arm pi@berry:/usr/lib/jvm/java-8-openjdk-armhf/jre/lib/arm $ sudo ln -s client server pi@berry:/usr/lib/jvm/java-8-openjdk-armhf/jre/lib/arm $ cd - pi@berry:~ $ sudo apt install default-jre ... W: APT had planned for dpkg to do more than it reported back (21 vs 25). Affected packages: ca-certificates-java:armhf pi@berry:~ $ java -version openjdk version "1.8.0_181" OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-2~deb9u1-b13) OpenJDK Client VM (build 25.181-b13, mixed mode)

As can be seen, the last installation did yield some complaints but the run time environment appear to be in place. After that I added HA Bridge as described below and it did work with OpenJDK.

I did not test this in Armbian. It does look as if OpenJDK could be installed along with Oracle JDK8 following How To Install Java with Apt on Debian 9 by Brian Hogan. I decided to leave well enough alone. Perhaps at a later date, I will try the OpenJDK on the Orange Pi Zero.


Installing HA Bridge toc

Installing HA Bridge is pretty straightforward following the instructions in Manual installation of ha-bridge and setup of systemd service. Just make sure to check the name of the latest release on the GitHub site. Currently this is version 5.2.2 Release candidate #3 available since November 14, 2018.

First download the latest release, which is a .jar file, and then create a unit file.

zero@opi:~$ mkdir ha-bridge zero@opi:~$ cd ha-bridge zero@opi:~/ha-bridge$ wget https://github.com/bwssytems/ha-bridge/releases/download/v5.2.2RC3/ha-bridge-5.2.2RC3.jar ... 2019-02-14 16:28:45 (2.56 MB/s) - ‘ha-bridge-5.2.2RC3.jar’ saved [9365754/9365754] zero@opi:~/ha-bridge$ sudo nano /etc/systemd/system/ha-bridge.service
[Unit] Description=HA Bridge Wants=network.target After=network.target [Service] Type=simple WorkingDirectory=/home/zero/ha-bridge ExecStart=/usr/bin/java -jar -Dconfig.file=/home/zero/ha-bridge/data/habridge.config /home/zero/ha-bridge/ha-bridge-5.2.2RC3.jar [Install] WantedBy=multi-user.target

Do not forget to adjust the WorkingDirectory and ExecStart lines so that the current user name is used.

zero@opi:~/ha-bridge$ sudo systemctl daemon-reload zero@opi:~/ha-bridge$ sudo systemctl start ha-bridge.service zero@opi:~/ha-bridge$ sudo systemctl enable ha-bridge.service zero@opi:~/ha-bridge$ sudo systemctl status ha-bridge.service ● ha-bridge.service - HA Bridge Loaded: loaded (/etc/systemd/system/ha-bridge.service; disabled; vendor preset: enabled) Active: active (running) since Thu 2019-02-14 16:40:05 AST; 12s ago Main PID: 8168 (java) Tasks: 19 (limit: 4915) CGroup: /system.slice/ha-bridge.service └─8168 /usr/bin/java -jar -Dconfig.file=/home/zero/ha-bridge/data/habridge.config /home/zero/ha-bridge/ha-bridge-5.2.2RC3.jar Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08,450 [main] INFO com.bwssystems.HABridge.devicemanagmeent.DeviceResource - HABridge device management service started.... Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08.624:INFO:oejs.session:Thread-0: DefaultSessionIdManager workerName=node0 Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08.625:INFO:oejs.session:Thread-0: No SessionScavenger set, using defaults Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08,640 [main] INFO com.bwssystems.HABridge.hue.HueMulator - Hue emulator service started.... Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08.646:INFO:oejs.session:Thread-0: Scavenging every 660000ms Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08.799:INFO:oejs.AbstractConnector:Thread-0: Started ServerConnector@1ad1dfc{HTTP/1.1,[http/1.1]}{0.0.0.0:80} Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08.803:INFO:oejs.Server:Thread-0: Started @3756ms Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08,811 [main] INFO com.bwssystems.HABridge.upnp.UpnpSettingsResource - Description xml service started.... Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08,847 [main] INFO com.bwssystems.HABridge.upnp.UpnpListener - UPNP Discovery Listener starting.... Feb 14 16:40:08 opi java[8168]: 2019-02-14 16:40:08,861 [main] INFO com.bwssystems.HABridge.upnp.UpnpListener - UPNP Discovery Listener running and ready....

Importing Domoticz Devices toc

HA Bridge has a Web-based interface at the default HTTP port: 80. Hence the URL to reach the bridge is http://opi.local or http://192.168.1.32 assuming that is the DHCP assigned address of the Orange Pi Zero or, in my case, because I set a static address using armbian-config.

Of course when first started, HA Bridge does not have links to any devices as can be seen on the initial login page screen.

However, it is very easy to import all of the devices already set up in Domoticz. Click on the Bridge Control button/tab.

And then scroll down to Domoticz Names and IP Addresses.

Only three fields had to be filled in: Name, which can be anything, IP and Port which must be the address of the Domoticz server. The latter's website is protected with a username and password but clients on the local network do not have to supply these as defined in its Setup/Settings page.

Once these three values are set, two buttons must be pressed. First the Add button to the right of the fields and then the Save button at the top of the page. As soon as that button is pressed, HA Bridge pops up a "wait until initialization is completed" message. I found it necessary to refresh the page after that message disappeared and then a new button/tab appeared: Domoticz Devices. Clicking on it, reveals list containing all devices, groups and scenes defined in Domoticzs.

I checked the devices and groups that I wanted to control with voice commands to Alexa on the Echo Dot. There were 14 of them. Then I clicked on the Bulk Add (14) button at the bottom of the page. I waited a little while, refreshed the page and found the added devices in the Bridge Devices tab.

Using the Test ON, Test OFF buttons, it quickly transpired that all the switches were correctly defined but that the groups and scenes did not work properly. That was because HA Bridge treated them as switches but Domoticz uses slightly different JSON URLs for switches and groups/scenes. Click on the Edit/Copy button of each scene or group and modify the Target Item of the On Items and Off Items. The change is minimal, &param=switchlight should be &param=switchscene as shown below.

If you wish, it is possible to change the device type to group or scene as appropriate. This is cosmetic and does not change anything.

Do not forget to click the Update Bridge Device button at the top of the page after both edits to complete the operation. Test that these scenes or groups now work with the Test ON, Test OFF buttons.

And that is basically it, HA Bridge is now functional. You can click on the My Echo button at the top of the page to let Alexa discover all the devices that HA Bridge now makes available.

Using HA Bridge Devices with Alexa toc

Because of my previous experimentation Amazon Alexa and HA Bridge, I had to undo some things. Unfortunately, I did not record exactly what I did, which in any case would not be very useful. Quickly, here is how I went about getting Alexa back to its initial status.

I used the Amazon Alexa web interface to initiate discovery. Apparently, this can be done with a verbal command but the Echo Dot is down two flights of stairs, and I am much too old to run up and down stairs and was too much in a hurry to leisurely do the round trip. I clicked on Smart Home then Devices and then on the Discover Devices button. I am "guessing" about these names translating them from the French terms.

I pressed the discover button twice, perhaps I was too impatient. And I may have refreshed the page but before long all the devices known to HA Bridge were found by Alexa.

At this point, it was possible to turn on or off any lamp or device with verbal commands.

Universal Plug and Play toc

Echo Dot finds devices on the network using the Universal Plug and Play (UPnP) protocols. When starting, the ha-bridge service advertises itself as a UPnP listener.

zero@opiz:~$ sudo systemctl status ha-bridge.service ● ha-bridge.service - HA Bridge Loaded: loaded (/etc/systemd/system/ha-bridge.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-08-11 16:31:05 ADT; 51s ago Main PID: 2226 (java) Tasks: 19 (limit: 4915) CGroup: /system.slice/ha-bridge.service └─2226 /usr/bin/java -jar -Dconfig.file=/home/zero/ha-bridge/data/habridge.config /home/zero/ha-bridge/ha-bridge-5.2.2.jar Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10.031:INFO:oejs.Server:Thread-0: jetty-9.4.z-SNAPSHOT, build timestamp: 2017-11-21T17:27:37-04:00, git hash: 82b8fb23f757335bb3329d540ce37a2a2615f0a8 Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10,137 [main] INFO com.bwssystems.HABridge.hue.HueMulator - Hue emulator service started.... Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10.208:INFO:oejs.session:Thread-0: DefaultSessionIdManager workerName=node0 Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10.209:INFO:oejs.session:Thread-0: No SessionScavenger set, using defaults Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10.232:INFO:oejs.session:Thread-0: Scavenging every 600000ms Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10.364:INFO:oejs.AbstractConnector:Thread-0: Started ServerConnector@a75fd6{HTTP/1.1,[http/1.1]}{0.0.0.0:80} Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10.367:INFO:oejs.Server:Thread-0: Started @4338ms Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10,373 [main] INFO com.bwssystems.HABridge.upnp.UpnpSettingsResource - Description xml service started.... Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10,420 [main] INFO com.bwssystems.HABridge.upnp.UpnpListener - UPNP Discovery Listener starting.... Aug 11 16:31:10 opiz java[2226]: 2020-08-11 16:31:10,425 [main] INFO com.bwssystems.HABridge.upnp.UpnpListener - UPNP Discovery Listener running and ready...

The Echo Dot, looking for connected devices, can obtain from ha-bridge its list of all connected devices. It works seamlessly and reasonably quickly if the UPNP service is enabled in the router. By default this capability is enabled on the consumer grade router supplied by my Internet service provider. However, some time ago, I disabled this feature and just now I have lost about a half an hour trying to figure out why Alexa could not find the latest added Wi-Fi switch running Tasmota even though HA Bridge picked it up from Domoticz without fail. Note to myself: do not forget to enable UPNP capability of the router when adding an IoT device.

And once the new devices has been added by Alexa, I must not forget to disable the UPNP capabilities. Just why I disable UPNP would take too long to explain here. Suffice to say that there are many articles on the Web on that very question.

HA Bridge Database Backup toc

After adding a device to the HA Bridge database, it is probably a good idea to backup the later. This is easily done in the Home page. The last entry in the page is Bridge Device DB Backup. If a + appears to the right of the title, click to expand the table.

Clicking on the Backup Device DB button creates a backup of the database that will be stored in the data directory.

zero@opiz:~$ ls -l ha-bridge/data total 60 -rw-r--r-- 1 root root 13887 Aug 11 16:22 device.db -rw-r--r-- 1 root root 11040 Jun 14 2019 device.db-2019-06-14-21-43-00.bk -rw-r--r-- 1 root root 11728 Jun 15 2019 device.db-2019-06-15-18-22-31.bk -rw-r--r-- 1 root root 13887 Aug 11 16:22 device.db-2020-08-11-17-01-27.bk -rw------- 1 root root 3117 Dec 16 2019 habridge.config

Presumably it would be very simple to restore any backed up database any time later. It would probably be a good idea to save the backup database and the configuration file to another machine.

Conclusion toc

I am glad to have found a good use for the Orange Pi Zero which is now enclosed in the very small black plastic cube that I purchased with it. It sits near the television set, not too far from the Echo Dot, running two programs: HA Bridge as outlined above and an IR remote control bridge I wrote in Python called ir-rem.py. Now I can turn on or off any IoT device in the house with verbal commands or with an IR remote. So far it all works very well. Time will tell how stable this solution is. Among things that could go wrong, I include a possible decision by Amazon, imitating Google, to remove local handling of Philips devices. If that happens, the IR remote will still be available.

Installation of HA Bridge and integration with Alexa was a pleasure. It took much less time to do that than to write it up. It seems pretty clear that the procedure would be quite similar if using other supported open source home automation programs such as OpenHab and Home Assistant or commercial products as well. Thank you very much BWS Systems.

I wish the third generation Echo Dot and the French Canadian version of Alexa had been available sooner.

HA Bridge and Alexa Device Discovery-> <-Using HA Bridge with the Echo Dot 3rd Generation
<-New Hardware (Episode 2): IP Camera, Itead Sonoff Goodies, and Amazon Echo Dot