fix: use paho-mqtt threaded loop for stable MQTT reconnection

The Meshtastic MQTT bridge was using client.loop(timeout=1.0) in a
blocking while loop. When the broker dropped the connection (common
after ~30s of idle in Docker), the client silently stopped receiving
messages with no auto-reconnect.

Switch to client.loop_start() which runs the MQTT network loop in a
background thread with built-in automatic reconnection. Also:
- Add on_disconnect callback for visibility into disconnection events
- Set reconnect_delay_set(1, 30) for fast exponential-backoff reconnect
- Lower keepalive from 60s to 30s to stay within Docker network timeouts
This commit is contained in:
anoracleofra-code
2026-03-26 16:48:06 -06:00
parent 81b99c0571
commit 86d2145b97
+13 -2
View File
@@ -536,15 +536,26 @@ class MeshtasticBridge:
else:
logger.error(f"Meshtastic MQTT connection refused: rc={rc}")
def _on_disconnect(client, userdata, rc):
if rc != 0:
logger.warning(f"Meshtastic MQTT disconnected unexpectedly (rc={rc}), will auto-reconnect")
else:
logger.info("Meshtastic MQTT disconnected cleanly")
client = mqtt.Client(client_id="shadowbroker-mesh", protocol=mqtt.MQTTv311)
client.username_pw_set(self.USER, self.PASS)
client.on_connect = _on_connect
client.on_message = self._on_message
client.on_disconnect = _on_disconnect
client.reconnect_delay_set(min_delay=1, max_delay=30)
client.connect(self.HOST, self.PORT, keepalive=60)
client.connect(self.HOST, self.PORT, keepalive=30)
client.loop_start()
while not self._stop.is_set():
client.loop(timeout=1.0)
self._stop.wait(1.0)
client.loop_stop()
client.disconnect()
def _on_message(self, client, userdata, msg):