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:
- ESP8266 boots normally
- WiFi connects
- ThingsBoard MQTT connects successfully
- 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:
- Is this a known PubSubClient / ThingsBoard SDK issue on ESP8266?
- Is there any internal MQTT state that survives object recreation?
- Is there a recommended full MQTT reset procedure without rebooting ESP?
- Could repeated failed reconnect attempts after internet outage cause internal socket lockup?
- Is there a safer reconnect flow recommended by the SDK maintainers?
Thank you.
I'm using ESP8266 with ThingsBoard Arduino SDK and MQTT over WiFiClient.
My setup:
and I recreate the client dynamically:
The device behavior is:
Then I simulate internet outage:
During outage:
When internet access is restored:
Debug example:
This repeats forever.
Important:
I already tried:
But MQTT reconnect still fails until full ESP restart.
Questions:
Thank you.