MQTT trouble
This commit is contained in:
24
gauge.py
24
gauge.py
@@ -877,28 +877,6 @@ def _subscribe_all(c):
|
|||||||
c.subscribe(f"{prefix}/status_led/green/set")
|
c.subscribe(f"{prefix}/status_led/green/set")
|
||||||
|
|
||||||
|
|
||||||
def _mqtt_connect_with_timeout(client, timeout_s=10):
|
|
||||||
"""Call client.connect() with a socket-level timeout to avoid blocking forever."""
|
|
||||||
try:
|
|
||||||
import usocket as _sm
|
|
||||||
except ImportError:
|
|
||||||
import socket as _sm
|
|
||||||
_orig = _sm.socket
|
|
||||||
def _timed(*a, **kw):
|
|
||||||
s = _orig(*a, **kw)
|
|
||||||
s.settimeout(timeout_s)
|
|
||||||
return s
|
|
||||||
_sm.socket = _timed
|
|
||||||
try:
|
|
||||||
client.connect()
|
|
||||||
finally:
|
|
||||||
_sm.socket = _orig
|
|
||||||
try:
|
|
||||||
client.sock.settimeout(None)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
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} ...")
|
||||||
@@ -920,7 +898,7 @@ def connect_mqtt():
|
|||||||
keepalive=30,
|
keepalive=30,
|
||||||
)
|
)
|
||||||
client.set_callback(on_message)
|
client.set_callback(on_message)
|
||||||
_mqtt_connect_with_timeout(client)
|
client.connect()
|
||||||
client_ref = client
|
client_ref = client
|
||||||
_mqtt_connected = True
|
_mqtt_connected = True
|
||||||
info(f"MQTT connected client_id={MQTT_CLIENT_ID}")
|
info(f"MQTT connected client_id={MQTT_CLIENT_ID}")
|
||||||
|
|||||||
Reference in New Issue
Block a user