MQTT Binding (v1.11) Getting Started 101

(I know that I can edit the first post :slight_smile: )

should we replace these troubleshooting instructions with the new setting?

pid: org.openhab.mqtt

I haven’t tested this yet (soon, I will) so I am asking around for now :stuck_out_tongue:

Sorry to have to ask a stupid question.

I get everything working fine up to step 7. I send the MQTT message from the client but nothing is showing up in the openHAB logs. Note, the logs is showing that the binding is able to connect to the broker. Would there be something obvious I am missing?

Could you post your mqtt.cfg and items, please

Hi vzorglub.

Never mind - worked it out. Had left a < on one of the entries in mqtt.cfg. Sorry for the stupidity

Absolutely.

ok… here we go… I did a small test on the pid parameter story… conclusion: It works as expected :slight_smile:
@Kai

start:
Clean openHAB 2.3 RC1 on rPi3 with Zulu Embedded build 1.8.0_152-b76 aarch32hf and mosquitto 1.5
installed mqtt1 binding and configured /etc/openhab2/services/mqtt.cfg as follows:

rpi3.url=tcp://127.0.0.1:1883
rpi3.clientId=oh2rpi3
rpi3.user=openhab
rpi3.pwd=some_strange_stuff

Got connected ok

2018-05-25 18:03:04.760 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2018-05-25 18:03:04.764 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'rpi3'

check console for stored configs:

openhab> config:list "(service.pid=org.openhab.mqtt)"
----------------------------------------------------------------
Pid:            org.openhab.mqtt
BundleLocation: mvn:org.openhab.io/org.openhab.io.transport.mqtt/1.12.0.RC1
Properties:
   felix.fileinstall.filename = file:/var/lib/openhab2/etc/org.openhab.mqtt.cfg
   rpi3.clientId = oh2rpi3
   rpi3.pwd = some_strange_stuff
   rpi3.url = tcp://127.0.0.1:1883
   rpi3.user = openhab
   service.pid = org.openhab.mqtt

checked the contents of /var/lib/openhab2/etc/org.openhab.mqtt.cfg

rpi3.clientId = oh2rpi3
rpi3.url = tcp://127.0.0.1:1883
rpi3.pwd = some_strange_stuff
rpi3.user = openhab

and /var/lib/openhab2/config/org/openhab/mqtt.config

rpi3.clientId="oh2rpi3"
rpi3.pwd="some_strange_stuff"
rpi3.url="tcp://127.0.0.1:1883"
rpi3.user="openhab"
service.pid="org.openhab.mqtt"

All good so far… now we go for the intentional hiccup

added a second (wrong) broker config in the /etc/openhab2/services/mqtt.cfg:

rpi3.url=tcp://127.0.0.1:1883
rpi3.clientId=oh2rpi3
rpi3.user=openhab
rpi3.pwd=some_strange_stuff
ERRORrpi3.url=tcp://127.0.0.1:1884

logged it:

==> openhab.log <==
2018-05-25 18:15:20.570 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2018-05-25 18:15:20.576 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'errorrpi3'
2018-05-25 18:15:20.621 [ERROR] [penhab.io.transport.mqtt.MqttService] - Error starting broker connection
org.eclipse.paho.client.mqttv3.MqttException: Unable to connect to server
	at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79) [223:org.openhab.io.transport.mqtt:1.12.0.RC1]
	at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650) [223:org.openhab.io.transport.mqtt:1.12.0.RC1]
	at java.lang.Thread.run(Thread.java:748) [?:?]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
	at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:?]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:?]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:?]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:?]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:?]
	at java.net.Socket.connect(Socket.java:589) ~[?:?]
	at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70) ~[?:?]
	... 2 more
2018-05-25 18:15:20.675 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'rpi3'

check again the console:

