From 95724bf3ff2178832140f9b3b216ee7d360ed721 Mon Sep 17 00:00:00 2001 From: Peter Woolery Date: Thu, 23 Apr 2026 13:21:23 -0700 Subject: [PATCH] feat(firmware): log boot and reboot reason to event log Every boot logs EVT_BOOT with esp_reset_reason(); every deliberate ESP.restart() is preceded by EVT_REBOOT with a reason code. This gives us a persistent answer to 'why did the device just reboot?'. --- firmware/src/main.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 91faca3..475bae5 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -8,6 +8,8 @@ #include "cv.h" #include "ble_scanner.h" #include "reporter.h" +#include "event_log.h" +#include // LED on GPIO2 (TimerCamera-F built-in LED) — verify against board schematic // Factory reset: hold GPIO37 (BOOT button) for 5 seconds @@ -45,6 +47,7 @@ static void check_factory_reset() { uint32_t held = millis(); while (digitalRead(BUTTON_PIN) == LOW) { if (millis() - held >= FACTORY_RESET_HOLD_MS) { + event_log_write(EVT_REBOOT, REBOOT_FACTORY_RESET, 0); config_clear_wifi(); ESP.restart(); } @@ -140,6 +143,9 @@ void setup() { pinMode(BUTTON_PIN, INPUT_PULLUP); led_set(true); // on = booting + event_log_init(); + event_log_write(EVT_BOOT, (uint16_t)esp_reset_reason(), 0); + if (!config_load(g_cfg)) { Serial.println("FATAL: device_id/location_id/hmac_secret not provisioned"); while (true) { delay(500); led_set(!digitalRead(LED_PIN)); } // fast blink @@ -148,6 +154,7 @@ void setup() { // Connect to WiFi if (!config_has_wifi()) { provisioning_run(); + event_log_write(EVT_REBOOT, REBOOT_WIFI_REPROV, 0); ESP.restart(); } @@ -161,6 +168,7 @@ void setup() { if (WiFi.status() != WL_CONNECTED) { // Saved creds failed — re-provision provisioning_run(); + event_log_write(EVT_REBOOT, REBOOT_WIFI_REPROV, 0); ESP.restart(); } @@ -183,7 +191,11 @@ void setup() { // OTA update support ArduinoOTA.setHostname(g_cfg.device_id.c_str()); ArduinoOTA.onStart([]() { ble_scanner_pause(); }); - ArduinoOTA.onEnd([]() { ble_scanner_resume(); ESP.restart(); }); + ArduinoOTA.onEnd([]() { + ble_scanner_resume(); + event_log_write(EVT_REBOOT, REBOOT_OTA, 0); + ESP.restart(); + }); ArduinoOTA.onError([](ota_error_t e) { ble_scanner_resume(); }); ArduinoOTA.begin();