Skip to content
Open
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
1 change: 1 addition & 0 deletions platformio/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ extern const char *NTP_SERVER_1;
extern const char *NTP_SERVER_2;
extern const unsigned long NTP_TIMEOUT;
extern const int SLEEP_DURATION;
extern const int ERROR_SLEEP_DURATION;
extern const int BED_TIME;
extern const int WAKE_TIME;
extern const int HOURLY_GRAPH_MAX;
Expand Down
3 changes: 3 additions & 0 deletions platformio/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ const unsigned long NTP_TIMEOUT = 20000; // ms
// Note: The OpenWeatherMap model is updated every 10 minutes, so updating more
// frequently than that is unnessesary.
const int SLEEP_DURATION = 30; // minutes
// Similar to above, this allows for a shorter duration for sleep after encountering
// an error to allow for faster recovery in the case of an unreliable network.
const int ERROR_SLEEP_DURATION = 5; // minutes
// Bed Time Power Savings.
// If BED_TIME == WAKE_TIME, then this battery saving feature will be disabled.
// (range: [0-23])
Expand Down
40 changes: 28 additions & 12 deletions platformio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,22 @@ Preferences prefs;
/* Put esp32 into ultra low-power deep sleep (<11μA).
* Aligns wake time to the minute. Sleep times defined in config.cpp.
*/
void beginDeepSleep(unsigned long startTime, tm *timeInfo)
void beginDeepSleep(unsigned long startTime, tm *timeInfo, bool fromError = false)
{
if (!getLocalTime(timeInfo))
{
Serial.println(TXT_REFERENCING_OLDER_TIME_NOTICE);
}

int deepSleepDuration = SLEEP_DURATION;
if (fromError)
{
deepSleepDuration = ERROR_SLEEP_DURATION;
}

// To simplify sleep time calculations, the current time stored by timeInfo
// will be converted to time relative to the WAKE_TIME. This way if a
// SLEEP_DURATION is not a multiple of 60 minutes it can be more trivially,
// deepSleepDuration is not a multiple of 60 minutes it can be more trivially,
// aligned and it can easily be deterimined whether we must sleep for
// additional time due to bedtime.
// i.e. when curHour == 0, then timeInfo->tm_hour == WAKE_TIME
Expand All @@ -71,17 +77,17 @@ void beginDeepSleep(unsigned long startTime, tm *timeInfo)
const int curSecond = curHour * 3600
+ timeInfo->tm_min * 60
+ timeInfo->tm_sec;
const int desiredSleepSeconds = SLEEP_DURATION * 60;
const int offsetMinutes = curMinute % SLEEP_DURATION;
const int desiredSleepSeconds = deepSleepDuration * 60;
const int offsetMinutes = curMinute % deepSleepDuration;
const int offsetSeconds = curSecond % desiredSleepSeconds;

// align wake time to nearest multiple of SLEEP_DURATION
int sleepMinutes = SLEEP_DURATION - offsetMinutes;
// align wake time to nearest multiple of deepSleepDuration
int sleepMinutes = deepSleepDuration - offsetMinutes;
if (desiredSleepSeconds - offsetSeconds < 120
|| offsetSeconds / (float)desiredSleepSeconds > 0.95f)
{ // if we have a sleep time less than 2 minutes OR less 5% SLEEP_DURATION,
{ // if we have a sleep time less than 2 minutes OR less 5% deepSleepDuration,
// skip to next alignment
sleepMinutes += SLEEP_DURATION;
sleepMinutes += deepSleepDuration;
}

// estimated wake time, if this falls in a sleep period then sleepDuration
Expand Down Expand Up @@ -223,7 +229,7 @@ void setup()
} while (display.nextPage());
}
powerOffDisplay();
beginDeepSleep(startTime, &timeInfo);
beginDeepSleep(startTime, &timeInfo, true);
}

// TIME SYNCHRONIZATION
Expand All @@ -239,7 +245,7 @@ void setup()
drawError(wi_time_4_196x196, TXT_TIME_SYNCHRONIZATION_FAILED);
} while (display.nextPage());
powerOffDisplay();
beginDeepSleep(startTime, &timeInfo);
beginDeepSleep(startTime, &timeInfo, true);
}

// MAKE API REQUESTS
Expand All @@ -254,6 +260,11 @@ void setup()
#endif
int rxStatus = getOWMonecall(client, owm_onecall);
if (rxStatus != HTTP_CODE_OK)
{
// Perform one retry
rxStatus = getOWMonecall(client, owm_onecall);
}
if (rxStatus != HTTP_CODE_OK)
{
killWiFi();
statusStr = "One Call " + OWM_ONECALL_VERSION + " API";
Expand All @@ -264,10 +275,15 @@ void setup()
drawError(wi_cloud_down_196x196, statusStr, tmpStr);
} while (display.nextPage());
powerOffDisplay();
beginDeepSleep(startTime, &timeInfo);
beginDeepSleep(startTime, &timeInfo, true);
}
rxStatus = getOWMairpollution(client, owm_air_pollution);
if (rxStatus != HTTP_CODE_OK)
{
// Perform one retry
rxStatus = getOWMairpollution(client, owm_air_pollution);
}
if (rxStatus != HTTP_CODE_OK)
{
killWiFi();
statusStr = "Air Pollution API";
Expand All @@ -278,7 +294,7 @@ void setup()
drawError(wi_cloud_down_196x196, statusStr, tmpStr);
} while (display.nextPage());
powerOffDisplay();
beginDeepSleep(startTime, &timeInfo);
beginDeepSleep(startTime, &timeInfo, true);
}
killWiFi(); // WiFi no longer needed

Expand Down