Use MQTT Binding to talk to Raspberry Pi

Tags: #<Tag:0x00007fc1fc118c60> #<Tag:0x00007fc1fc118b98> #<Tag:0x00007fc1fc118ad0>

Hey OpenHAB Community,

I try to get the MQTT Bindung to talk to some other Raspberry Pis. Previously I used the MQTT Action Binding with some rules to send specific text values to the berries.

With the new released openHAB 2.5 I want to implement the MQTT Binding using switches and channels. Even If I tried several guides and the official description of the binding, I cant receive any messages on other berries.
I am using the embedded broker, created a MQTT generic thing and connected to a Switch Channel (“MQTT command topic” is “test”). Changing the state of the switch to ON or OFF shall send “ON” or “OFF”.
No error messages are at the log files and the broker and generic thing are ONLINE / INITIALIZED.

With the other PI I subscribe to the topic “test” (like I did with the MQTT Action binding) :

mosquitto_sub –t test

Switching the MQTT-Switch doesn’t give me any response at the subscribed topic.

I really don’t know my mistake – Could be the subscription to the topic “test” by the mosquitto client the wrong approach?

Thanks
Phil

I think your subscription is wrong :wink: Try it with this command:

mosquitto_sub –t test/#

A good little Guide for the Client…

http://www.steves-internet-guide.com/mosquitto_pub-sub-clients/

Simplify and make changes gradually. Presumably if you had this working with the MQTT v1 binding working using the MQTT Action, you have Mosquitto or some other external broker running. Keep using that. Honestly, the embedded broker is no longer maintained and probably shouldn’t be used anyway, but you definitely don’t want to move from a solid broker like Mosquitto to that.

Keep your Rules the same. Once you have your Broker Thing connected and Online, you can publish messages using

actions.getAction("mqtt", "broker:thing:id").publishMQTT("topic", "message", false) // last argument is the retained flag

Once that works, then move to creating the Generic MQTT Things and Channels to replace your Rules.

Minimize your changes to make it easier to find where it went wrong.

@ei_Gelb_Geek
Changing the subscripted topic like you mentioned didn’t help.

I adjusted settings are now:

broker.things

Bridge mqtt:broker:testbrokername [ host="192.168.222.10", secure=false ] {
Thing topic testtopic {Channels:Type switch : testswitch "label" [
stateTopic="topicstate", commandTopic="topiccommand", on="ON", off="OFF"]}}

demo.items
Switch testswitch_sitemap "Dummy" channel= "mqtt:topic:testbrokername:testtopic:testswitch" }

sitemap
Switch item=testswitch_sitemap

subscription of the other pi
I tried:

  • mosquitto_sub -t topicstate
  • mosquitto_sub -t topicstate/
  • mosquitto_sub -t topicstate/#
  • and the same with topiccomand

@rlkoshak
I also tried your code with the embedded broker - I wasn’t able to transfer any messages. How to use mosquitto for that?

The problem might be roundabout 40cm in front of the screen :smiley:

It doesn’t matter what the broker is. If the configuration is correct it will work. You configure the connection to the Broker in the MQTT Broker Thing.

So do you see errors in the logs? Get MQTT Explorer and monitor all of the messages on your broker instead of just mosquitto-sub.

I think this Config is not correct…

Try it like this…

Bridge mqtt:broker:testbrokername [ host="192.168.222.10",port=1883,secure=false,retainMessages=false, retain=false ]

Thing mqtt:topic:testtopic "MQTT - Testtopic" (mqtt:broker:testbrokername) {
  Channels:
    Type switch : testswitch "Test Switch" [ stateTopic="/topicstate/state", commandTopic="/topiccommand/command", on="ON", off="OFF"]
}

And Subscribe the Topic with this Command:

mosquitto_sub –t /topicstate/state
mosquitto_sub –t /topiccommand/command

@ei_Gelb_Geek

Whats the difference between:

  1. with curly bracket
    Bridge mqtt: .... { Thing mqtt: ... }

  2. without curly bracket
    Bridge mqtt: .... Thing mqtt: ...

Nevertheless, using your code doens’t work at my side.
Where is the connection to the embedded mqtt broker? Shall the bridge make this connection and how do I see this in the code?

Right now at the PaperUI I have three Brokers

@rlkoshak
When I connect to my openhabserver 192.168.222.10:1883 with the following settings I don’t get any message (no error logs, no $SYS or something else)
grafik

The MQTT-Client ID is jsut a name not the connected MQTT Server ID, isn’t it?

Is this Item really connected to your testswitch Thing?

NO … 2 Brokers and 1 Thing

@ei_Gelb_Geek

seems that the item testswitch_sitemap is connected to the geneic mqtt thing using the channel testswitch

:smiley: it works.

My way:

  • At PaperUI I created a new MQTT Broker (not the System MQTT Broker)
  • Broker Hostname/IP: localhost
  • and gave them a username and a password (I didn’t do that the last times) --> could this be a problem?

Afterwards the same procedure as last time:

  • Generic MQTT Thing with a bridge
  • Channel with a linked item

Thank you guys for your help and patience.

If your Broker is configured to require a username and password than yes, of course, you must supply the username and password to connect to it.

Where can I see If an username or pw is required or not? I didn’t choose a secure connection or something similar

This is based on how mosquitto was set up in the first place. If you never set up a username and password, how did you know what username and password to use to get it to work?

I gave my own created broker an user with a password

user/password values are not required so left them empty at the first tries.