Adafruit_CircuitPython_MiniMQTT
Adafruit_CircuitPython_MiniMQTT copied to clipboard
MQTT Connection Breaks After Reconnecting to WiFi When Messages Are Sent During Disconnection
I'm using a Raspberry Pi Pico W with an MQTT client to connect to Adafruit IO. The Pico W handles WiFi reconnection well when the connection drops. However, there's an issue when messages are sent to the subscribed topic while the Pico W is disconnected from WiFi. After reconnecting to WiFi:
- The Pico W does not receive new messages that are sent after it reconnects.
- After a few seconds, it becomes unstable and eventually fails to reconnect to the MQTT broker entirely.
- The log shows a socket error: "An existing socket is already connected to mqtt://io.adafruit.com:1883".
Important Note: If no messages are sent to the topic while the Pico W is offline, everything works as expected. The Pico W maintains the same MQTT connection after WiFi reconnection and continues receiving messages without any issues. In this case, the Pico doesn’t even need to reconnect to the broker—everything remains stable and functional.
Steps to Reproduce:
- Connect the Pico W to WiFi and the MQTT broker.
- Subscribe to a topic (e.g., myfeed/onoff).
- Disconnect the Pico W from WiFi (e.g., by disabling the hotspot).
- While the Pico W is disconnected, send a message to the topic (e.g., using Adafruit IO).
- Re-enable WiFi, allowing the Pico W to reconnect automatically.
- Observe that the Pico W fails to handle incoming messages and eventually cannot reconnect to the MQTT broker.
Log Output:
Connecting to WiFi
Connected to WiFi
My MAC addr: ['0x28', '0xcd', '0xc1', '0x3', '0xeb', '0x27']
My IP address is 192.168.169.203
...
KeepAlive period elapsed - requesting a PINGRESP from the server...
Sending PINGREQ
Attempting to reconnect with MQTT broker
Resetting reconnect backoff
Attempting to connect to MQTT broker (attempt #0)
Attempting to establish MQTT connection...
Socket error when connecting: An existing socket is already connected to mqtt://io.adafruit.com:1883
The problem seems tied to messages being sent to the topic while the Pico W is disconnected from WiFi.