Skip to content

Commit 1c562b1

Browse files
committed
Add function to handle NTP sync
Function will be called by both getTime() and getCellularTime() to sync NTP if needed.
1 parent 8efd7f4 commit 1c562b1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/ArduinoCellular.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ unsigned long ArduinoCellular::getTime() {
1212
return Time(year, month, day, hour, minute, second).getUNIXTimestamp();
1313
}
1414

15+
int ArduinoCellular::syncNTPServer(bool forceNTPSync) {
16+
static bool needNTPSync = true;
17+
if((needNTPSync || forceNTPSync) && !modem.NTPServerSync()) {
18+
// NTP sync failed
19+
return 1;
20+
}
21+
// No sync needed
22+
needNTPSync = false;
23+
return 0;
24+
}
25+
1526
ArduinoCellular::ArduinoCellular() {
1627
}
1728

@@ -134,7 +145,7 @@ Time ArduinoCellular::getCellularTime(){
134145
int minute = 0;
135146
int second = 0;
136147
float tz;
137-
if (modem.NTPServerSync() == 0) {
148+
if (syncNTPServer() == 0) {
138149
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
139150
}
140151
return Time(year, month, day, hour, minute, second);

src/ArduinoCellular.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ class ArduinoCellular {
304304
static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */
305305

306306
static constexpr unsigned long waitForNetworkTimeout = 20000L; /**< Maximum wait time for network registration (In milliseconds). */
307+
308+
static int syncNTPServer(bool forceNTPSync = false); /** Function for synchronizing the NTP server. */
307309
};
308310

309311

0 commit comments

Comments
 (0)