Skip to content

Commit 732e3a6

Browse files
Merge pull request brettdottech#245 from dreed47/NTP-fixes
Set NTP update interval to every 15 minutes. Also check for update r…
2 parents 5c0b41f + 5692b4f commit 732e3a6

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

firmware/src/core/globaltime/GlobalTime.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "ConfigManager.h"
44
#include "config_helper.h"
55
#include <ArduinoJson.h>
6+
#include <ArduinoLog.h>
67

78
GlobalTime *GlobalTime::m_instance = nullptr;
89

@@ -13,7 +14,7 @@ GlobalTime::GlobalTime() {
1314
m_ntpServer = cm->getConfigString("ntpServer", m_ntpServer); // config added in MainHelper
1415
Serial.printf("GlobalTime initialized, tzLoc=%s, clockFormat=%d, ntpServer=%s\n", m_timezoneLocation.c_str(), clockFormat, m_ntpServer.c_str());
1516
m_format24hour = (clockFormat == CLOCK_FORMAT_24_HOUR);
16-
m_timeClient = new NTPClient(m_udp, m_ntpServer.c_str());
17+
m_timeClient = new NTPClient(m_udp, m_ntpServer.c_str(), 0, m_updateInterval);
1718
m_timeClient->begin();
1819
}
1920

@@ -33,8 +34,17 @@ void GlobalTime::updateTime(bool force) {
3334
if (m_timeZoneOffset == -1 || (m_nextTimeZoneUpdate > 0 && m_unixEpoch > m_nextTimeZoneUpdate)) {
3435
getTimeZoneOffsetFromAPI();
3536
}
36-
m_timeClient->update();
37+
bool updated = m_timeClient->update();
3738
m_unixEpoch = m_timeClient->getEpochTime();
39+
if (updated) {
40+
if (year(m_unixEpoch) < m_lowYearTest || year(m_unixEpoch) > m_highYearTest) {
41+
Log.warningln("NTP updated but year is not in acceptable range, skipping update, trying again...");
42+
m_timeClient->setUpdateInterval(3000); // try again in 3 seconds
43+
return;
44+
}
45+
Log.noticeln("NTP updated successful - Year is: %d", year(m_unixEpoch));
46+
m_timeClient->setUpdateInterval(m_updateInterval); // reset update interval
47+
}
3848
m_updateTimer = millis();
3949
m_minute = minute(m_unixEpoch);
4050
if (m_format24hour) {
@@ -173,4 +183,4 @@ bool GlobalTime::getFormat24Hour() {
173183
bool GlobalTime::setFormat24Hour(bool format24hour) {
174184
m_format24hour = format24hour;
175185
return m_format24hour;
176-
}
186+
}

firmware/src/core/globaltime/GlobalTime.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class GlobalTime {
9292
WiFiUDP m_udp;
9393
NTPClient *m_timeClient{nullptr};
9494

95+
unsigned long m_updateInterval = 900000; // Update every 15 min
96+
const int m_lowYearTest = 2025;
97+
const int m_highYearTest = 2035;
9598
unsigned long m_oneSecond = 1000;
9699
unsigned long m_updateTimer = 0;
97100

@@ -101,4 +104,4 @@ class GlobalTime {
101104
void getTimeZoneOffsetFromAPI();
102105
};
103106

104-
#endif // GLOBALTIME_H
107+
#endif // GLOBALTIME_H

0 commit comments

Comments
 (0)