Snorktracker
 All Data Structures Files Functions Variables Macros Pages
DeepSleep.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 SFini
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
23 #define NO_DEEP_SLEEP_STARTUP_TIME 120
24 
25 
31 {
32 protected:
35 
36 public:
37  MyDeepSleep(MyOptions &options, MyData &data);
38 
39  bool begin();
40 
41  bool haveToSleep();
42  void sleep(bool start = true);
43 };
44 
45 /* ******************************************** */
46 
49  : myOptions(options)
50  , myData(data)
51 {
52 }
53 
60 {
61  MyDbg(F("MyDeepSleep::begin"));
62 
63  MyData::RtcData rtcData;
64 
65  ESP.rtcUserMemoryRead(0, (uint32_t *) &rtcData, sizeof(MyData::RtcData));
66  if (!rtcData.isValid()) {
67  MyDbg(F("RtcData invalid (power on?)"));
68  } else {
69  MyDbg(F("RtcData read"));
70  myData.rtcData = rtcData;
71  }
72 
75  long checkTimeElapsed = secondsSincePowerOn() - myData.rtcData.deepSleepStartSec;
76 
77  // Check from time to time the power and return to deep sleep if the
78  // power is too low until the deep sleep time is over.
79  MyDbg((String) F("CheckTime elapsed: ") + String(checkTimeElapsed) + F(" sec"));
80  if (checkTimeElapsed < myOptions.deepSleepTimeSec) {
81  sleep(false); // back to sleep
82  }
83  MyDbg(F("Awake"));
84  }
85  }
86  return true;
87 }
88 
91 {
92  long activeTimeSec = millis() / 1000 - myData.awakeTimeOffsetSec;
93 
97  }
98 
99  return (myOptions.isDeepSleepEnabled &&
102  activeTimeSec >= myOptions.activeTimeSec);
103 }
104 
106 void MyDeepSleep::sleep(bool start /* = true */)
107 {
108  long powerCheckIntervalSec = myOptions.powerCheckIntervalSec;
109 
110  if (powerCheckIntervalSec > 60 * 60) {
111  MyDbg(F("Invalid DeepSleep time more than 60 Minutes!"));
112  powerCheckIntervalSec = 60 * 60;
113  }
114  MyDbg((String) F("Entering DeepSleep for: ") + String(myOptions.powerCheckIntervalSec) + F("Sec"));
115  delay(1000);
116 
117  if (start) {
119  }
120  myData.rtcData.aktiveTimeSec += millis() / 1000;
121  myData.rtcData.deepSleepTimeSec += powerCheckIntervalSec;
123  ESP.rtcUserMemoryWrite(0, (uint32_t *) &myData.rtcData, sizeof(MyData::RtcData));
124  ESP.deepSleep(powerCheckIntervalSec * 1000000);
125 }
long aktiveTimeSec
Time in active mode without current millis().
Definition: Data.h:37
long deepSleepTimeSec
Time to stay in deep sleep (without check interrupts)
Definition: Options.h:53
void setCRC()
Definition: Data.h:139
void sleep(bool start=true)
Definition: DeepSleep.h:106
double powerSaveModeVoltage
Minimum voltage to stay always alive.
Definition: Options.h:50
long awakeTimeOffsetSec
Awake time offset for SaveSettings.
Definition: Data.h:72
bool isValid()
Definition: Data.h:133
MyOptions & myOptions
Reference to the options.
Definition: DeepSleep.h:33
class MyData::RtcData rtcData
Data to store in the RTC memory.
MyOptions myOptions
The global options.
Definition: tracker.ino:57
Definition: Data.h:27
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
bool haveToSleep()
Definition: DeepSleep.h:90
MyData & myData
Reference to the data.
Definition: DeepSleep.h:34
long powerCheckIntervalSec
Time interval to check the power supply.
Definition: Options.h:51
long activeTimeSec
Maximum alive time after deepsleep.
Definition: Options.h:52
long secondsSincePowerOn()
Definition: tracker.ino:132
double voltage
Current supply voltage.
Definition: Data.h:74
#define NO_DEEP_SLEEP_STARTUP_TIME
No deep sleep for the first minute.
Definition: DeepSleep.h:23
long secondsToDeepSleep
Time until next deepsleep. -1 = disabled.
Definition: Data.h:71
long deepSleepTimeSec
Time in deep sleep mode.
Definition: Data.h:39
long deepSleepStartSec
Timestamp of the last deep sleep start.
Definition: Data.h:40
MyDeepSleep(MyOptions &options, MyData &data)
Definition: DeepSleep.h:48
MyData myData
The global collected data.
Definition: tracker.ino:58
bool isDeepSleepEnabled
Should the system go into deepsleep if needed.
Definition: Options.h:49
bool begin()
Definition: DeepSleep.h:59