Menu
Cart

Link device to openHAB over MQTT

At this point you should have a Mosquitto MQTT server connected up to an OpenHAB server. If not read through the previous tutorial which can be found here: http://www.engimusing.com/pages/setting-up-mqtt-server-and-connecting-to-openhab

Configure OpenHAB:

 First we need to create some new openHAB items that are setup to talk to MQTT. 

Lets start by creating a new file in the configurations/items directory called "mqttLeds.items". Easiest way is to copy the blink.items file and rename it mqttLeds.items.

Any file with the .items extention that is in the configurations directory will be loaded by OpenHAB and available for the sitemap to include in the openHAB website.

Open the new mqttLeds.items in a text editor.

Delete anything in the file if you copied it from blink.items.

Copy and paste the following into the file:

Group LEDS_MQTT (Tutorial)
Switch LED0_MQTT "LED - Red" (LEDS_MQTT) {mqtt=">[localbroker:EFM/BOARD/LED0/CTL:command:*:${command}], <[localbroker:EFM/BOARD/LED0?/LED:state:default]"}
Switch LED1_MQTT "LED - Blue" (LEDS_MQTT) {mqtt=">[localbroker:EFM/BOARD/LED1/CTL:command:*:${command}], <[localbroker:EFM/BOARD/LED1?/LED:state:default]"}
Switch LED2_MQTT "LED - Green" (LEDS_MQTT) {mqtt=">[localbroker:EFM/BOARD/LED2/CTL:command:*:${command}], <[localbroker:EFM/BOARD/LED2?/LED:state:default]"} 

 The code above creates 3 switches which are connected to the localbrocker mqtt server which we configured in a previous tutorial in the openHAB.cfg file. The first half of the mqtt command is connecting the switch up to some logic that sends the topic "EFM/BOARD/LED0/CTL" with the payload ON/OFF depending on the new state of the switch. The second half is connecting the topic EFM/BOARD/LED0?/LED to the state of the switch. So after the command has been recieved by the board it will send the current status and this is fed back to the openHAB server.

Next we need to add this group to the sitemap. Open "configuration/sitemaps/engimusing.sitemap".

Lets add a new frame section at the top for this new group. After the first { add the following:

Frame label="OpenHAB MQTT Tutorial" {
Group item=LEDS_MQTT label="MQTT Controlled Leds" icon="light-on"
}
 

 Now if you look at your openHAB engimusing page you should see a new section called "OpenHAB MQTT Tutorial". If you open that section and click on any of the switches you should see messages appear related to the switch you pressed in both the Mosquitto and the OpenHAB consoles.

Setup Device for MQTT

The last step is to setup the engimusing board to listen for MQTT messages and send MQTT responses. 

Open the Arduino IDE which has the Engimusing library setup already (if you haven't done the Ardunio IDE setup tutorial it can be found here: http://www.engimusing.com/pages/arduino-ide-adding-engimusing-boards).

Open the MqttHub\LED_Tutorial example.

This sketch is setup to connect to the openHAB configurations we setup in the first half of this tutorial. 

For example look at the LEDCtrol0 begin call. See how the 3rd parameter is set to "EFM/BOARD/LED0". This matches the LED0_MQTT definition above which will allow the commands from the openHAB server to control that light on the Engimusing board.

Change the device to match the Engimusing device you would like to program. 

Upload the sketch to the device.

If you open the serial monitor in the Arduino IDE you should now see the following happen every 30 seconds or so:

{"TOP":"EFM/BOARD/LED2/#","PLD":"SUB"}
{"TOP":"EFM/BOARD/LED1/#","PLD":"SUB"}
{"TOP":"EFM/BOARD/LED0/#","PLD":"SUB"}

Those are the MQTT subscription messages so the device can let the MQTT server know that it is interested in those topics.

Test the board by sending it a command to turn on the Red LED. To do this, type the following into the serial monitor and hit send:

{"TOP":"EFM/BOARD/LED0/CTL","PLD":"ON"}

The Red LED should have turned on.

If you send:

{"TOP":"EFM/BOARD/LED0/CTL","PLD":"OFF"}

Then the Red LED should turn off.

The last step to connect the device to the MQTT server is to start the EFM_Serial2Mqtt application which connects to the Serial port and the MQTT server and translates messages between the two.

LINK TO EFM_Serial2Mqtt HERE!

Open a terminal and change the current working directory to the contents of the zip file from the link. 

(Windows)

Open EFM_Serial2Mqtt.bat in a text editor and change the COM3 to the Serial port that is connected to the Engimusing device. (In the Arduino IDE look in the bottom left corner to see which COM port is being used.) Save EFM_Serial2Mqtt.bat.

Run EFM_Serial2Mqtt.bat.

(other os directions here)

wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo pip install paho-mqtt

 

In the Mosquitto console you should see something like this:

1482264972: New connection from ::1 on port 1883.
1482264972: New client connected from ::1 as subscriber (c1, k60).
1482264972: Sending CONNACK to subscriber (0, 0)
1482264972: New connection from ::1 on port 1883.
1482264972: New client connected from ::1 as publisher (c1, k60).
1482264972: Sending CONNACK to publisher (0, 0)
1482264972: Received SUBSCRIBE from publisher
1482264972: home/habtutor/# (QoS 0)
1482264972: publisher 0 home/habtutor/#
1482264972: Sending SUBACK to publisher

This means that the EFM_Serial2Mqtt script correctly connected to the MQTT server.

If the Engimusing device is still connected then you should see the following messages every 30 seconds or so on the Mosquitto console:

1482265025: Received SUBSCRIBE from subscriber
1482265025: EFM/BOARD/LED2/# (QoS 0)
1482265025: subscriber 0 EFM/BOARD/LED2/#
1482265025: Sending SUBACK to subscriber
1482265025: Received SUBSCRIBE from subscriber
1482265025: EFM/BOARD/LED1/# (QoS 0)
1482265025: subscriber 0 EFM/BOARD/LED1/#
1482265025: Sending SUBACK to subscriber
1482265025: Received SUBSCRIBE from subscriber
1482265025: EFM/BOARD/LED0/# (QoS 0)
1482265025: subscriber 0 EFM/BOARD/LED0/#
1482265025: Sending SUBACK to subscriber
1482265032: Received PINGREQ from publisher
1482265032: Sending PINGRESP to publisher
1482265055: Received SUBSCRIBE from subscriber
1482265055: EFM/BOARD/LED2/# (QoS 0)
1482265055: subscriber 0 EFM/BOARD/LED2/#
1482265055: Sending SUBACK to subscriber
1482265055: Received SUBSCRIBE from subscriber
1482265055: EFM/BOARD/LED1/# (QoS 0)
1482265055: subscriber 0 EFM/BOARD/LED1/#
1482265055: Sending SUBACK to subscriber
1482265055: Received SUBSCRIBE from subscriber
1482265055: EFM/BOARD/LED0/# (QoS 0)
1482265055: subscriber 0 EFM/BOARD/LED0/#

If so then everything is connected up correctly.

Now if you toggle the switch in OpenHAB you should see the light change on the Engimusing Device.