Home Automation

Enter Your Electronics & Design Project for a chance to win a $100 Shopping Cart!

Back to The Project14 homepage

Project14 Home
Monthly Themes
Monthly Theme Poll

 

OpenHAB 2 MQTT Binding for the MATRIX Creator

 

Overview

This post is a continuation of the openHAB 2 Binding set-up that was shown in a previous post.

When setting up a Home Automation project, there are many options to communicate with remote devices over the ether; IFTTT, Amazon Alexa Voice Services, Mozilla Things Gateway, Google Assistant, and many others. With openHAB, many of these cloud and web based tools can be implemented into a single easy to use UI.  In this project, since the intention for the time being is to keep the communication implementation local without the need of interfacing with the internet, outside of implementing the Exec Binding in openHAB 2 to send commands to the MATRIX Creator, the MQTT Binding was used to communicate with remote devices; a MKR1010 in this instance.  Quite briefly, MQTT (Message Queuing Telemetry Transport) is a light weight publish-subscribe messaging based protocol that works on top of the TCP/IP protocol.  It was first authored by Andy Standford-Clark of IBM and Arlen Nipper of Cirrus Link in 1999. In 2013 MQTT v3.1 was submitted to the OASIS specification body by IBM. There are many variations of the MQTT implementation and for this project the Eclipse Mosquitto MQTT Broker along with the Paho MQTT client were used.

 

Reference:

https://en.wikipedia.org/wiki/MQTT

 

Previous Posts:

OpenHAB 2 with Matrix Creator and RasPi 3 A+: Intro

OpenHAB 2 with Matrix Creator and RasPi 3 A+: MATRIX Lite Python

OpenHAB 2 with Matrix Creator and RasPi 3 A+: OpenHAB 2 Exec Binding

OpenHAB 2 with Matrix Creator and RasPi 3 A+: Everloop and demo

OpenHAB 2 with Matrix Creator and RasPi 3 A+: GPIO and Postmortem

 

Related Posts:

MKR WiFi 1010 - MQTT Remote Relay Board Control

[Pi IoT] -  Remote Horse Feeder System #6 : OpenHAB, Pi Face and MQTT

 

 

Mosquitto MQTT Broker install

  To orchestrate the MQTT message pass between node via topics, the Eclipse Mosquitto MQTT Broker was installed on the Raspberry Pi 3 A+ that is also hosting the MATRIX Creator.  The Eclipse MQTT Broker is an open source implementation of 3.1 and 3.1.1 versions of the MQTT protocol.  This has been been used in previous projects so there is a bit of familiarity with the tool making it easier to implement in this project.  The MQTT Broker will run on the Raspberry Pi and the openHAB 2 MQTT Bindings will be used to control the messages between nodes.

 

The first step is to install Mosquitto on the Raspberry Pi.  This can be performed with manually or using the 'apt-get' tool, however there seems to be an issue getting Mosquitto installed on the STRECH version of Raspbian, so the manual option was used.

    Ref: STRECH and MQTT -  https://www.raspberrypi.org/forums/viewtopic.php?t=191027

 

    1. Download  and install the Mosquitto bit to a local folder on the RasPi.

 

     Install Dependencies

 sudo apt-get install cmake libssl1.0-dev libwebsockets-dev uuid-dev

 

    Create a location for Mosquitto build the code.

mkdir mosquitto

cd mosquitto/

wget http://mosquitto.org/files/source/mosquitto-1.4.14.tar.gz

tar -zxvf mosquitto-1.4.14.tar.gz 

cd mosquitto-1.4.14/

cmake .

# cmake -DWITH_WEBSOCKETS=ON . if you need websockets support

make -j4

sudo make install

 

    2.  Once the code is built and install, run 'mosquitto' to ensure it is working.

         If successful, it should look like the following.

   

 

openHAB 2 MQTT Binding config

 

  With the Mosquitto MQTT Broker installed and running, use the openHAB 2 PAPER UI to add the MQTT Broker Binding and create a MQTT Broker Thing.

 

   1.  Open a browser to the openHAB 2 address and navigate to Add-Ons and Bindings and then scroll down to the MQTT Binding option and click Install.

 

 

   Once installed, the 'M' in the circle will turn blue.

 

    2. With the MQTT, Binding installed, from the PAPER UI, click on Inbox and then the Blue Circle with the '+' symbol.

         NOTE: the MQTT Thing Binding should be listed under Choose Binding.

 

   

 

    3. Click on MQTT Binding and a window will appear and any MQTT Things found will be listed.  Click on ADD Manually to add the MQTT Broker.

   

 

    4. A new window will appear listing a number of MQTT Things that can be added. Click on MQTT Broker.

   

 

    5. Once the MQTT Broker Thing is selected, a Configure MQTT Broker window will appear where the MQTT Broker definitions can be added.

        Add a Name, Thing ID, Broker Hostname(IP) .

   

 

    6. Clock on SHOW MORE to view more configuration options.  Here the Broker Port, QOS, Client ID and other options can be added.

   

 

    7.  Click the Blue circle with the check mark in it to save the configuration.  If all goes well, the MQTT Broker will show Green Online indicating openHAB has connected to it.

   

 

 

  Add a Generic MQTT Thing

 

    1.  The next step is to create a Generic MQTT Thing to communicate with.  Click on Inbox again, then the Blue Circle with the plus symbol, and  ADD Manually to get to the MQTT Binding-> Choose Thing screen.

   

 

    2.  Select Generic MQTT Thing and a Name, Thing ID, Location and select the MQTT Broker previously configured for the new thing.

   

 

    3.  Click the Blue Circle with the plus symbol to save the Thing.  To view the list of configured Things, click Things under the Configuration heading.

   

 

 

   Add Channels to the MQTT Generic Thing

 

    With the Generic MQTT Thing in place, channels can be added where topics for publishing and subscribing to can be added.  In this project a getHumid and a getTemp topic was create to send messages to retrieve the Humidity and Temp from a connected HUMI-01 sensor.  The values are then published to the associated 'temp' and 'humid' topics and viewed in the BASIC UI.

 

     1. Click on the new Thing to add a Channel.

 

    2. Click Channels to add a channel.  Select a Channel Type (Text In this instance), Channel ID, Label and a MQTT state topic to receive messages or a command topic to send messages.  A command topic mkr1010/get_temp was added here.

   

 

    3. Click SAVE and the new Channel will be added to the Generic MQTT Thing.

 

   

 

    4.   Under the Channel Label listing, GetTemp as an example, is the channel ID that will be used to to configure the Thing for the Basic UI.

           Ex: mqtt:topic:mkr1010_get_temp:mkr1010_get_temp_val

   

  Basic UI Manual config

 

    With the Things in place, to get these to display in the Basic UI, a few manual file edits are required. These configuration files are located under '/opt/openhab/conf'. Here items. sitemaps and rules were created for the associated MQTT Things.

 

   1.  First configure an item for the associated MQTT Channel ID. Here is where the channel ID is used.

        Ex:  items/sensor.items

        NOTE: A Group can be added to associate similar items in a collection

 