openhab> config:list "(service.pid=org.openhab.mqtt)"
----------------------------------------------------------------
Pid:            org.openhab.mqtt
BundleLocation: mvn:org.openhab.io/org.openhab.io.transport.mqtt/1.12.0.RC1
Properties:
   ERRORrpi3.url = tcp://127.0.0.1:1884
   felix.fileinstall.filename = file:/var/lib/openhab2/etc/org.openhab.mqtt.cfg
   rpi3.clientId = oh2rpi3
   rpi3.pwd = some_strange_stuff
   rpi3.url = tcp://127.0.0.1:1883
   rpi3.user = openhab
   service.pid = org.openhab.mqtt

and /var/lib/openhab2/etc/org.openhab.mqtt.cfg:

rpi3.clientId = oh2rpi3
rpi3.url = tcp://127.0.0.1:1883
rpi3.pwd = some_strange_stuff
rpi3.user = openhab
ERRORrpi3.url = tcp://127.0.0.1:1884

and /var/lib/openhab2/config/org/openhab/mqtt.config:

ERRORrpi3.url="tcp://127.0.0.1:1884"
felix.fileinstall.filename="file:/var/lib/openhab2/etc/org.openhab.mqtt.cfg"
rpi3.clientId="oh2rpi3"
rpi3.pwd="some_strange_stuff"
rpi3.url="tcp://127.0.0.1:1883"
rpi3.user="openhab"
service.pid="org.openhab.mqtt"

stop OH2.3RC1 (systemctl stop openhab2), remove the ERRORrpi3.url=tcp://127.0.0.1:1884 line from /etc/openhab2/services/mqtt.cfg, started OH2 and of course …

2018-05-25 18:21:07.051 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'errorrpi3'
2018-05-25 18:21:07.412 [ERROR] [penhab.io.transport.mqtt.MqttService] - Error starting broker connection
org.eclipse.paho.client.mqttv3.MqttException: Unable to connect to server
	at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79) [223:org.openhab.io.transport.mqtt:1.12.0.RC1]
	at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650) [223:org.openhab.io.transport.mqtt:1.12.0.RC1]
	at java.lang.Thread.run(Thread.java:748) [?:?]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
	at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:?]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:?]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:?]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:?]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:?]
	at java.net.Socket.connect(Socket.java:589) ~[?:?]
	at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70) ~[?:?]
	... 2 more
2018-05-25 18:21:07.449 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'rpi3'

and console as well as both /var/lib/openhab2/etc/org.openhab.mqtt.cfg and /var/lib/openhab2/config/org/openhab/mqtt.config have kept the stale setting.

stop OH2.3RC1, add pid:org.openhab.mqtt as the first line within /etc/openhab2/services/mqtt.cfg

and… Voila!
errors are gone and ConfigAdmin has been updated with the exclusive settings in the mqtt.cfg file !

2018-05-25 18:37:14.755 [WARN ] [g.dispatch.internal.ConfigDispatcher] - The file /etc/openhab2/services/mqtt.cfg subsequently defines the exclusive PID 'org.openhab.mqtt'. Overriding existing configuration now.

So: It works as expected and we will update the first post :stuck_out_tongue:

edit: First post has been updated. By the way, this works also with a live system (no real need to stop/start OH2… I left it there just as an extra step). Of course, this pid exclusive config story can be used with many other binding configs (not only mqtt).

3 Likes

don’t we need a… Step 0. Install the Mosquitto MQTT Broker:

sudo apt-get update
sudo apt-get install mosquitto

:slight_smile:
@rlkoshak @vzorglub

Under “A few resources first…” it already says:

An MQTT broker is needed to proceed! For testing purposes you can use an open one but it’s better to install one locally. A good option is Eclipse Mosquitto on a normal Linux system. Mosquitto is part of the optional components of openHABian

So it is already listed as a prerequisite. I don’t know if installation instructions for Mosquitto on all the different OS’s would be in scope for this tutorial. And since the rest of the tutorial is OS independent I’d be hesistant to just add the instructions for apt based Linux.

