The plan is to make use of these cheep switch modules from Sonoff to operate some lamps in our living room. We’re going to replace the stock firmware and use openHAB’s build in support for MQTT to control them.
Configure openHab 2 to use MQTT
It is beyond the scope of this article to explain MQTT, suffice to say to it is a lightweight message protocol specifically designed for use in IoT applications.
First we need to install an MQTT broker somewhere. The same Raspberry Pi running our openHAB 2 is an ideal choice. Fortunately the openHABian configuration tool can install and configure Mosquitto for you.
- SSH to
openhabianpi
. - Run the openHABian configuration tool.
sudo openhabian-config
- Open menu Option 20 Optional Components, then Option 23 Mosquitto. Don’t forget to set a password.
Next we are going to install the MQTT binding in openHAB 2.
- Open the openHAB 2 dashboard http://openhabianpi:8080.
- Select the Paper UI, this is used for configuring openHAB 2.
- Navigate to Add-ons in the left hand menu, then the BINDINGS tab.
- Find the MQTT Binding in the list and install it.
- Also, the MQTT Action on the ACTIONS tab.
- Finally, the JSONPath and Map Transformations on the TRANSFORMATIONS tab.
Now we are going to configure the MQTT service. Your openHABian configure a number of Samba shares to make this easier.
-
- Open the text file \\OPENHABIANPI\openHAB-share\openhab2-conf\services\mqtt.cfg, I use NotePad++ for this.
- Uncomment the line:
#[broker].url=tcp://localhost:1883
Delete the # symbol and replace [broker] with the name you want to give your broker.
- We also need to set the user, pwd, retain, and async parameters. Ending up with something looking like this:
mybroker.url=tcp://localhost:1883 mybroker.user=openhabian mybroker.pwd=mypassword mybroker.retain=false mybroker.async=false
- Check the logs at http://openhabianpi:9001/ for a couple of lines looking like this:
2017-12-16 21:16:48.377 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed. 2017-12-16 21:16:48.379 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'mybroker'
Next let’s see if it works.
-
- Create an items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\default.items, and add the following lines:
-
Switch mySwitch {mqtt=">[mqbroker:myhouse/office/light:command:ON:ON],>[mqbroker:myhouse/office/light:command:OFF:OFF]"} Switch myLamp "lamp [%s]" {mqtt="<[mqbroker:myhouse/office/light:command:default]"}
Create a sitemap file: \\OPENHABIANPI\openHAB-share\openhab2-conf\sitemaps\default.sitemap, and add the following lines:
sitemap default label="My first sitemap" { Switch item=mySwitch label="My Switch" Switch item=myLamp label="My Lamp" }
- In Paper UI, navigate to Services and select Basic UI.
- Finally we enter the name of out sitemap default, and save.
Now if we open the dashboard http://openhabianpi:8080 and select Basic UI we should see our site:
Try toggling the switch, you can see the connected lamp turn on (after a refresh).
Looking in the logs http://openhabianpi:9001/ you can see the events:
2017-12-16 23:28:06.093 [ItemCommandEvent ] - Item 'mySwitch' received command OFF
2017-12-16 23:28:06.099 [ItemStateChangedEvent ] - mySwitch changed from ON to OFF
2017-12-16 23:28:06.108 [ItemCommandEvent ] - Item 'myLamp' received command OFF
2017-12-16 23:28:06.113 [ItemStateChangedEvent ] - myLamp changed from ON to OFF
To debug further, you can install an MQTT client, like the Chrome plugin MQTTlens. Once connected to your broker, you can subscribe to the topic we used myhouse/office/light
and watch the messages as they are sent.
One thought on “DIY Home Automation with openHAB 2 – Part 2”