From af3067d4814d80b0265260a958b219a664d7461c Mon Sep 17 00:00:00 2001 From: Peter Woolery Date: Thu, 23 Apr 2026 13:36:29 -0700 Subject: [PATCH] refactor(firmware): drive WiFi reconnect from net_guard events loop() no longer blocks for 5s after a disconnect; reconnect is scheduled from the WiFi event handler with exponential backoff. Buffered reports flush on every clean UP transition. --- firmware/lib/net_guard/library.json | 6 ++++++ firmware/src/main.cpp | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 firmware/lib/net_guard/library.json diff --git a/firmware/lib/net_guard/library.json b/firmware/lib/net_guard/library.json new file mode 100644 index 0000000..fe445e3 --- /dev/null +++ b/firmware/lib/net_guard/library.json @@ -0,0 +1,6 @@ +{ + "name": "net_guard", + "build": { + "flags": ["-I$PROJECT_SRC_DIR"] + } +} diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 475bae5..4cb595d 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -9,6 +9,7 @@ #include "ble_scanner.h" #include "reporter.h" #include "event_log.h" +#include "net_guard.h" #include // LED on GPIO2 (TimerCamera-F built-in LED) — verify against board schematic @@ -172,6 +173,7 @@ void setup() { ESP.restart(); } + net_guard_start(g_cfg); led_set(false); // off = connected // NTP sync (UTC) @@ -208,15 +210,14 @@ void setup() { void loop() { ArduinoOTA.handle(); check_factory_reset(); + net_guard_tick(); - if (WiFi.status() != WL_CONNECTED) { - led_set(true); // on = no WiFi - WiFi.reconnect(); - delay(5000); - if (WiFi.status() == WL_CONNECTED) { - led_set(false); - reporter_flush(g_cfg); - } + static bool s_was_up = true; + bool up = net_guard_is_up(); + if (up != s_was_up) { + led_set(!up); // LED on when NOT up + if (up) reporter_flush(g_cfg); + s_was_up = up; } - delay(1000); + delay(200); }