Attempt with tcp probe

This commit is contained in:
2026-04-21 20:26:31 +02:00
parent 1f8ba45685
commit 2ea19b14f8

View File

@@ -884,25 +884,13 @@ def _subscribe_all(c):
c.subscribe(f"{prefix}/status_led/green/set")
def _probe_tcp(host, port, timeout_s=10):
"""Open and immediately close a TCP connection to verify the route is up."""
import usocket
s = usocket.socket()
s.settimeout(timeout_s)
try:
s.connect(usocket.getaddrinfo(host, port)[0][-1])
finally:
s.close()
def connect_mqtt():
global client_ref, _mqtt_connected
info(f"Connecting to MQTT broker {MQTT_BROKER}:{MQTT_PORT} ...")
last_error = None
for attempt in range(3):
gc.collect()
try:
_probe_tcp(MQTT_BROKER, MQTT_PORT)
if client_ref is not None:
try:
client_ref.disconnect()
@@ -925,7 +913,7 @@ def connect_mqtt():
return
except Exception as e:
last_error = e
log_err(f"MQTT connect attempt {attempt + 1} failed: {e}")
log_err(f"MQTT connect attempt {attempt + 1} failed: {type(e).__name__}: {e}")
try:
client.sock.close()
except Exception: