[SOLVED] OpenHab, Arduino, Mqtt

  • Platform information:
  • Hardware: Raspberry PI3+
  • OS: openHABian
  • Java Runtime Environment: javac 1.8.0_222
  • openHAB version: 2.4.0-1
  • Issue of the topic:

I want to interface openhab and Arduino, equipped with an 8-relay board and a temperature sensor, using the MQTT protocol. I installed MQTT Binding and enbedded MQTT Broker, on Openhab.
fig1
fig2
Arduino subscribes to the following topics:
-for sensors:

> controller_garage/sensor/humidity
> controller_garage/sensor/temperature

-for commands (payload “on” and “off”)

> “controller_garage/switch1”
> “controller_garage/switch2”
> “controller_garage/switch3”
> “controller_garage/switch4”
> “controller_garage/switch5”
> “controller_garage/switch6”
> “controller_garage/switch7”
> “controller_garage/switch8”

-for states (payload “on” and “off”)

> “controller_garage/switchconfirm1”
> “controller_garage/switchconfirm2”
> “controller_garage/switchconfirm3”
> “controller_garage/switchconfirm4”
> “controller_garage/switchconfirm5”
> “controller_garage/switchconfirm6”
> “controller_garage/switchconfirm7”
> “controller_garage/switchconfirm8”

The broker works well. Taking of evidence by “mqtt.fx”, relays respond to commands and update status.
I manually configured openhab and I can not update the relay states.
The temperature and humidity sensors work perfectly with OpenHab but the relay, no.
This is my configuration:

.things

//MQTT Binding
Bridge mqtt:systemBroker:embedded-mqtt-broker "MQTT Broker" @ "Garage" [ host="localhost", secure=false ] {
    Thing topic mqtt_things "MQTT Things" @ "Garage" {
         Channels:
             Type number : temperature "Air Temperature"    [ stateTopic="controller_garage/sensor/temperature"]
             Type number : humidity "Humidity"              [stateTopic="controller_garage/sensor/humidity"]
             Type switch : switch1 "switch1"                [stateTopic="controller_garage/switchconfirm1", commandTopic="controller_garage/switch1", on="on", off="off"]
     }
}

.items

