From 4b7a44679edd32cf23279b7eb20c44b6aa76bdb7 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 17 Jul 2025 12:12:37 +0200 Subject: [PATCH 1/4] getCellularTime add flag to select local/UTC time --- src/ArduinoCellular.cpp | 8 ++++++-- src/ArduinoCellular.h | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index a4f0d2b..8e6c899 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -126,7 +126,7 @@ Time ArduinoCellular::getGPSTime(){ return Time(year, month, day, hour, minute, second); } -Time ArduinoCellular::getCellularTime(){ +Time ArduinoCellular::getCellularTime(bool localTime){ int year = 1970; int month = 1; int day = 1; @@ -135,7 +135,11 @@ Time ArduinoCellular::getCellularTime(){ int second = 0; float tz; if (modem.NTPServerSync() == 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); } diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index 8f32de5..fd09cc9 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -163,7 +163,7 @@ class ArduinoCellular { * @brief Gets the current time from the network. * @return The current time. */ - Time getCellularTime(); + Time getCellularTime(bool localTime = true); /** * @brief Gets the current time from the GPS module. From 2a37d6c349dd33256a81ac0bdc1caddf89ad6364 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 17 Jul 2025 12:32:48 +0200 Subject: [PATCH 2/4] Add flags to handle NTP sync --- src/ArduinoCellular.cpp | 19 ++++++++++++------- src/ArduinoCellular.h | 5 +++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index 8e6c899..f032f64 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -126,7 +126,7 @@ Time ArduinoCellular::getGPSTime(){ return Time(year, month, day, hour, minute, second); } -Time ArduinoCellular::getCellularTime(bool localTime){ +Time ArduinoCellular::getCellularTime(bool localTime, bool forceNTPSync) { int year = 1970; int month = 1; int day = 1; @@ -134,13 +134,18 @@ Time ArduinoCellular::getCellularTime(bool localTime){ int minute = 0; int second = 0; float tz; - if (modem.NTPServerSync() == 0) { - if (localTime) { - modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); - } else { - modem.getNetworkUTCTime(&year, &month, &day, &hour, &minute, &second, &tz); - } + + if ((needNTPSync || forceNTPSync) && (modem.NTPServerSync() != 0)) { + return Time(year, month, day, hour, minute, second); } + needNTPSync = false; + + 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); } diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index fd09cc9..3a970a2 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -163,7 +163,7 @@ class ArduinoCellular { * @brief Gets the current time from the network. * @return The current time. */ - Time getCellularTime(bool localTime = true); + Time getCellularTime(bool localTime = true, bool forceNTPSync = false); /** * @brief Gets the current time from the GPS module. @@ -277,7 +277,6 @@ class ArduinoCellular { private: bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass); - /** * @brief Waits for network registration. (Blocking call) @@ -304,6 +303,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). */ + + bool needNTPSync = true; /**< Flag to indicate if NTP synchronization is needed. */ }; From 66dd34a6db4a4649aabdd2d45e7b82d0b524bdc2 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 17 Jul 2025 15:38:52 +0200 Subject: [PATCH 3/4] use mktime to compute unix timestamp --- src/TimeUtils.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/TimeUtils.h b/src/TimeUtils.h index 2d400bb..f9980cf 100644 --- a/src/TimeUtils.h +++ b/src/TimeUtils.h @@ -91,9 +91,7 @@ class Time { * @return The time in UNIX timestamp format. */ String getUNIXTimestampString() { - // Simple conversion to UNIX timestamp, not accounting for leap years or time zones - long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926; - return String(timestamp); + return String(getUNIXTimestamp()); } /** @@ -101,9 +99,27 @@ class Time { * @return The time in UNIX timestamp format. */ unsigned long getUNIXTimestamp() { - // Simple conversion to UNIX timestamp, not accounting for leap years or time zones - unsigned long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926; - return timestamp; + struct tm t = + { + 0 /* tm_sec */, + 0 /* tm_min */, + 0 /* tm_hour */, + 0 /* tm_mday */, + 0 /* tm_mon */, + 0 /* tm_year */, + 0 /* tm_wday */, + 0 /* tm_yday */, + 0 /* tm_isdst */ + }; + t.tm_mon = month - 1; + t.tm_mday = day; + t.tm_year = year - 1900; + t.tm_hour = hour; + t.tm_min = minute; + t.tm_sec = second; + t.tm_isdst = -1; + + return mktime(&t); } /** From bfbd25791d200b113d640e16eed43ce547ab0b0c Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 17 Jul 2025 16:22:17 +0200 Subject: [PATCH 4/4] propagate waitForever flag to connectToGPRS --- src/ArduinoCellular.cpp | 11 ++++++++--- src/ArduinoCellular.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index f032f64..bc90e0c 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -78,7 +78,7 @@ bool ArduinoCellular::connect(String apn, String username, String password, bool return true; } - if(connectToGPRS(apn.c_str(), username.c_str(), password.c_str())){ + if(connectToGPRS(apn.c_str(), username.c_str(), password.c_str(), waitForever)){ auto response = this->sendATCommand("+QIDNSCFG=1,\"8.8.8.8\",\"8.8.4.4\""); if(response.indexOf("OK") != -1){ @@ -196,11 +196,16 @@ bool ArduinoCellular::isConnectedToOperator(){ return modem.isNetworkConnected(); } -bool ArduinoCellular::connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass){ +bool ArduinoCellular::connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass, bool waitForever){ if(this->debugStream != nullptr){ this->debugStream->println("Connecting to 4G network..."); } while(!modem.gprsConnect(apn, gprsUser, gprsPass)) { + + if(!waitForever) { + return false; + } + if(this->debugStream != nullptr){ this->debugStream->print("."); } @@ -252,7 +257,7 @@ bool ArduinoCellular::awaitNetworkRegistration(bool waitForever){ this->debugStream->println("Waiting for network registration..."); } while (!modem.waitForNetwork(waitForNetworkTimeout)) { - + if(!waitForever) { return false; } diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index 3a970a2..7eaca8a 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -276,7 +276,7 @@ class ArduinoCellular { SimStatus getSimStatus(); private: - bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass); + bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass, bool waitForever = true); /** * @brief Waits for network registration. (Blocking call)