How to Set-up MQTTGateway with OpenHAB
What is MQTTGateway:
MQTTGateway will act as a broker, a server. And any MQTT clients should be able to connect to it (Please let me know if you experience problems).
MQTTGateway is a very simple implementation. It will not care if the client subscribes to anything or not.
Every node package it receives it will publish to the client. Sketch name and version is also implemented.
Most clients handle this well and just ignores messages it hasn't been subscribed yet.
There are some settings in MQTT that can be good to take a look at.
You will need to configure TCP_IP and TCP_PORT.
MQTTGateway will also give nodes an ID between MQTT_FIRST_SENSORID and MQTT_LAST_SENSORID. The last position is stored in EEprom.
As standard MQTT_SEND_SUBSCRIPTION is enabled. This basically means that the broker will send a empty payload to the node upon a subscribe package. This can be useful. Just keep in mind that you can receive empty payload packages.
The MQTTGateway will also present its SketchName and SketchVersion.
Lets get started!
First follow https://github.com/openhab/openhab/wiki/Quick-Setup-an-openHAB-Server You can setup and try with DEMO configuration first.
Then we need to unpack org.openhab.binding.mqtt into addons folder * NOTE (During development of MQTTGateway I found a bug inside openhab mqtt client which is solved in 1.6.0 nightly build. Use this nightly addon if you experience problems)
ClientID does not matter at all (just keep it short). And IP and Port is self explanatory.
Now you can start OpenHAB, it will connect but nothing more will happen.
This is because we hasn't subscribed to anything and OpenHAB will just ignore every incoming package.
So here comes a sample configuration of OpenHAB.
-
items/test.items
Group all
Group node1 (all)
Group node2 (all)
Group sketch (all)
Number node1_temp "Temp [%.1f °C]" (node1,all) {mqtt="<[mysensor:MyMQTT/20/10/V_TEMP:state:default]"}
Number node1_humid "Humid [%.1f %%Rh]" (node1,all) {mqtt="<[mysensor:MyMQTT/20/11/V_HUM:state:default]"}
Switch node2_sw1 "sw2" (node2,all) {mqtt=">[mysensor:MyMQTT/21/1/V_LIGHT:command:ON:1],>[mysensor:MyMQTT/21/1/V_LIGHT:command:OFF:0]"}
Switch node2_sw2 "sw2 send + recieve example" (node2,all) {mqtt=">[mysensor:MyMQTT/21/2/V_LIGHT:command:ON:1],>[mysensor:MyMQTT/21/2/V_LIGHT:command:OFF:0],<[mysensor:MyMQTT/21/2/V_LIGHT:command:MAP(1on0off.map)]"}
String sketch20 "Sketch name 20 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/20/255/V_SKETCH_NAME:state:default]"}
String sketch21 "Sketch name 21 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/21/255/V_SKETCH_NAME:state:default]"}
String sketch22 "Sketch name 22 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/22/255/V_SKETCH_NAME:state:default]"}
String sketch23 "Sketch name 23 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/23/255/V_SKETCH_NAME:state:default]"}
String sketch24 "Sketch name 24 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/24/255/V_SKETCH_NAME:state:default]"}
String sketch25 "Sketch name 25 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/25/255/V_SKETCH_NAME:state:default]"}
String sketch26 "Sketch name 26 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/26/255/V_SKETCH_NAME:state:default]"}
String sketch27 "Sketch name 27 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/27/255/V_SKETCH_NAME:state:default]"}
String sketch28 "Sketch name 28 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/28/255/V_SKETCH_NAME:state:default]"}
String sketch29 "Sketch name 29 [%s]" (sketch,all) {mqtt="<[mysensor:MyMQTT/29/255/V_SKETCH_NAME:state:default]"}
We also need to create a sitemap :
and a simple transformation file :
-
transform/1on0off.map
1=ON
0=OFF
If everything worked and I didn't forget anything just fire up :
http://[Openhab]:8080/openhab.app?sitemap=test
After you restart one of your node's the data and sketch name should be visible.
Openhab will forget everything after every restart. Use persistence to fix this (https://github.com/openhab/openhab/wiki/rrd4j-Persistence)