From d46b2bfabaccb9c886081926033f54b8a64bd015 Mon Sep 17 00:00:00 2001 From: alkonosst Date: Sat, 22 Nov 2025 23:19:14 -0300 Subject: [PATCH] fix: Remove NTP sync from setNTPServer to separate responsabilities --- README.md | 10 ++++++++-- examples/ModemTest/ModemTest.ino | 23 +++++++++++++++-------- src/SIM7600Modem.cpp | 11 ++--------- src/SIM7600Modem.h | 10 ++++------ 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index f57bad3..4ae56db 100644 --- a/README.md +++ b/README.md @@ -504,9 +504,15 @@ Modem modem(&Serial1); // Use Serial1 for communication void setup() { // ... Initialize modem as shown in the previous example - // Synchronize time with NTP server + // Set NTP server, UTC time zone = 0 + Status status = modem.setNTPServer("pool.ntp.org", 0); + if (status != Status::Success) { + // Handle error + } + + // Synchronize time NTPSyncStatus ntp_status; - Status status = modem.setNTPServer(ntp_status, "pool.ntp.org"); + status = modem.synchronizeTime(ntp_status); if (status != Status::Success) { // Handle error } diff --git a/examples/ModemTest/ModemTest.ino b/examples/ModemTest/ModemTest.ino index 9cdbd55..5d35408 100644 --- a/examples/ModemTest/ModemTest.ino +++ b/examples/ModemTest/ModemTest.ino @@ -243,14 +243,21 @@ void handleSerial() { case '5': { - SIM7600::NTPSyncStatus ntp_status; - status = modem.setNTPServer(ntp_status, "pool.ntp.org", 0); + status = modem.setNTPServer("pool.ntp.org", 0); if (status == SIM7600::Status::Success) { - Serial.printf("NTP server set: %u\r\n", static_cast(ntp_status)); + Serial.printf("NTP server set successfully\r\n"); } else { - Serial.printf("NTP server setup or time synchronization failed: %s\r\n", - SIM7600::statusToString(status)); + Serial.printf("NTP server setup failed: %s\r\n", SIM7600::statusToString(status)); + } + + SIM7600::NTPSyncStatus ntp_status; + status = modem.synchronizeTime(ntp_status); + if (status == SIM7600::Status::Success) { + Serial.printf("Time synchronized successfully, NTP Status: %u\r\n", + static_cast(ntp_status)); + } else { + Serial.printf("Time synchronization failed: %s\r\n", SIM7600::statusToString(status)); } } break; @@ -261,10 +268,10 @@ void handleSerial() { status = modem.getNetworkTime(time_data); if (status == SIM7600::Status::Success) { - Serial.printf("Network time: %02u-%02u-%04u %02u:%02u:%02u TZ: %d\r\n", - time_data.day, - time_data.month, + Serial.printf("Network time: %04u-%02u-%02u %02u:%02u:%02u TZ: %d\r\n", time_data.year, + time_data.month, + time_data.day, time_data.hour, time_data.minute, time_data.second, diff --git a/src/SIM7600Modem.cpp b/src/SIM7600Modem.cpp index 18787ed..d545c3f 100644 --- a/src/SIM7600Modem.cpp +++ b/src/SIM7600Modem.cpp @@ -842,21 +842,14 @@ Status Modem::configureAPN(const char* apn, const char* user, const char* passwo return Status::Success; } -Status Modem::setNTPServer(NTPSyncStatus& ntp_status, const char* ntp_server, - const int8_t time_zone, const uint32_t timeout_ms) { +Status Modem::setNTPServer(const char* ntp_server, const int8_t time_zone) { SIM7600_LOGD(tag, "Setting NTP server: %s, time zone: %d", ntp_server, time_zone); - ntp_status = NTPSyncStatus::UnknownError; - // Configure NTP server Status status = sendATCmd("AT+CNTP=\"%s\",%d", ntp_server, time_zone); if (status != Status::Success) return status; - status = waitForResponse(AT_OK, 10000); - if (status != Status::Success) return status; - - // Sync time - return synchronizeTime(ntp_status, timeout_ms); + return waitForResponse(AT_OK); } Status Modem::synchronizeTime(NTPSyncStatus& ntp_status, const uint32_t timeout_ms) { diff --git a/src/SIM7600Modem.h b/src/SIM7600Modem.h index 9a66e87..f0b448c 100644 --- a/src/SIM7600Modem.h +++ b/src/SIM7600Modem.h @@ -444,18 +444,16 @@ class Modem { /* ---------------------------------------- NTP methods --------------------------------------- */ /** - * @brief Set the NTP server and synchronize time. - * @param ntp_status Reference to store the NTPSyncStatus enum value. + * @brief Set the NTP server to synchronize time. * @param ntp_server NTP server address. * @param time_zone Time zone offset from UTC in hours. - * @param timeout_ms Timeout in milliseconds to wait for synchronization. * @return Status::Success on success, error code otherwise. */ - Status setNTPServer(NTPSyncStatus& ntp_status, const char* ntp_server, const int8_t time_zone = 0, - const uint32_t timeout_ms = SIM7600_MODEM_NTP_SYNC_TIMEOUT_MS); + Status setNTPServer(const char* ntp_server, const int8_t time_zone = 0); /** - * @brief Synchronize time with the configured NTP server. + * @brief Synchronize time with the configured NTP server. You must set the NTP server. At least + * one successful synchronization is required to get the network time. * @param ntp_status Reference to store the NTPSyncStatus enum value. * @param timeout_ms Timeout in milliseconds to wait for synchronization. * @return Status::Success on success, error code otherwise.