Problems using MQTTService for Binding-Development


I’m trying to use MQTT in openHAB 2 within a binding I’m currently developing (for a research project). The idea is that MQTT is used under the hood instead of having a user to manually specifying the MQTT channels.

However, I face two problems preventing me from testing my code:

  1. When running the binding from within Eclipse, everything compiles, openHAB starts, I can create a new Thing in the PaperUI. However, the binding can’t subscribe or publish to any topic, because there are no connections visible to it. Interestingly, there is one connection (to a mosquitto instance running locally) somehow according to the logs:
[DEBUG] [o.i.t.m.i.MqttBrokerConnection:294  ] -
Creating new client for 'tcp://localhost:1883' using id
'paho97636217526118' and file store '/tmp/mosquitto'

To my code: I’m using OSGI Declarative services getting a MqttService in the ThingHandlerFactory and pass this to newly created ThingHandlers. However, mqttService.getAllBrokerConnections() always returns an empty collection.

  1. In my attempt to package the binding and test it in a running openHAB 2 instance (on a Raspberry Pi), the build process (mvn clean install) fails to resolve the import, which is weird, because Eclipse does resolve it.

Can you point me to a solution, or documentation on how to use MQTT (programmatically) within openHAB, other than
If any information/code is missing, I’m happy to provide it.

Have you looked at the work being done on the MQTT 2 binding? I believe one of the points of it was to perform auto-discovery on MQTT topics to solve just this problem. There might be something there that can point you towards a solution.

Or even just looking at the MQTT 1.x binding code or the MQTT Publish action code.

Have a look at this binding that uses the eclipse paho mqtt in the background

Thanks for the hint. And no, not explicitly. However, after some searching, I stumbled across

There, it was stated, that one should use a configuration file name “org.eclipse.smarthome.mqtt.cfg” in the services directory. After creating this, the first problem was solved, i.e., the MQTTService had a connection, and I was able to subscribe and publish to a topic.

However, my second problem still persists.
Do you have any idea, what can be done to solve it?

Nope, no idea

I don’t do binding development so I’m afraid I have no suggestions.

I somehow circumvent the problem by using the Eclipse feature to export the binding as Deployable plug-ins and fragments, however this does not reflect the correct way of building as of
Guess, with this, my problems are solved. Thanks for you help!