homieiot / homie-esp8266

💡 ESP8266 framework for Homie, a lightweight MQTT convention for the IoT
http://homieiot.github.io/homie-esp8266
MIT License
1.36k stars 308 forks source link

MQTT reconnect fails after Wifi-disconnect #736

Open dobernhardt opened 1 year ago

dobernhardt commented 1 year ago

When rebooting the Wifi-AP or temporarily shutting down Wifi for any other reason the device does not reconnect to the MQTT broker but gets stuck in an endless reconnection loop

✔ Wi-Fi connected, IP: 192.168.100.90
Triggering WIFI_CONNECTED event...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...
↕ Attempting to connect to MQTT...

If only the connection to the MQTT broker is lost a reconnect works without any problems.

I'm using latest e0347c08231592672c16c07356127a8f5d539c26 from develop on an esp32 (M5StickPlus).

dobernhardt commented 1 year ago

Probably related to https://github.com/homieiot/homie-esp8266/issues/320 ?

dobernhardt commented 1 year ago

Solved it with the workaround

void onHomieEvent(HomieEvent event) {
  switch(event.type) {
    case HomieEventType::WIFI_DISCONNECTED:
        Logger::log(LogLevel::LOG_WARN,"Got Wifi Disconnected event") ;
        ESP.restart() ;
        break ;
  }
}
luebbe commented 1 year ago

OK, that's the sledgehammer method :) The develop branch is the most up to date and works fine for me, but I'm only using esp8266, so I can't say if it is ok for ESP32.

Pierre-33 commented 1 year ago

Hi, I'm having exactly the same issue with e0347c0 on an esp8266.

After some test, it appears that it's because I use AsyncMqttClient 0.9.0 instead of the 0.8.2 packaged with Homie. Was that your case as well?

Unfortunately my home assistant auto discovery payload seems too big for 0.8.2, so I really need to stick to 0.9.0

dobernhardt commented 1 year ago

I was indeed using the version 0.9.0 of the AsyncMqttClient library. I have not tested with an older version and I'm still relying on my sledgehammer workaround resetting the device on WIFI_DISCONNECTED.

luebbe commented 1 year ago

@bertmelis has developed a much more stable mqtt library (https://github.com/bertmelis/espMqttClient). It is long on my todo list to replace asyncmqtt with bert's library in homie.

Pierre-33 commented 1 year ago

I was indeed using the version 0.9.0 of the AsyncMqttClient library. I have not tested with an older version and I'm still relying on my sledgehammer workaround resetting the device on WIFI_DISCONNECTED.

I use the same kind of tricks but on the WIFI_CONNECTED event as doing the reboot in the WIFI_DISCONNECTED event somehow made the esp reboot again and again untill the Wifi is back.

Pierre-33 commented 1 year ago

@bertmelis has developed a much more stable mqtt library (https://github.com/bertmelis/espMqttClient). It is long on my todo list to replace asyncmqtt with bert's library in homie.

That sounds like a lot of works. I was under the impression that Homie development has kind of stopped. I was looking for a tiny synchronous MQTT Client that I could use to publish my HomeAssistant Autodiscovery message when the esp boot and then shut it down and let Homie works with 0.8.2. But all I could find it "huge" async mqtt client that requires an update loop and I didn't feel like having two Mqtt client working in parallel even if it's just during setup.

bertmelis commented 1 year ago

Wel actually, you only need Homie.

Let Homie do the work and use its mqtt client to send the discovery messages: Homie.getMqttClient().

Pierre-33 commented 1 year ago

yes, that's what I do, but Homie Mqtt Client doesn't support "big" payload (my payload are actually quite small). That's why I upgrade it to 0.9 in the first place. And now I got this reconnection issue. So I was looking for a solution to rollback to AsyncMqttClient 0.8.2 and use another client for my "big" payload.