Attempt with tcp probe

This commit is contained in:
2026-04-21 20:10:38 +02:00
parent 5a98a3c63b
commit 1f8ba45685

View File

@@ -884,12 +884,25 @@ def _subscribe_all(c):
c.subscribe(f"{prefix}/status_led/green/set") 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(): def connect_mqtt():
global client_ref, _mqtt_connected global client_ref, _mqtt_connected
info(f"Connecting to MQTT broker {MQTT_BROKER}:{MQTT_PORT} ...") info(f"Connecting to MQTT broker {MQTT_BROKER}:{MQTT_PORT} ...")
last_error = None last_error = None
for attempt in range(3): for attempt in range(3):
try: try:
_probe_tcp(MQTT_BROKER, MQTT_PORT)
if client_ref is not None: if client_ref is not None:
try: try:
client_ref.disconnect() client_ref.disconnect()