From e125d375ffccaaaa551360f980355c151c09213d Mon Sep 17 00:00:00 2001 From: Kevin Le Date: Thu, 3 Sep 2026 22:54:02 +0700 Subject: [PATCH] Added PowerSaving for sensor firmware --- examples/simple_sensor/SensorMesh.cpp | 5 +++++ examples/simple_sensor/SensorMesh.h | 3 +++ examples/simple_sensor/main.cpp | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index 9bfa5ec6..0c958e74 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -979,3 +979,8 @@ void SensorMesh::loop() { dirty_contacts_expiry = 0; } } + +// To check if there is pending work +bool SensorMesh::hasPendingWork() const { + return _mgr->getOutboundTotal() > 0; +} \ No newline at end of file diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index 7b9d7767..44ba497b 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -52,6 +52,9 @@ public: void begin(FILESYSTEM* fs); void loop(); void handleCommand(uint32_t sender_timestamp, char* command, char* reply); + + // To check if there is pending work + bool hasPendingWork() const; // CommonCLI callbacks const char* getFirmwareVer() override { return FIRMWARE_VERSION; } diff --git a/examples/simple_sensor/main.cpp b/examples/simple_sensor/main.cpp index 7585663a..69ecbf2e 100644 --- a/examples/simple_sensor/main.cpp +++ b/examples/simple_sensor/main.cpp @@ -52,6 +52,9 @@ void halt() { static char command[160]; +// For power saving +unsigned long POWERSAVING_FIRSTSLEEP_SECS = 120; // The first sleep (if enabled) from boot + void setup() { Serial.begin(115200); delay(1000); @@ -166,4 +169,19 @@ void loop() { #ifdef HAS_EXTERNAL_WATCHDOG external_watchdog.loop(); #endif + + if (the_mesh.getNodePrefs()->powersaving_enabled && !the_mesh.hasPendingWork()) { +#if defined(NRF52_PLATFORM) + board.sleep(0); // nrf ignores seconds param, sleeps whenever possible +#else + if (the_mesh.millisHasNowPassed(POWERSAVING_FIRSTSLEEP_SECS * 1000)) { // To check if it is time to sleep + board.sleep(30); // Sleep. Wake up after a while or when receiving a LoRa packet + } +#endif + } + + if (the_mesh.getNodePrefs()->reboot_interval > 0 && + the_mesh.millisHasNowPassed(the_mesh.getNodePrefs()->reboot_interval * 3600000)) { + board.reboot(); + } }