OpenCode experiments...
This commit is contained in:
43
gauge.py
43
gauge.py
@@ -25,19 +25,10 @@ import network
|
|||||||
import utime
|
import utime
|
||||||
import ujson
|
import ujson
|
||||||
import gc
|
import gc
|
||||||
|
import usocket
|
||||||
from umqtt.robust import MQTTClient
|
from umqtt.robust import MQTTClient
|
||||||
from machine import UART
|
from machine import UART
|
||||||
|
|
||||||
# Activate WiFi driver before any heavy heap allocation so it can claim its
|
|
||||||
# contiguous DRAM block before the Python heap fragments the address space.
|
|
||||||
# Only activate if not already running (e.g. boot.py may have started it).
|
|
||||||
gc.collect()
|
|
||||||
_early_wlan = network.WLAN(network.STA_IF)
|
|
||||||
if not _early_wlan.active():
|
|
||||||
_early_wlan.active(True)
|
|
||||||
del _early_wlan
|
|
||||||
gc.collect()
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Logging
|
# Logging
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -577,6 +568,22 @@ def connect_wifi(ssid, password, timeout_s=15, force_reconnect=False):
|
|||||||
info(f" MAC : {mac}")
|
info(f" MAC : {mac}")
|
||||||
info(f" IP : {ip} mask:{mask} gw:{gw} dns:{dns}")
|
info(f" IP : {ip} mask:{mask} gw:{gw} dns:{dns}")
|
||||||
utime.sleep_ms(500)
|
utime.sleep_ms(500)
|
||||||
|
if dns in ("0.0.0.0", ""):
|
||||||
|
info("DNS not yet from DHCP — waiting ...")
|
||||||
|
for _ in range(30):
|
||||||
|
utime.sleep_ms(200)
|
||||||
|
ip, mask, gw, dns = _wifi_sta.ifconfig()
|
||||||
|
if dns not in ("0.0.0.0", ""):
|
||||||
|
info(f"DHCP settled: DNS={dns}")
|
||||||
|
break
|
||||||
|
info(" still waiting ...")
|
||||||
|
utime.sleep_ms(1000)
|
||||||
|
info(f"Testing DNS for {MQTT_BROKER} ...")
|
||||||
|
try:
|
||||||
|
ai = usocket.getaddrinfo(MQTT_BROKER, MQTT_PORT)
|
||||||
|
info(f"DNS resolved: {ai[0][4][0]}")
|
||||||
|
except Exception as e:
|
||||||
|
warn(f"DNS test failed: {e}")
|
||||||
return ip
|
return ip
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
last_error = e
|
last_error = e
|
||||||
@@ -894,16 +901,25 @@ def connect_mqtt():
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
broker_ip = MQTT_BROKER
|
||||||
|
try:
|
||||||
|
ai = usocket.getaddrinfo(MQTT_BROKER, MQTT_PORT)
|
||||||
|
broker_ip = ai[0][4][0]
|
||||||
|
info(f"Resolved {MQTT_BROKER} → {broker_ip}")
|
||||||
|
except Exception as e:
|
||||||
|
warn(f"Could not resolve {MQTT_BROKER}: {e} — using hostname as-is")
|
||||||
client = MQTTClient(
|
client = MQTTClient(
|
||||||
client_id=MQTT_CLIENT_ID,
|
client_id=MQTT_CLIENT_ID,
|
||||||
server=MQTT_BROKER,
|
server=broker_ip,
|
||||||
port=MQTT_PORT,
|
port=MQTT_PORT,
|
||||||
user=MQTT_USER,
|
user=MQTT_USER,
|
||||||
password=MQTT_PASSWORD,
|
password=MQTT_PASSWORD,
|
||||||
keepalive=30,
|
keepalive=30,
|
||||||
)
|
)
|
||||||
client.set_callback(on_message)
|
client.set_callback(on_message)
|
||||||
|
client.sock.settimeout(15)
|
||||||
client.connect()
|
client.connect()
|
||||||
|
client.sock.settimeout(0)
|
||||||
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}")
|
||||||
@@ -1311,6 +1327,11 @@ def apply_motion_defaults():
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
gc.collect()
|
||||||
|
_w = network.WLAN(network.STA_IF)
|
||||||
|
if not _w.active():
|
||||||
|
_w.active(True)
|
||||||
|
del _w
|
||||||
gc.collect()
|
gc.collect()
|
||||||
info("=" * 48)
|
info("=" * 48)
|
||||||
info("Gauge MQTT controller starting")
|
info("Gauge MQTT controller starting")
|
||||||
|
|||||||
Reference in New Issue
Block a user