Added PowerSaving for sensor firmware

This commit is contained in:
Kevin Le
2026-09-03 22:54:02 +07:00
parent af15c37d8b
commit e125d375ff
3 changed files with 26 additions and 0 deletions
+5
View File
@@ -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;
}
+3
View File
@@ -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; }
+18
View File
@@ -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();
}
}