//MQTT
Number Temperature  "Garage Temperature[%.2f °C]"   <temperature>   ["TargetTemperature"]   { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:temperature" }
Number Humidity     "Garage Humidity[%.2f %%]"      <humidity>                              { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:humidity" }
Switch mqttsw1      "mqttsw1"                       <light>          [ "Lighting" ]         {channel = "mqtt:topic:embedded-mqtt-broker:switch1:command", channel = "mqtt:topic:embedded-mqtt-broker:switch1:state" }

.sitemap

	 Frame label="test" {
	 	Text item=Temperature
	 	Text item=Humidity
		Switch item=mqttsw1

	 }

fig4

When active mqttsw1 (from Basic UI) I have the following message in the logs without any effect on relay1.
fig5
Can you help me?
Thank you

These are not things:
.things

//MQTT
Number Temperature  "Garage Temperature[%.2f °C]"   <temperature>   ["TargetTemperature"]   { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:temperature" }
Number Humidity     "Garage Humidity[%.2f %%]"      <humidity>                              { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:humidity" }
Switch mqttsw1      "mqttsw1"                       <light>          [ "Lighting" ]   

These are items definitions.
Use the paper UI to create the Generic MQTT things and add channels as required:
Configuration -> Things -> Add -> MQTT binding -> Add Manually -> Generic MQTT thing
Name it - Change the defualt name to something human
Add channels

Link the channels to your items in an items file

sorry I made a mistake (I corrected the post)
This is the correct definition of things

.things

//MQTT Binding
Bridge mqtt:systemBroker:embedded-mqtt-broker "MQTT Broker" @ "Garage" [ host="localhost", secure=false ] {
    Thing topic mqtt_things "MQTT Things" @ "Garage" {
         Channels:
             Type number : temperature "Air Temperature"    [ stateTopic="controller_garage/sensor/temperature"]
             Type number : humidity "Humidity"              [stateTopic="controller_garage/sensor/humidity"]
             Type switch : switch1 "switch1"                [stateTopic="controller_garage/switchconfirm1", commandTopic="controller_garage/switch1", on="on", off="off"]
     }
}

Temperature and humidity sensors work well.
I do not understand how to send the payload to MQTT channel. :pensive:

Also I would like to know if can I send a custom payload?
Thank you

Switch mqttsw1      "mqttsw1"                       <light>          [ "Lighting" ]         {channel = "mqtt:topic:embedded-mqtt-broker:switch1" }

Just like that

1 Like

Thak you so much, Vincent!!! :+1:
I understood. My code is ok.

------------------------------------> “.things” <--------------------------------

//MQTT Binding
Bridge mqtt:systemBroker:embedded-mqtt-broker "MQTT Broker" @ "Garage" [ host="localhost", secure=false ] {
    Thing topic mqtt_things "MQTT Things" @ "Garage" {
         Channels:
             Type number : temperature "Air Temperature"    [ stateTopic="controller_garage/sensor/temperature"]
             Type number : humidity "Humidity"              [stateTopic="controller_garage/sensor/humidity"]
             Type switch : switch1 "switch1"                [stateTopic="controller_garage/switchconfirm1", commandTopic="controller_garage/switch1", on="on", off="off"]
             Type switch : switch2 "switch2"                [stateTopic="controller_garage/switchconfirm2", commandTopic="controller_garage/switch2", on="on", off="off"]
             Type switch : switch3 "switch3"                [stateTopic="controller_garage/switchconfirm3", commandTopic="controller_garage/switch3", on="on", off="off"]
             Type switch : switch4 "switch4"                [stateTopic="controller_garage/switchconfirm4", commandTopic="controller_garage/switch4", on="on", off="off"]
             Type switch : switch5 "switch5"                [stateTopic="controller_garage/switchconfirm5", commandTopic="controller_garage/switch5", on="on", off="off"]
             Type switch : switch6 "switch6"                [stateTopic="controller_garage/switchconfirm6", commandTopic="controller_garage/switch6", on="on", off="off"]
             Type switch : switch7 "switch7"                [stateTopic="controller_garage/switchconfirm7", commandTopic="controller_garage/switch7", on="on", off="off"]
             Type switch : switch8 "switch8"                [stateTopic="controller_garage/switchconfirm8", commandTopic="controller_garage/switch8", on="on", off="off"]
     }
}

------------------------------------> “.items” <--------------------------------

//MQTT
Number Temperature  "Garage Temperature[%.2f °C]"   <temperature>   ["TargetTemperature"]   { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:temperature" }
Number Humidity     "Garage Humidity[%.2f %%]"      <humidity>                              { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:humidity" }
Switch mqttsw1      "mqtt sw1"                       <light>          [ "Lighting" ]         { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:switch1" }
Switch mqttsw2      "mqtt sw2"                       <light>          [ "Lighting" ]         { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:switch2" }
Switch mqttsw3      "mqtt sw3"                       <light>          [ "Lighting" ]         { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:switch3" }
Switch mqttsw4      "mqtt sw4"                       <light>          [ "Lighting" ]         { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:switch4" }
Switch mqttsw5      "mqtt sw5"                       <light>          [ "Lighting" ]         { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:switch5" }
Switch mqttsw6      "mqtt sw6"                       <light>          [ "Lighting" ]         { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:switch6" }
Switch mqttsw7      "mqtt sw7"                       <light>          [ "Lighting" ]         { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:switch7" }
Switch mqttsw8      "mqtt sw8"                       <light>          [ "Lighting" ]         { channel = "mqtt:topic:embedded-mqtt-broker:mqtt_things:switch8" }

------------------------------------> “.sitemap” <--------------------------------

	 Frame label="test" {
		Default item=Temperature label="Garage Temperature"
		Default item=Humidity label="Garage Humidity"
		Default item=mqttsw1 label="mqtt sw1"
		Default item=mqttsw2 label="mqtt sw2"
		Default item=mqttsw3 label="mqtt sw3"
		Default item=mqttsw4 label="mqtt sw4"
		Default item=mqttsw5 label="mqtt sw5"
		Default item=mqttsw6 label="mqtt sw6"
		Default item=mqttsw7 label="mqtt sw7"
		Default item=mqttsw8 label="mqtt sw8"
	 }

The payload is:

and