Skip to content

Commit fbecb07

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 fbecb07

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/ArduinoCellular.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ 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) {
18+
return 0;
19+
}
20+
if(modem.NTPServerSync() == 0) {
21+
needNTPSync = false;
22+
return 0;
23+
}
24+
return -1;
25+
}
26+
1527
ArduinoCellular::ArduinoCellular() {
1628
}
1729

@@ -134,7 +146,7 @@ Time ArduinoCellular::getCellularTime(){
134146
int minute = 0;
135147
int second = 0;
136148
float tz;
137-
if (modem.NTPServerSync() == 0) {
149+
if (syncNTPServer() == 0) {
138150
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
139151
}
140152
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)