MQTT 2.5.1, with switch

Hi!

I am experimenting with my new ESP8266 to start applying it to my projects.

I started simply by connecting a led but I had a problem related to the status.

When I change the switch from on to off, the LED turns on but the switch goes back to off, so I have to change the switch back to on, this does not affect the light of the LED, and for the off of the LED the same thing happens.

I’ve been reading that it could be that the status of the item is not updated or something related to that.

I leave you information about my code.

.things

Bridge mqtt:broker:mosquitto "Mosquitto" @ "System" [ host="x.x.x.x", secure=false ]
{	
    Thing topic prueba "S" {
        Channels:
            Type switch : verde1 [ commandTopic="esp8266/4", on="1", off="0"]
    }
}

.items

Switch Verde {channel="mqtt:topic:mosquitto:prueba:verde1"}

.sitemap

sitemap demo label="Menú"
{
    Frame label="Clima" {
		Switch		item=Verde
}}

And in case you need part of the Arduino code that allows me to publish the status

..........  
if(topic=="esp8266/4"){
      Serial.print("Changing GPIO 4 to ");
      if(messageTemp == "1"){
        digitalWrite(ledGPIO4, HIGH);
        Serial.print("On");
      }
      else if(messageTemp == "0"){
        digitalWrite(ledGPIO4, LOW);
        Serial.print("Off");
      }
  }
..........
..........
..........
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");  
      client.subscribe("esp8266/4");

Any help at all is welcome.

Thanks!

I believe the issue could be with not having a state topic aka response from arduino.

If I am reading correct you have a command sent from openHAB but no response coming back.

The thing for your device can have a state topic which is the response. Once you have this in your thing you need to add to arduino code.

Personally, I think it’s a defect in the MQTT binding. If no state update methods are defined in the channel, it should not be altering the linked Item. Write-only channels are allowed in openHAB in general.

(I think the binding actually interferes with autoupdate’s prediction rather than updating directly, but same principle.)

1 Like

@rossko57 this is a true statement that I did not think about. You can most certainly have a write only channel!

The OP probably does want to have his device publishing status and add stateTopic to his channel.
This was just a comment about the way it failed without.