DIY Home Automation with openHAB 2 – Part 2

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.

  1. SSH to openhabianpi.
  2. Run the openHABian configuration tool.
    sudo openhabian-config
  3. 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.

  1. Open the openHAB 2 dashboard http://openhabianpi:8080.
  2. Select the Paper UI, this is used for configuring openHAB 2.
  3. Navigate to Add-ons in the left hand menu, then the BINDINGS tab.
  4. Find the MQTT Binding in the list and install it.MQTT Binding
  5. Also, the MQTT Action on the ACTIONS tab.MQTT Action
  6. Finally, the JSONPath and Map Transformations on the TRANSFORMATIONS tab.JSON Path

Now we are going to configure the MQTT service. Your openHABian configure a number of Samba shares to make this easier.

    1. Open the text file \\OPENHABIANPI\openHAB-share\openhab2-conf\services\mqtt.cfg, I use NotePad++ for this.
    2. Uncomment the line:

      Delete the # symbol and replace [broker] with the name you want to give your broker.

    3. We also need to set the user, pwd, retain, and async parameters. Ending up with something looking like this:
    4. Check the logs at http://openhabianpi:9001/ for a couple of lines looking like this:
      2017-12-16 21:16:48.377 [INFO ] [] - 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.

    1. Create an items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\default.items, and add the following lines:
    2. 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"
    3. In Paper UI, navigate to Services and select Basic UI.
    4. Finally we enter the name of out sitemap default, and save.basic UI config

Now if we open the dashboard http://openhabianpi:8080 and select Basic UI we should see our site:basic UI

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. mqtt lens

Next time we’ll be configuring some actual hardware.

One thought on “DIY Home Automation with openHAB 2 – Part 2

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s