diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index a4f0d2b..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){ @@ -126,7 +126,7 @@ Time ArduinoCellular::getGPSTime(){ return Time(year, month, day, hour, minute, second); } -Time ArduinoCellular::getCellularTime(){ +Time ArduinoCellular::getCellularTime(bool localTime, bool forceNTPSync) { int year = 1970; int month = 1; int day = 1; @@ -134,9 +134,18 @@ Time ArduinoCellular::getCellularTime(){ int minute = 0; int second = 0; float tz; - if (modem.NTPServerSync() == 0) { + + 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); } @@ -187,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("."); } @@ -243,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 8f32de5..7eaca8a 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, bool forceNTPSync = false); /** * @brief Gets the current time from the GPS module. @@ -276,8 +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) @@ -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. */ }; 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); } /**