2 Likes

Thank you - got it working!

Full credit to you for taking the initiative, if I only had this when when I stated…:wink:

Just an FYI, I had a power failure at my house while on holidays and when I got back, Openhabian was up and running but none of my MQTT items were working.

After uninstalling the binding, pulling my hair out, et cetera, I did what I should have done at the beggining - restarted Mosquitto - and everything started working again.

One thing that did change was I now get this error (none of my items had changed post the power failure for this to begin occuring):

2018-07-21 11:40:04.558 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'Xiaomi_Switch_Ceiling'

The item this relates to is this:

Switch Xiaomi_Switch_Ceiling "In Line Fan Switch [%s]" { mqtt=">[broker:zigbee2mqtt/0x00158d000215fcfb/set:command:*:default], <[broker:zigbee2mqtt/0x00158d000215fcfb:state:default" }

Any thoughts as to why this error is popping up?

When Items are first initialized after OH starts they get initialized to NULL, meaning basically an unknown state. The NULL states is meaningless to devices so OH won’t try to send it.

You need to figure out why Xiaomi_Switch_Ceiling is NULL and get it set to ON or OFF. Most use persistence with restoreOnStartup to initialized items with whatever state they were is when oh went down.

Dumb question… Is this still current for openHAB 2…
I been trying to get MQTT working without success and i previously ignored this page as i was thinking the (V1.11) title was for an old version?

Want to check before i follow this guide…

No go ahead a follow this.
It works. But don’t miss a step…

1 Like

Thanks @ThomDietrich, Great Tutorial! Just my $.02: one thing I would suggest is, in Step 6 is to give an example as to how to publish a simple message from a client. For example, for those using mosquitto on OpenHABian, it would be:

[08:05:51] openhabian@openHABianPi:~$ mosquitto_pub -t "testing/mqtt/topic" -m "ON"
[08:05:53] openhabian@openHABianPi:~$ mosquitto_pub -t "testing/mqtt/topic" -m "OFF"

some people may not be familiar with the syntax in the command line for publishing a message, and the above commands are not listed in any of the linked documentation. This would at least get them started with a basic usage command and test the install. They can then go and search to learn how to write more in-depth / complex messages, configure Last Will topic/message, wildcards, etc.

~John

Anyone here that is in a position to clarify whether the “Embedded MQTT Broker” (available in Paper UI under Add-ons / Misc) is now “production ready” to replace a separate Mosquitto installation?

BTW; I am setting up a clean system based on version 2.3.0 stable.

Hi
I was running openHAB 2.1 on openhabian. My MQTT server (Synology box) decide to stop working, so I decided to upgrade openhabian to the openHAB 2.3 version and in addition, install a few optional apps like Mosquitto.
I performed the upgrade using the openhabian tool in the following order 01,02,03 then selected 20 and added items such as Log viewer, InfluxDB…, NodeRE and Mosquitto.
The only upgrade/install that encountered an error was Mosquitto.

The following packages have unmet dependencies:
_ mosquitto : Depends: libwebsockets3 (>= 1.2) but it is not installable_
E: Unable to correct problems, you have held broken packages.

How do I overcome the libwebsockets3 problem?

Thanks,
Neil.

I wasn’t aware that Synology supported openHABian. This error seems to point in that direction. I don’t have any advice to offer beyond a Google search for how to install Mosquitto on Synology and doing it by hand.

Hi Rich,
Sorry, I wasn’t clear enough. My Synology box was only the MQTT server. It played no other part in openHAB.

openHAB is installed on a RPi 3 ModelB+ with the standard openhabian Pi image. I have been running it this way for a few years now.
Now that MQTT on the Synology has died, I decided to implement the openhabian option of Mosquitto on my OH Pi and this is where the error now appears on my OH Pi install after running the Mosquitto menu option.
Neil.

Ok, that is different and very wired.

I imagine you get the same error if you run sudo apt-get install mosquitto?