Skip to content

ESP8266 MQTT reconnect permanently fails after internet outage until full reboot (ThingsBoard Arduino SDK) #271

Description

@arimukhlas

I'm using ESP8266 with ThingsBoard Arduino SDK and MQTT over WiFiClient.

My setup:

WiFiClient *espClient = nullptr;
ThingsBoard *tb = nullptr;

and I recreate the client dynamically:

void initThingsBoardClient() {
  if (tb != nullptr) {
    tb->disconnect();
    delete tb;
    tb = nullptr;
  }

  if (espClient != nullptr) {
    espClient->flush();
    espClient->stop();
    delete espClient;
    espClient = nullptr;
  }

  espClient = new WiFiClient();
  tb = new ThingsBoard(*espClient, MAX_MESSAGE_SIZE);
}

The device behavior is:

  1. ESP8266 boots normally
  2. WiFi connects
  3. ThingsBoard MQTT connects successfully
  4. Telemetry works correctly for hours

Then I simulate internet outage:

  • Router WiFi remains connected ("WL_CONNECTED")
  • But internet access is removed

During outage:

  • MQTT connection fails as expected
  • Pending telemetry is stored locally in LittleFS
  • NTP resync fails as expected

When internet access is restored:

  • Internet check becomes OK again
  • NTP resync works successfully
  • But MQTT connection to ThingsBoard keeps failing forever

Debug example:

[NET] Internet status = OK
[NET] Internet restored, recreate TB client
[TB] Client recreated
[NTP] Resync OK
Connecting TB : iot.pdam-sby.go.id : 1883
[TB] Connecting to server failed
TB connect failed

This repeats forever.

Important:

  • WiFi remains connected
  • DNS works
  • NTP works again after internet restore
  • Only ThingsBoard MQTT reconnect fails
  • Restarting ESP8266 immediately fixes everything
  • After restart, ThingsBoard reconnects successfully and all pending telemetry is sent

I already tried:

  • "tb.disconnect()"
  • "espClient.flush()"
  • "espClient.stop()"
  • recreating "WiFiClient"
  • recreating "ThingsBoard"
  • WiFi reconnect
  • WiFi.mode(WIFI_OFF)
  • recreating MQTT objects dynamically

But MQTT reconnect still fails until full ESP restart.

Questions:

  1. Is this a known PubSubClient / ThingsBoard SDK issue on ESP8266?
  2. Is there any internal MQTT state that survives object recreation?
  3. Is there a recommended full MQTT reset procedure without rebooting ESP?
  4. Could repeated failed reconnect attempts after internet outage cause internal socket lockup?
  5. Is there a safer reconnect flow recommended by the SDK maintainers?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions