Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 15 additions & 8 deletions examples/ModemTest/ModemTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(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<uint8_t>(ntp_status));
} else {
Serial.printf("Time synchronization failed: %s\r\n", SIM7600::statusToString(status));
}
} break;

Expand All @@ -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,
Expand Down
11 changes: 2 additions & 9 deletions src/SIM7600Modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 4 additions & 6 deletions src/SIM7600Modem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down