I want to synchronize time with NTP server once in some time. I am using DOIT ESP32 DEVKIT V1, I tried with getLocalTime() function but it returned true
even without wifi, but as I know without wifi it can not conect to NTP server. Can anyone say if it is possible how to correctly synchronize time with NTP server? Maybe with some other libraries or with other functions.
#include <WiFi.h>
#include "time.h"
const int gmtOffset_sec = 7200;
const int daylightOffset_sec = 3600;
const char* ntpServer = "pool.ntp.org";
void setup() {
Serial.begin(115200);
Serial.println("Board started\n");
WiFi.begin("<ssid>", "<password>");
struct tm timeinfo;
Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time 1");
} else {
Serial.println("Time obtained 1");
}
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time 2");
} else {
Serial.println("Time obtained 2");
}
}
void loop() {
}
The output to console is:
Board started
Connecting....
WiFi connected.
Time obtained 1
Time obtained 2
But I would expect 2nd output to be Failed to obtain time 2
.
```c++
before the code, and```
after it.