LongDeepSleep
 
Loading...
Searching...
No Matches
LongDeepSleep.h
Go to the documentation of this file.
1#ifndef _LONGDEEPSLEEP_H_
2#define _LONGDEEPSLEEP_H_
3
4#ifdef _MOCK_TEST_
5#include "MockTestSetup.h"
6#else
7#include <ESP8266WiFi.h>
8#include <WiFiUdp.h>
9#include <NTPClient.h>
10#include <include/WiFiState.h>
11#endif
19{
20public:
21
25 enum {
31 };
32
33
37 enum {
38 OK = 0,
42 };
43
53
54
63 LongDeepSleep(CloneWorkaround param_cloneWorkaround, const char* ssid = nullptr, const char* password = nullptr, NTPClient* ntpClient = nullptr);
64
72 LongDeepSleep(const char* ssid = nullptr, const char* password = nullptr, NTPClient* ntpClient = nullptr)
73 :LongDeepSleep(NO_WORKAROUND,ssid,password,ntpClient){}
74
84 int checkWakeUp(uint32_t toleranceSec = 0, uint16_t failureSleepSecs = 0);
85
93 void performLongDeepSleep(uint64_t sleepTimeSec);
94
102 int performLongDeepSleepUntil(uint64_t epocheTime);
103
111 void restoreWifi(uint16_t failureSleepSecs = 0);
112
118 void releaseWifi();
119
129 inline void changeWaitCycles(uint16_t quickConnectCycles, uint16_t wifiWaitCycles)
130 {
131 maxQuickConnectCycles = quickConnectCycles;
132 maxWifiWaitCycles = wifiWaitCycles;
133 }
134
142 uint32_t rebootCounter(){return rtcData.rebootCounter;}
149 void resetRebootCounter(uint32_t resetValue=0){rtcData.rebootCounter=resetValue;}
150
158 static uint32_t UsedRTCDataSize(){return sizeof(UsedRTCData);}
159
160private:
161 void saveRTCAndCallLongDeepSleep(uint64_t sleepTimeSec);
162 bool readFromRTCmemory();
163 void writeRTCmemory();
164 void saveWifiInformation();
165 void resetWifi();
166 void resetRTCdata();
167 uint64_t deepSleepMax();
168
169 uint32_t calculateCRC32(const uint8_t *data, size_t length);
170
171 bool RTCmemoryValid = false;
172 int sleepTimeUntilWifiReconnectSec = 60 * 60;
173
174 uint16_t maxQuickConnectCycles = 20;
175 uint16_t maxWifiWaitCycles = 2000;
176
177 const char* wifiSsid;
178 const char* wifiPassword;
179 NTPClient* timeClient;
180
181 CloneWorkaround cloneWorkaround = NO_WORKAROUND;
182
186 struct UsedRTCData{
187 uint32_t crc32;
188 uint32_t rebootCounter;
189 uint64_t remainingSleepTimeUs;
190 uint32_t sleepUntilEpocheTime;
191 WiFiState wifiState;
192 } rtcData;
193};
194
195#endif
void changeWaitCycles(uint16_t quickConnectCycles, uint16_t wifiWaitCycles)
Customizes connection wait cycles.
Definition LongDeepSleep.h:129
LongDeepSleep(CloneWorkaround param_cloneWorkaround, const char *ssid=nullptr, const char *password=nullptr, NTPClient *ntpClient=nullptr)
Second constructor for clone modules with deep sleep problems.
Definition LongDeepSleep.cpp:7
void performLongDeepSleep(uint64_t sleepTimeSec)
Sleep for a relative amount of time (seconds).
Definition LongDeepSleep.cpp:169
static uint32_t UsedRTCDataSize()
Returns RTC memory size used by this library.
Definition LongDeepSleep.h:158
void restoreWifi(uint16_t failureSleepSecs=0)
Restores WiFi after wake-up from deep sleep.
Definition LongDeepSleep.cpp:201
int checkWakeUp(uint32_t toleranceSec=0, uint16_t failureSleepSecs=0)
Checks whether the device woke up from deep sleep and handles state.
Definition LongDeepSleep.cpp:14
CloneWorkaround
Differrent types of workarounds for deepsleep problems with clone modules.
Definition LongDeepSleep.h:47
@ UNKNOWN
Definition LongDeepSleep.h:51
@ WEMOS_D1_V3_CLONE
This worked for my Wemos D1 mini V3 clones.
Definition LongDeepSleep.h:49
@ NO_WORKAROUND
This is standard solution, no workaround is applied.
Definition LongDeepSleep.h:48
@ ESP01_MODULE
esp01 modules, not tested yet. Just copy/pasted code from here: https://github.com/nokxs/esp8266-alte...
Definition LongDeepSleep.h:50
void releaseWifi()
Releases WiFi to conserve power.
Definition LongDeepSleep.cpp:305
@ DEEP_SLEEP_UNTIL_DONE
Wake-up completed, WiFi and time server already available.
Definition LongDeepSleep.h:27
@ DEEP_SLEEP_DONE
Wake-up completed, proceed. WiFi not yet restored.
Definition LongDeepSleep.h:26
@ ABSOLUTE_TIME_CHECK_FAILED
Wake-up for absolute time failed due to time server/WiFi issues.
Definition LongDeepSleep.h:30
@ RTC_MEMORY_CHECK_FAILED
RTC memory is invalid or corrupted.
Definition LongDeepSleep.h:29
@ OTHER_WAKE_UP_REASON
Wake-up due to other reason (reset, power-on, etc.).
Definition LongDeepSleep.h:28
uint32_t rebootCounter()
Returns a number that is increased in each call of checkWakeUp and also saved in RTC memory.
Definition LongDeepSleep.h:142
void resetRebootCounter(uint32_t resetValue=0)
Resets the reboot counter to any other value.
Definition LongDeepSleep.h:149
@ SPECIFIED_TIME_IN_PAST
The specified until time stamp lies in the past, hence sleep not required, do whatever you want to do...
Definition LongDeepSleep.h:41
@ OK
Will never be seen as then the ESP will go into deep sleep.
Definition LongDeepSleep.h:38
@ NO_ABSOLUE_TIME_AVAILABLE
time client was specified, but it was not possible to determine an absolute time, hence this function...
Definition LongDeepSleep.h:40
@ NO_TIME_CLIENT
No time client specified when creating class longdeepsleep, hence this function is not available.
Definition LongDeepSleep.h:39
LongDeepSleep(const char *ssid=nullptr, const char *password=nullptr, NTPClient *ntpClient=nullptr)
Constructor.
Definition LongDeepSleep.h:72
int performLongDeepSleepUntil(uint64_t epocheTime)
Sleep until an absolute epoch time.
Definition LongDeepSleep.cpp:176