MQTT subscriber

Hello All,

I’ve struggled my way through to the point I’m at now. I keep spending days on what seem to be simple issues but are not widely documented.

My latest issue is that I am able to publish messages to my mosquito broker both with openHAB and with my ESP8266 device. Verified this with a subscriber listener that is able to show receipt of messages from both clients as I’m subscribed and publishing under the same topic hierarchy. The problem is that openHAB and my ESP8266 as subscribers aren’t receiving messages from the other respectively.

Some new details and my configuration below:

In my openhab.cfg file I have:
mqtt:ESP8266.url=tcp://192.168.0.17:1883
mqtt:ESP8266.clientId=openhab

In my Items file I have:

Group All
Switch mqttsw1 “Switch 1” (all) {mqtt=">[ESP8266:/F1/R00/D01/I06:command:on:1],>[ESP8266:/F1/R00/D01/I06:command:off:0]"}
Switch mqttsw2 “Switch 2” (all) {mqtt=">[ESP8266:/F1/R00/D01/I02:command:off:0],>[ESP8266:/F1/R00/D01/I02:command:on:1]"}
Contact mqttcon1 “Front Door” (All) {mqtt="<[ESP8266:/F1/R00/D01/I01:command:open:1],<[ESP8266:/F1/R00/D01/I01:command:closed:0]"}

And my Sitemap:

sitemap dolphin label=“Main Menu”
{
Frame label=“MQTT” {
Switch item=mqttsw1 label=“MQTT Switch 1”
Switch item=mqttsw2 label=“MQTT Switch 2”
Switch item=mqttcon1
}
}

I have validated that hitting the switch for Switch 1 and Switch two generate a message received when subscribing:

mosquitto_sub -d -t /F1/R00/D01/#

Client mosqsub/2716-raspberryp sending CONNECT
Client mosqsub/2716-raspberryp received CONNACK
Client mosqsub/2716-raspberryp sending SUBSCRIBE (Mid: 1, Topic: /F1/R00/D01/#, QoS: 0)
Client mosqsub/2716-raspberryp received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/2716-raspberryp received PUBLISH (d0, q0, r0, m0, ‘/F1/R00/D01/I06’, … (1 bytes))
1

When running the same subscribe from another raspberry pi it doesn’t pick up the message. It seems that the messages aren’t making it out onto my network… but I would have thought that openhab being installed on the same machine as my broker would be able to pick up the messages, but that doesn’t seem to be the case.

void door_open() {

  // turn on wifi if we aren't connected
  if(WiFi.status() != WL_CONNECTED) {
    wifi_init();
  }
  
  Serial.println("Sending to Broker");
  mqtt_client.publish("/F1/R00/D01/I01","1");
  PrevDoor = HIGH;
}

void door_closed() {

  // turn on wifi if we aren't connected
  if(WiFi.status() != WL_CONNECTED) {
    wifi_init();
  }
  
  Serial.println("Sending to Broker");
  mqtt_client.publish("/F1/R00/D01/I01","0");
  PrevDoor = LOW;
  
}

The above is my Arduino code showing the type and message being sent from that end and should merry up to mqttcon1 item.

Try your items with ON, OFF, OPEN, CLOSED instead of on, off, open, closed and let us know if that works!

It’s also worth checking the ports are open if you have a firewall running anywhere, this sounds a bit like a port is blocked somewhere… ufw may well be running on the pi, with only ssh, ftp and http etc open.

This looks to me like you are only setting up the item binding as outgoing - i.e. when you flick the switch in openHAB a message is sent out on that MQTT topic. But there is no incoming binding setup - i.e. nothing listening on that topic for changes which are caused by a 3rd party.

I think you need to replicate what you have done in your “Front Door” contact binding, in the switch binding configs. I.e. add a < MQTT binding to monitor the topic for changes.

This looks to have partially made it work. The icon is updating appropriately. But the switch state on the sitemap isn’t changing. What item type should I use in the sitemap so that I can show a state? I would even be happy with using the text status of saying Door is open. But I can’t get that to work.

Currently right now that is all I was intending to have happen. The intention of those changes were to turn on an LED (light simulation) but those messages when sent out of OpenHAB were not making it to my ESP8266 module. But I wonder if it’s because of the same issue as the inbound of using lowercase instead of upper case. Iw ill have to try with the uppercase and report back.

I don’t believe it’s ports because when Mosquitto is running it’s running with the 0.0.0.0 as a wildcard for IP address. Meaning that any IP connecting to 1883 would work. And I also believe that it wouldn’t be blocked since I’ve verified that I’ve been able to update OpenHAB with the Contact swtich. I’m thinking I have a syntax issue somewhere.

Thank you so much as this was my issue for the inbound updates. Which led me to being able to troubleshoot my outgoing issue.

For my outgoing issue I found that I had a typo in my ESP8266 code for the Subscription.

I formerly had:

mqtt_client.subscribe("/F1/R00/D01/*");

but should be (and is now):

mqtt_client.subscribe("/F1/R00/D01/#");

Thanks so much for all the replies.

This seems to be my life story Brad. I’ve been wondering if it was just me. Thanks for reminding me that it’s not! :wink: And Happy New Year!