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:
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.
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 org.eclipse.smarthome.io.transport.mqtt.MqttBrokersObserver, which is weird, because Eclipse does resolve 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.
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?