Skip to content

Frequent Reconnections on ESP32 #1085

@xzbit22

Description

@xzbit22

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

  1. Is there a recommended way to implement reconnect backoff in PubSubClient for unstable networks?
  2. Could the keep-alive handling be improved to better tolerate mobile network fluctuations?
  3. Are there known issues with PubSubClient on ESP32 when using smartphone hotspots?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions