Skip to content
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

try to get correct uptime without millis() overrolled issue. #121

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
12 changes: 11 additions & 1 deletion src/NTPClientLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,17 @@ void NTPClient::onNTPSyncEvent (onSyncEvent_t handler) {
}

time_t NTPClient::getUptime () {
_uptime = _uptime + (millis () - _uptime);
unsigned long long currentMillis = millis();
if (currentMillis < _lastMillis)
{
// millis() has rolled over
_uptime += (ULONG_MAX - _lastMillis) + currentMillis;
}
else
{
_uptime += (currentMillis - _lastMillis);
}
_lastMillis = currentMillis;
return _uptime / 1000;
}

Expand Down
3 changes: 2 additions & 1 deletion src/NtpClientLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ class NTPClient {
int _longInterval = DEFAULT_NTP_INTERVAL; ///< Interval to set periodic time sync
time_t _lastSyncd = 0; ///< Stored time of last successful sync
time_t _firstSync = 0; ///< Stored time of first successful sync after boot
unsigned long _uptime = 0; ///< Time since boot
unsigned long long _uptime = 0; ///< Time since boot
unsigned long _lastMillis = 0; ///< defined to avoid millis() rollover
uint16_t ntpTimeout = 1500; ///< Response timeout for NTP requests
onSyncEvent_t onSyncEvent; ///< Event handler callback

Expand Down