From fbecb076edacc9a53f8e39891c6bd77d0f5cb879 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 22 Jul 2025 09:55:36 +0200 Subject: [PATCH 1/5] Add function to handle NTP sync Function will be called by both getTime() and getCellularTime() to sync NTP if needed. --- src/ArduinoCellular.cpp | 14 +++++++++++++- src/ArduinoCellular.h | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index a4f0d2b..af440fd 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -12,6 +12,18 @@ unsigned long ArduinoCellular::getTime() { return Time(year, month, day, hour, minute, second).getUNIXTimestamp(); } +int ArduinoCellular::syncNTPServer(bool forceNTPSync) { + static bool needNTPSync = true; + if(!needNTPSync && !forceNTPSync) { + return 0; + } + if(modem.NTPServerSync() == 0) { + needNTPSync = false; + return 0; + } + return -1; +} + ArduinoCellular::ArduinoCellular() { } @@ -134,7 +146,7 @@ Time ArduinoCellular::getCellularTime(){ int minute = 0; int second = 0; float tz; - if (modem.NTPServerSync() == 0) { + if (syncNTPServer() == 0) { modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); } return Time(year, month, day, hour, minute, second); diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index 8f32de5..24cdf6b 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -304,6 +304,8 @@ class ArduinoCellular { static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */ static constexpr unsigned long waitForNetworkTimeout = 20000L; /**< Maximum wait time for network registration (In milliseconds). */ + + static int syncNTPServer(bool forceNTPSync = false); /** Function for synchronizing the NTP server. */ }; From 1ced48c8d979d85dc304d9fe3902e10fc90dd8c3 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 22 Jul 2025 09:59:58 +0200 Subject: [PATCH 2/5] getTime() return UNIX epoch if not in sync with NTP --- src/ArduinoCellular.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index af440fd..fdb84ca 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -6,9 +6,16 @@ #endif unsigned long ArduinoCellular::getTime() { - int year, month, day, hour, minute, second; + int year = 1970; + int month = 1; + int day = 1; + int hour = 0; + int minute = 0; + int second = 0; float tz; - modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); + if (syncNTPServer() == 0) { + modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); + } return Time(year, month, day, hour, minute, second).getUNIXTimestamp(); } From ecbb77d83a17eb7e6178bb223bc9dbd23b6cc77a Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 22 Jul 2025 10:14:26 +0200 Subject: [PATCH 3/5] add getTimeStruct to handle local/utc time --- src/ArduinoCellular.cpp | 27 +++++++++++++-------------- src/ArduinoCellular.h | 2 ++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index fdb84ca..fdadad3 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -5,7 +5,7 @@ #include "Watchdog.h" #endif -unsigned long ArduinoCellular::getTime() { +Time ArduinoCellular::getTimeStruct(bool localTime) { int year = 1970; int month = 1; int day = 1; @@ -14,9 +14,17 @@ unsigned long ArduinoCellular::getTime() { int second = 0; float tz; if (syncNTPServer() == 0) { - modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); + if (localTime) { + modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); + } else { + modem.getNetworkUTCTime(&year, &month, &day, &hour, &minute, &second, &tz); + } } - return Time(year, month, day, hour, minute, second).getUNIXTimestamp(); + return Time(year, month, day, hour, minute, second); +} + +unsigned long ArduinoCellular::getTime() { + return getTimeStruct().getUNIXTimestamp(); } int ArduinoCellular::syncNTPServer(bool forceNTPSync) { @@ -146,17 +154,8 @@ Time ArduinoCellular::getGPSTime(){ } Time ArduinoCellular::getCellularTime(){ - int year = 1970; - int month = 1; - int day = 1; - int hour = 0; - int minute = 0; - int second = 0; - float tz; - if (syncNTPServer() == 0) { - modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); - } - return Time(year, month, day, hour, minute, second); + // Get the current time from the network as localtime + return getTimeStruct(true); } diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index 24cdf6b..c3200bc 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -303,6 +303,8 @@ class ArduinoCellular { static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */ + static Time getTimeStruct(bool localTime = false); /** Function for getting the current time as a Time object. */ + static constexpr unsigned long waitForNetworkTimeout = 20000L; /**< Maximum wait time for network registration (In milliseconds). */ static int syncNTPServer(bool forceNTPSync = false); /** Function for synchronizing the NTP server. */ From 4fc33a295859efa35bc9419db78b4ae896968a3f Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 22 Jul 2025 10:38:22 +0200 Subject: [PATCH 4/5] Add syncCellularTime() public method --- src/ArduinoCellular.cpp | 4 ++++ src/ArduinoCellular.h | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index fdadad3..86a08f7 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -158,6 +158,10 @@ Time ArduinoCellular::getCellularTime(){ return getTimeStruct(true); } +bool ArduinoCellular::syncCellularTime(){ + // Sync the time with the network NTP service + return syncNTPServer(true) == 0 ? true : false; +} void ArduinoCellular::sendSMS(String number, String message){ modem.sendAT("+CMGF=1"); diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index c3200bc..2b5be38 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -158,13 +158,19 @@ class ArduinoCellular { * @return The GPS location. If the location is not retrieved, the latitude and longitude will be 0.0. */ Geolocation getGPSLocation(unsigned long timeout = 60000); - + /** * @brief Gets the current time from the network. * @return The current time. */ Time getCellularTime(); + /** + * @brief Sync the modem time using NTP service. + * @return True on success false otherwise. + */ + bool syncCellularTime(); + /** * @brief Gets the current time from the GPS module. * @return The current time. From 520415f97e51bbfcfe90bf925c73409b7100c2a9 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 22 Jul 2025 10:42:37 +0200 Subject: [PATCH 5/5] getCellularTime: add flag to handle local/utc time --- src/ArduinoCellular.cpp | 4 ++-- src/ArduinoCellular.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index 86a08f7..4b34eab 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -153,9 +153,9 @@ Time ArduinoCellular::getGPSTime(){ return Time(year, month, day, hour, minute, second); } -Time ArduinoCellular::getCellularTime(){ +Time ArduinoCellular::getCellularTime(bool localTime){ // Get the current time from the network as localtime - return getTimeStruct(true); + return getTimeStruct(localTime); } bool ArduinoCellular::syncCellularTime(){ diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index 2b5be38..31923be 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -161,9 +161,10 @@ class ArduinoCellular { /** * @brief Gets the current time from the network. + * @param localTime If true, the time will be converted to local time. Default is true. * @return The current time. */ - Time getCellularTime(); + Time getCellularTime(bool localTime = true); /** * @brief Sync the modem time using NTP service.