Snorktracker
 All Data Structures Files Functions Variables Macros Pages
Public Member Functions | Protected Attributes
MyDeepSleep Class Reference

#include <DeepSleep.h>

Public Member Functions

 MyDeepSleep (MyOptions &options, MyData &data)
 
bool begin ()
 
bool haveToSleep ()
 
void sleep (bool start=true)
 

Protected Attributes

MyOptionsmyOptions
 Reference to the options.
 
MyDatamyData
 Reference to the data.
 

Detailed Description

Class to read/save a deepsleep counter and start the deepsleep mode if the voltage is too low.

Definition at line 30 of file DeepSleep.h.

Constructor & Destructor Documentation

MyDeepSleep::MyDeepSleep ( MyOptions options,
MyData data 
)

Constructor

Definition at line 48 of file DeepSleep.h.

49  : myOptions(options)
50  , myData(data)
51 {
52 }
MyOptions & myOptions
Reference to the options.
Definition: DeepSleep.h:33
MyData & myData
Reference to the data.
Definition: DeepSleep.h:34

Member Function Documentation

bool MyDeepSleep::begin ( )

Read the deepsleep counter from the RTC memory. Use a simple random value variable to identify if the counter is still initialized

Definition at line 59 of file DeepSleep.h.

References MyData::RtcData::deepSleepStartSec, MyOptions::deepSleepTimeSec, MyOptions::isDeepSleepEnabled, MyData::RtcData::isValid(), myData, MyDbg(), myOptions, NO_DEEP_SLEEP_STARTUP_TIME, MyOptions::powerSaveModeVoltage, MyData::rtcData, secondsSincePowerOn(), sleep(), and MyData::voltage.

Referenced by setup().

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 }
long deepSleepTimeSec
Time to stay in deep sleep (without check interrupts)
Definition: Options.h:53
void sleep(bool start=true)
Definition: DeepSleep.h:106
double powerSaveModeVoltage
Minimum voltage to stay always alive.
Definition: Options.h:50
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.
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
MyData & myData
Reference to the data.
Definition: DeepSleep.h:34
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 deepSleepStartSec
Timestamp of the last deep sleep start.
Definition: Data.h:40
bool isDeepSleepEnabled
Should the system go into deepsleep if needed.
Definition: Options.h:49
bool MyDeepSleep::haveToSleep ( )

Check if the configured time has elapsed and the voltage is too low then go into deep sleep.

Definition at line 90 of file DeepSleep.h.

References MyOptions::activeTimeSec, MyData::awakeTimeOffsetSec, MyOptions::isDeepSleepEnabled, myData, myOptions, NO_DEEP_SLEEP_STARTUP_TIME, MyOptions::powerSaveModeVoltage, secondsSincePowerOn(), MyData::secondsToDeepSleep, and MyData::voltage.

Referenced by loop().

91 {
92  long activeTimeSec = millis() / 1000 - myData.awakeTimeOffsetSec;
93 
97  }
98 
99  return (myOptions.isDeepSleepEnabled &&
102  activeTimeSec >= myOptions.activeTimeSec);
103 }
double powerSaveModeVoltage
Minimum voltage to stay always alive.
Definition: Options.h:50
long awakeTimeOffsetSec
Awake time offset for SaveSettings.
Definition: Data.h:72
MyOptions & myOptions
Reference to the options.
Definition: DeepSleep.h:33
MyData & myData
Reference to the data.
Definition: DeepSleep.h:34
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
bool isDeepSleepEnabled
Should the system go into deepsleep if needed.
Definition: Options.h:49
void MyDeepSleep::sleep ( bool  start = true)

Entering the DeepSleep mode. Be sure we have connected the RST pin to the D0 pin for wakeup.

Definition at line 106 of file DeepSleep.h.

References MyData::RtcData::aktiveTimeSec, MyData::RtcData::deepSleepStartSec, MyData::RtcData::deepSleepTimeSec, myData, MyDbg(), myOptions, MyOptions::powerCheckIntervalSec, MyData::rtcData, secondsSincePowerOn(), and MyData::RtcData::setCRC().

Referenced by begin(), and loop().

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
void setCRC()
Definition: Data.h:139
MyOptions & myOptions
Reference to the options.
Definition: DeepSleep.h:33
class MyData::RtcData rtcData
Data to store in the RTC memory.
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
MyData & myData
Reference to the data.
Definition: DeepSleep.h:34
long powerCheckIntervalSec
Time interval to check the power supply.
Definition: Options.h:51
long secondsSincePowerOn()
Definition: tracker.ino:132
long deepSleepTimeSec
Time in deep sleep mode.
Definition: Data.h:39
long deepSleepStartSec
Timestamp of the last deep sleep start.
Definition: Data.h:40

The documentation for this class was generated from the following file: