@@ -99,6 +99,11 @@ MyMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables, store
9999 #endif
100100);
101101
102+ // Power saving timing variables
103+ unsigned long lastActive = 0 ; // Last time there was activity
104+ unsigned long nextSleepInSecs = 120 ; // Wait 2 minutes before first sleep
105+ const unsigned long WORK_TIME_SECS = 5 ; // Stay awake 5 seconds after wake/activity
106+
102107/* END GLOBAL OBJECTS */
103108
104109void halt () {
@@ -216,6 +221,9 @@ void setup() {
216221#ifdef DISPLAY_CLASS
217222 ui_task.begin (disp, &sensors, the_mesh.getNodePrefs ()); // still want to pass this in as dependency, as prefs might be moved
218223#endif
224+
225+ // Initialize power saving timer
226+ lastActive = millis ();
219227}
220228
221229void loop () {
@@ -225,4 +233,31 @@ void loop() {
225233 ui_task.loop ();
226234#endif
227235 rtc_clock.tick ();
236+
237+ // Power saving when BLE/WiFi is disabled
238+ // Don't sleep if GPS is enabled - it needs continuous operation to maintain fix
239+ // Note: Disabling BLE/WiFi via UI actually turns off the radio to save power
240+ if (!serial_interface.isEnabled () && !the_mesh.getNodePrefs ()->gps_enabled ) {
241+ // Check for pending work and update activity timer
242+ if (the_mesh.hasPendingWork ()) {
243+ lastActive = millis ();
244+ if (nextSleepInSecs < 10 ) {
245+ nextSleepInSecs += 5 ; // Extend work time by 5s if still busy
246+ }
247+ }
248+
249+ // Only sleep if enough time has passed since last activity
250+ if (millis () >= lastActive + (nextSleepInSecs * 1000 )) {
251+ #ifdef PIN_USER_BTN
252+ // Sleep for 30 minutes, wake on LoRa packet, timer, or button press
253+ board.enterLightSleep (1800 , PIN_USER_BTN);
254+ #else
255+ // Sleep for 30 minutes, wake on LoRa packet or timer
256+ board.enterLightSleep (1800 );
257+ #endif
258+ // Just woke up - reset timers
259+ lastActive = millis ();
260+ nextSleepInSecs = WORK_TIME_SECS; // Stay awake for 5s after wake
261+ }
262+ }
228263}
0 commit comments