Frequent Reconnections on ESP32
I'm experiencing frequent reconnections when using PubSubClient on an ESP32 connected to a Mosquitto 2.0.22 broker via a smartphone hotspot (4G/WiFi). The client repeatedly disconnects and reconnects within seconds, causing a loop of "Client <client_id> already connected, closing old connection" in the Mosquitto log. This happens even when the network signal is relatively stable.
The ESP32 reconnects frequently (every 1–4 seconds), as seen in the Mosquitto log:
2025-09-24 15:03:19: New connection from 36.67.33.97:54867 on port 1883.
2025-09-24 15:03:19: Client lidya already connected, closing old connection.
2025-09-24 15:03:19: New client connected from 36.67.33.97:54867 as lidya (p2, c1, k60, u'lidya').
2025-09-24 15:03:23: New connection from 36.67.33.97:63186 on port 1883.
2025-09-24 15:03:23: Client lidya already connected, closing old connection.
The client reconnects with different TCP ports, and the broker closes the old connection. This creates a loop, overloading the broker and duplicating messages.
Questions
- Is there a recommended way to implement reconnect backoff in PubSubClient for unstable networks?
- Could the keep-alive handling be improved to better tolerate mobile network fluctuations?
- Are there known issues with PubSubClient on ESP32 when using smartphone hotspots?
Can you please share the code how you connect to the broker. If I look into the example there is a 5s delay that I don't see in the log ...
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
You can also update to https://github.com/hmueller01/pubsubclient3 and enable -D DEBUG_PUBSUBCLIENT to get more information from the client ...
If your client looses connection it must reconnect. That shouldn't be a problem, if connection is stable long enough.
The keep alive timeout is only used by the broker to detect a disconnect and send the LWT message to the other clients.
void reconnectMQTT() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
if (mqttClient.connect(clientID.c_str(), mqttusername.c_str(), mqtt_password.c_str())) {
Serial.println("Connected");
if (mqttClient.subscribe(mqttControlTopic.c_str())) {
Serial.println("Berhasil subscribe ke topik [" + mqttControlTopic + "]");
} else {
Serial.println("Gagal subscribe ke topik [" + mqttControlTopic + "]");
}
} else {
Serial.print("Failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" Try again in 5 seconds");
delay(5000);
}
}
}
this is my code
I've run into this problem when you have more than one client with the same client name. Each client is connecting to the broker, the broker is disconnecting the old one in favor of the new one, the old one then reconnects, and the cycle repeats indefinitely. Using unique client names for every device is what solved it for me.
On Fri, Oct 3, 2025 at 5:05 AM xzbit22 @.***> wrote:
xzbit22 left a comment (knolleary/pubsubclient#1085) https://github.com/knolleary/pubsubclient/issues/1085#issuecomment-3365431630
void reconnectMQTT() { while (!mqttClient.connected()) { Serial.print("Attempting MQTT connection..."); if (mqttClient.connect(clientID.c_str(), mqttusername.c_str(), mqtt_password.c_str())) { Serial.println("Connected"); if (mqttClient.subscribe(mqttControlTopic.c_str())) { Serial.println("Berhasil subscribe ke topik [" + mqttControlTopic + "]"); } else { Serial.println("Gagal subscribe ke topik [" + mqttControlTopic + "]"); } } else { Serial.print("Failed, rc="); Serial.print(mqttClient.state()); Serial.println(" Try again in 5 seconds"); delay(5000); } } }
this is my code
— Reply to this email directly, view it on GitHub https://github.com/knolleary/pubsubclient/issues/1085#issuecomment-3365431630, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUQXV5ZXVT3O5WOWQVVPMFT3VZQ7ZAVCNFSM6AAAAACHLCF4XSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGNRVGQZTCNRTGA . You are receiving this because you are subscribed to this thread.Message ID: @.***>