Group MKR1010
String Temperature "MKR1010 Temp [%s]F" (MKR1010) { channel="mqtt:topic:mkr1010_temp:temp_val" }
String MKR1010Humid "MKR1010 Humid [%s]" (MKR1010) { channel="mqtt:topic:mkr1010_temp:mkr1010_hunid" }

Switch Relay1Set "Set Relay1" <switch> (MKR1010) 
Switch Relay2Set "Set Relay2" <switch> (MKR1010) 
String MKR1010Relay1 "Set MKR1010Relay1" (MKR1010) { channel="mqtt:topic:mkr1010_relay:mkr1010_relay1" }
String GetTemp "Get  MKR1010 Temp" (MKR1010) { channel="mqtt:topic:mkr1010_get_temp:mkr1010_get_temp_val" }
String GetHumid "Get  MKR1010 Humid" (MKR1010) { channel="mqtt:topic:mkr1010_get_temp:mkr1010_get_humid_val" }

 

    2.  The items then are added to a sitemap file to format how the item will be displayed in the Basic UI.

          Ex: sitemaps/sensor.sitemap

Frame label="MKR1010" {   

Text item=Temperature label="Temp [%s]" icon="temperature" 

Text item=MKR1010Humid label="Humid [%s]" icon="humidity" 

Switch item=Relay1Set  label="Set Relay1" icon="blinds"

Switch item=Relay2Set  label="Set Relay2" icon="blinds"

}

    3.   With openHAB, rules can be added to perform specific actions against the items. In this instance, rules were added to configure the messages published to specific topics as well as read values from subscribed topics with added formatting if needed. Here, the Temp and Humidity are read via MQTT from a HUMI-01 connected to a MK1010 and MKR Relay Board, as well as control the Relays on the Relay board.

  Ex: rules/MKR1010.rules

 

var String relay1OnCmd = '{cmd1 : on, cmd2 : off}' 
var String relay1OffCmd = '{cmd1 : off, cmd2 : off}' 
var String relay2OnCmd = '{cmd1 : off, cmd2 : on}' 
var String relay2OffCmd = '{cmd1 : off, cmd2 : off}' 
var String getTempHumid = ' -m \"1\" ' 

rule "Get MKR1010 Temp"
when
    Time cron "0/30 * * * * ?"
then
    logInfo("Get MKR1010 Temp", "** Get the Temp **")
    GetTemp.sendCommand(getTempHumid)
    Thread::sleep(1000)
    GetHumid.sendCommand(getTempHumid)
end

rule "Set Relay1"
when
    Item Relay1Set received command
then
    if(receivedCommand == ON) {
        logInfo("Relay1Set", "*** Set Relay 1 On ***")
        //relay1OnCmd = "ON"
        logInfo("Relay1On", "ON")
        //sendCommand(Relay1On, relay1OnCmd) 
        MKR1010Relay1.sendCommand(relay1OnCmd)
    }
    else {
        logInfo("Relay1Set", "*** Set Relay 1 Off ***")
        //relay1OffCmd = "OFF"
        logInfo("Relay1On", "OFF")
        //sendCommand(Relay1On, relay1OffCmd) 
        MKR1010Relay1.sendCommand(relay1OffCmd)
    }
end

rule "Set Relay2"
when
    Item Relay2Set received command
then
    if(receivedCommand == ON) {
        logInfo("Relay2Set", "*** Set Relay 2 On ***")
        //relay2OnCmd = "ON"
        logInfo("Relay2On", "ON")
        //sendCommand(Relay2On, relay2OnCmd) 
        MKR1010Relay1.sendCommand(relay2OnCmd)
    }
    else {
        logInfo("Relay2Set", "*** Set Relay 2 Off ***")
        //relay2OffCmd = "OFF"
        logInfo("Relay2On", "OFF")
        //sendCommand(Relay2On, relay2OffCmd) 
        MKR1010Relay1.sendCommand(relay2OffCmd)
    }
end

 

 

   4.   This is displayed in the Basic UI as follows:

   

 

Conclusion

   This is about it for the basic set-up of MQTT with openHAB 2 and the Basic UI.  There are many other options and configuration options, but this will have to be a topic for another time.

The next step is to look at adding interfaces to the MATRIX Creator Expantion GPIO option; time permitting of course.