espMqttClient: connect/disconnect/reconnect not working

@GnomiBerlin

          Hi,

I had the same observation due to an instable WiFi connection to an ESP32-CAM module. http Server was always reachable after WiFi reconnects, but the MQTT stops after some time.

Then I changed to this 1.4.4. version, but still have this problem. In the http page I added two links for ESP.restart() and for mqttClient.disconnect(). Both are working. But after some time, when MQTT is dead, the mqttClient.disconnect() is not helping.

I want not to open this issue as it is may be another reason, but MQTT is not stable.

Environment:

Dependency Graph
|-- espMqttClient @ 1.4.4
|-- AsyncElegantOTA @ 2.2.7
|-- AsyncTCP-esphome @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.0.0
|-- SPIFFS @ 2.0.0
|-- AsyncTCP @ 1.1.1
|-- Ticker @ 2.0.0
|-- WiFi @ 2.0.0

Code snippets:

void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
  #ifdef DEBUG
  Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
  #endif
  if (WiFi.isConnected()) {
    reconnectMqtt = true;
    lastReconnect = millis();
  }
}
// Reconnect MQTT  via Webinterface using disconnect() and auto-reconnect from async MQTT
  httpserver.on("/MQTTrestart", HTTP_GET, [](AsyncWebServerRequest *request)
    {
    String htmlBody = htmlHead_pre_b + htmlHead + "<h1>MQTT restarting ...</h1>";
    request->send(200, "text/html", htmlBody);
    delay(500);
    if (!mqttClient.disconnect()) {
    #ifdef DEBUG
    Serial.println("Disconnecting failed.");
    #endif
    } else {
    #ifdef DEBUG
    Serial.println("Disconnection done.");
    #endif
    } 
    });

Debugs: After ESP32 restart:

Connecting to MQTT...
Connection done.
Connected to MQTT broker: 192.168.xxx.xxx, port: 1883
************************************************
Session present: 0
Last Will -online- setting- QoS 1/retain false, packetId: 1
Subscribing at QoS 1, packetId: 2
Subscribing at QoS 1, packetId: 3
Subscribing at QoS 1, packetId: 4
Subscribing at QoS 1, packetId: 5
Subscribing at QoS 1, packetId: 6
Subscribing at QoS 1, packetId: 7
Subscribing at QoS 1, packetId: 8
Subscribing at QoS 1, packetId: 9

MQTT Publish succesful: 10
Publish acknowledged.
  packetId: 1
Subscribe acknowledged.
  packetId: 2
  qos: 1
Subscribe acknowledged.
  packetId: 3
  qos: 1
Subscribe acknowledged.
  packetId: 4
  qos: 1
Subscribe acknowledged.
  packetId: 5
  qos: 1
Subscribe acknowledged.
  packetId: 6
  qos: 1
Subscribe acknowledged.
  packetId: 7
  qos: 1
Subscribe acknowledged.
  packetId: 8
  qos: 1
Subscribe acknowledged.
  packetId: 9
  qos: 1
Publish acknowledged.
  packetId: 10

After mqttClient.disconnect():

Disconnection done.
Disconnected from MQTT: 0.
Connecting to MQTT...
Connection done.
Connected to MQTT broker: 192.168.xxx.xxx, port: 1883
************************************************
Session present: 0
Last Will -online- setting- QoS 1/retain false, packetId: 11
Subscribing at QoS 1, packetId: 12
Subscribing at QoS 1, packetId: 13
Subscribing at QoS 1, packetId: 14
Subscribing at QoS 1, packetId: 15
Subscribing at QoS 1, packetId: 16
Subscribing at QoS 1, packetId: 17
Subscribing at QoS 1, packetId: 18
Subscribing at QoS 1, packetId: 19

MQTT Publish succesful: 20
Publish acknowledged.
  packetId: 11
Subscribe acknowledged.
  packetId: 12
  qos: 1
Subscribe acknowledged.
  packetId: 13
  qos: 1
Subscribe acknowledged.
  packetId: 14
  qos: 1
Subscribe acknowledged.
  packetId: 15
  qos: 1
Subscribe acknowledged.
  packetId: 16
  qos: 1
Subscribe acknowledged.
  packetId: 17
  qos: 1
Subscribe acknowledged.
  packetId: 18
  qos: 1
Subscribe acknowledged.
  packetId: 19
  qos: 1
Publish acknowledged.
  packetId: 20

_Originally posted by @GnomiBerlin in https://github.com/bertmelis/espMqttClient/issues/102#issuecomment-1646839715_

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 37 (21 by maintainers)

Most upvoted comments

ESP32 has an operating system so you can define different tasks. Like on your PC, you have different programs running at the same time.

espMqttClient (by default) creates its own task where it does the connecting, sending and receiving. This tasks works independently from the code you have in your loop() in your Arduino program.

So while your Arduino code is running in one task, espMqttClient is running in another task. The espMqttCient task might be blocked during connecting but your Arduino task keeps running .

ESP8266 doesn’t have an operating system like ESP32 has. So you cannot create a separate task. Hence, if espMqttClient has to wait for the connection to be made, your Arduino code will also have to wait. The Async version works differently. No code is running until there is a certain event (TCP connected or disconnected, MQTT packet comes in, you enqueue a packet…)

I am using the async version and yes, it is bad WiFi as I dismounted the antenna and put into my hands to simulate the bad environment and to reach the reconnect state, where this error happens. I will try your logging input soon.