Sent string commands via MQTT

I want to sent string commands from openhab (installed on my raspberry pi) to an arduino gateway and from the gateway to the nodes of my smart house with NRF24(transceivers).
How can this be possible?

One random thought, you could use the shell binding and mosquitto_pub (comes with the mosquitto MQTT broker) to send the strings.

You can easily send string commands using the MQTT item binding. You would have a single MQTT broker installed somewhere (like the mosquitto broker on your Raspberry Pi). The MQTT binding in openHAB would publish to a defined topic name, like “/your/arduino/command”, and your Arduino would subscribe to the same topic. Your openHAB item would look something like:

String ArduinoGateway "Arduino" { mqtt=">[broker:/your/arduino/command:command:*:default]" }

If the command strings were predefined and you wanted to send them via UI clicks, you could use a Switch widget in your sitemap:

Switch item=ArduinoGateway mappings=[STOP=Stop,GO=Go,HOVER=Hover]

Or you could send commands from rules, like:

rule Hover
when
  Item SomeItem changed
then
  ArduinoGateway.sendCommand("HOVER")
end

Read up, if you haven’t already, on the MQTT binding and various example configurations. Good luck!

I have installed mosquito on my pi.

My item file is : String LED “switch the led” {mqtt=">[mymosquitto:/Topic:command:*:default]"}

Sitemaps : Selection item=LED mappings=[STOP=Stop, GO=Go]

and Rules :
Rule test
when
Item LED received update
then
LED.sendCommand(“HI”)
end

I have also installed MQTT.fx and I have subscribed the /Topic channel but I can’t see the messages. According to my configuration when I choose STOP or GO from the UI ,openhab should sent a string “HI” on the channel /Topic .
What I’m doing wrong?

In case you’re doing something different, all components that communicate via MQTT need to be configured to use the same MQTT broker. Configuring multiple brokers might be part of a working configuration, but in the simplest case, you must use only a single broker (mosquitto is common).

I guess I’m not sure what you are trying to accomplish. I usually scratch something out on a piece of paper, with directed lines marked “subscribe” and lines marked “publish” going from the components back to the single broker. In the rule above, I just don’t understand why you are triggering on the state update for the LED item, and upon seeing the state updated, sending the message “HI” to the “/Topic” topic.

I want to press a button on UI and sent on an arduino a string like “HI” . So when the arduino receives this string to do an action as light a LED.

  • Configure the Arduino and openHAB to use the same MQTT broker.
  • If your broker is named mosquitto and the topic is named /Topic, have the Arduino subscribe to messages from that broker on that topic.
  • Define an item in openHAB like this:
String LED "LED" { mqtt=">[mosquitto:/Topic:command:*:default]" }
  • Define the sitemap to reference that item and have a button for “HI”:
Switch item=LED mappings=[HI=HI]
  • Press the HI button in the openHAB UI. It will send the message “HI” to the /Topic topic.
  • You can use the tools mosquitto_sub and mosquitto_pub to test connectivity.

I wrote exactly the same but the button HI didn’t appear, there is only a switch button.

Can you please give me the openhab.cfg configurations for MQTT ?

Depending on your mosquitto setup (tcp or ssl) you only need one line for tcp connection:

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
mqtt:mymosquitto.url=tcp://localhost:1883

One more hint: in MQTT.fx subscribe to topic “#”, then you will see on which topic you receive or send your mqtt messages.

If I make a switch item I can sent ON,OFF commands via MQTT. I try to do a String item and set mappings in the sitemap as you written above but again at the UI I can only see a slider and not the mappings. Any advice please?

Hi Nikos,

Is the explanation of sitemaps missing some critical information? I have a Switch in my sitemap like you can see here. Are you seeing something different? Do you use the openHAB Designer to help guard against typos that might cause the sitemap to fail to load properly?

Hope this helps.