Skip to content

[deepSleep] Make deepSleep work again with 2.5.0 and refactor delay var #2217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/Misc.ino
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool readyForSleep()
return timeOutReached(timerAwakeFromDeepSleep + 1000 * Settings.deepSleep);
}

void deepSleep(int delay)
void deepSleep(int dsdelay)
{

checkRAM(F("deepSleep"));
Expand Down Expand Up @@ -158,28 +158,35 @@ void deepSleep(int delay)
}
}
saveUserVarToRTC();
deepSleepStart(delay); // Call deepSleepStart function after these checks
deepSleepStart(dsdelay); // Call deepSleepStart function after these checks
}

void deepSleepStart(int delay)
void deepSleepStart(int dsdelay)
{
// separate function that is called from above function or directly from rules, usign deepSleep as a one-shot
String event = F("System#Sleep");
rulesProcessing(event);


RTC.deepSleepState = 1;
saveToRTC();

if (delay > 4294 || delay < 0)
delay = 4294; //max sleep time ~1.2h

addLog(LOG_LEVEL_INFO, F("SLEEP: Powering down to deepsleep..."));
delay(100); // give the node time to send above log message before going to sleep
#if defined(ESP8266)
ESP.deepSleep((uint32_t)delay * 1000000, WAKE_RF_DEFAULT);
#if defined(CORE_2_5_0)
uint64_t deepSleep_usec = dsdelay * 1000000ULL;
if ((deepSleep_usec > ESP.deepSleepMax()) || dsdelay < 0) {
deepSleep_usec = ESP.deepSleepMax();
}
ESP.deepSleepInstant(deepSleep_usec, WAKE_RF_DEFAULT);
#else
if (dsdelay > 4294 || dsdelay < 0)
dsdelay = 4294; //max sleep time ~71 minutes
ESP.deepSleep((uint32_t)dsdelay * 1000000, WAKE_RF_DEFAULT);
#endif
#endif
#if defined(ESP32)
esp_sleep_enable_timer_wakeup((uint32_t)delay * 1000000);
esp_sleep_enable_timer_wakeup((uint32_t)dsdelay * 1000000);
esp_deep_sleep_start();
#endif
}
Expand Down Expand Up @@ -515,9 +522,9 @@ void statusLED(boolean traffic)
/********************************************************************************************\
delay in milliseconds with background processing
\*********************************************************************************************/
void delayBackground(unsigned long delay)
void delayBackground(unsigned long dsdelay)
{
unsigned long timer = millis() + delay;
unsigned long timer = millis() + dsdelay;
while (!timeOutReached(timer))
backgroundtasks();
}
Expand Down
15 changes: 13 additions & 2 deletions src/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1271,8 +1271,19 @@ void handle_config() {
addHelpButton(F("SleepMode"));
addFormNote(F("0 = Sleep Disabled, else time awake from sleep"));

addFormNumericBox( F("Sleep time"), F("delay"), Settings.Delay, 0, 4294); //limited by hardware to ~1.2h
addUnit(F("sec"));
int dsmax = 4294; // About 71 minutes
#if defined(CORE_2_5_0)
dsmax = INT_MAX;
if ((ESP.deepSleepMax()/1000000ULL) <= (uint64_t)INT_MAX)
dsmax = (int)(ESP.deepSleepMax()/1000000ULL);
#endif
addFormNumericBox( F("Sleep time"), F("delay"), Settings.Delay, 0, dsmax); //limited by hardware
{
String maxSleeptimeUnit = F("sec (max: ");
maxSleeptimeUnit += String(dsmax);
maxSleeptimeUnit += ')';
addUnit(maxSleeptimeUnit);
}

addFormCheckBox(F("Sleep on connection failure"), F("deepsleeponfail"), Settings.deepSleepOnFail);

Expand Down