How to on off realy in ardiuno via openhab 2 useing mqtt


i am new in openhub, i am just install openhub in pi3, any one help me how to on off relay in ardiuno via openhub2 using mqtt server stap by stap

thnks you

Start reading the configuration pages at the official docs:

Then proceed to the MQTT binding page:

If you did perform all steps written there and it does not work, post your mqtt,cfg file and your items and sitemaps setup.

thanks to replay sir,

i am try to do as per your suggestion,now i am able to on off relay publish topic via my phone using “MQTT Deskbrod App” and raspberry pi terminal but i cant on off relay using openhab here my arduino ,mqtt,items and sitemaps file in pdf format ARDIUON mqtt.pdf (23.6 KB)
mqtt cfg.pdf (11.0 KB)
default sitemap.pdf (7.0 KB)
switch items.pdf (6.2 KB)

I would take a mqtt client (either the command line tool mosquitto_sub -v -t /# or for example mqtt.spy) and subscribe to the # topic to see all messages and verify the correct topic your are subscribing to)

The only suspicions thing I can spot is: the topics have a trailing slash, i would try without it:

char const* switchTopic1 = "/house/switch1";

instead of

char const* switchTopic1 = "/house/switch1/";

Also note that you are missing three of these trailing slashes in your item definition:

Switch Switch1

Only the outgoing ON command has this slash …

1 Like

good morning sir,

i am try to put slashes in item file but it can’t work, i can try on off relay via terminal and is work perfect and working fine with my mobile mqtt app and mqtt fx in my pc only via openhab i cant on off relay , i don’t know what is the mistake in openhab here pic of raspberry terminal and openhab log, in openhab switch status it change at click time but relay not on or off

You still have the trailing slash in your item config, remove it.

i am try remove slash but is not working , via openhab

Switch Switch1 {mqtt=">[mqtt:broker:/house/switch1:command:ON:1],>[mqtt:broker:/house/switch1:command:OFF:0],

Looks good now.

Any errors in your openhab.log or broker log? Any state change events in your events.log?

Go through all the steps described here:

sir sorry to disturb you again now i have this problem :“MQTT connection to ‘’ was lost: Connection lost : ReasonCode 32109” : Cause : null" and ever time if i reboot raspberry "‘binding-mqtt1’ service get uninstall

Go to your addons.cfg a and choose a package, for example:

package = expert

1 Like

Though I am not claiming to have the solution here, I just want to point out that there is a simpler way to MQTT your relay.
You are sending the digitalWrite state (“1” or “0”) in your MQTT message. Though that is perfectly possible, I tend to just send the default values for the Switch and in my Arduino I then just check for “ON” or “OFF”.
advantage is that you get by with only 2 MQTT statements (for command and state) rather than having to implement 2 for command (ON and OFF) and for state (ON and OFF).

Nevertheless, your method is quite OK and should work, provided the parsing in the Arduinofile is correct