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(){rtcData.rebootCounter=0;}
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
168 uint32_t calculateCRC32(const uint8_t *data, size_t length);
169
170 bool RTCmemoryValid = false;
171 int sleepTimeUntilWifiReconnectSec = 60 * 60;
172
173 uint16_t maxQuickConnectCycles = 20;
174 uint16_t maxWifiWaitCycles = 2000;
175
176 const char* wifiSsid;
177 const char* wifiPassword;
178 NTPClient* timeClient;
179
180 CloneWorkaround cloneWorkaround = NO_WORKAROUND;
181
185 struct UsedRTCData{
186 uint32_t crc32;
187 uint32_t rebootCounter;
188 uint64_t remainingSleepTimeUs;
189 uint32_t sleepUntilEpocheTime;
190 WiFiState wifiState;
191 } rtcData;
192};
193
194#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:155
void resetRebootCounter()
Releases WiFi to conserve power.
Definition LongDeepSleep.h:149
static uint32_t UsedRTCDataSize()
Releases WiFi to conserve power.
Definition LongDeepSleep.h:158
void restoreWifi(uint16_t failureSleepSecs=0)
Restores WiFi after wake-up from deep sleep.
Definition LongDeepSleep.cpp:187
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:285
@ 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()
Releases WiFi to conserve power.
Definition LongDeepSleep.h:142
@ 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:162