I am trying to sync my esp32 to NTP server using configTime(); my code looks like this:
void startWiFi(){
int wifiTimeout = 180;
const char* ntpServer = "pool.ntp.org";
const long utcOffsetSeconds = 3600*utcOffset;
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setDrawColor(2);
WiFi.mode(WIFI_STA);
WiFiManager wm;
wm.resetSettings();
u8g2.setFont(u8g2_font_freedomSuperTiny_mf);
u8g2.setCursor(0, 15);
u8g2.print("STARTING WIFI...");
u8g2.sendBuffer();
wm.setConfigPortalTimeout(wifiTimeout);
u8g2.setCursor(0, 30);
u8g2.print("SELECT 'ENCOM DW 1.0'...");
u8g2.sendBuffer();
if (!wm.startConfigPortal("ENCOM DW 1.0")) {
u8g2.setCursor(0, 45);
u8g2.print("FAILED. WIFI TIMEOUT");
u8g2.sendBuffer();
delay(3000);
}
else {
u8g2.setCursor(0,45);
u8g2.print("CONNECTED: IP "+ WiFi.localIP().toString());
u8g2.sendBuffer();
configTime(utcOffsetSeconds, 3600*dstFlag, ntpServer);
// Wait until time is synchronized
while (!time(nullptr)) {
delay(1000);
}
}
//WiFi.disconnect(true);
//WiFi.mode(WIFI_OFF);
}
I am experiencing a strange behavior. The code actually exits this subroutine and continues to run and then some time later the time will actually update. Almost as if the configTime(); is running in the background. I thought the configTime was blocking. At any rate, it seems like it is not until like 2 or 3 seconds after it exits this subroutine that the time actually updates. Anybody got any ideas why this is? If so, is there a way for the code to "wait" until the time has updated?
The routine only sets up the SNTP configuration and starts a synchronization task. Once that got a reply from an NTP server it will set the time. For ways to wait until the synchronization is in effect you'll find ways in the ESP Time documentation.
Thanks @pylon. Here was my solution in case anybody else comes across this:
the configTime(); calls sntp_init(); which keeps updating the time at certain intervals (I believe default interval is 1 hour). However, you can use sntp_get_sync_status(); to see if the time has completed syncing. sntp_get_sync_status(); can be the following values:
SNTP_SYNC_STATUS_RESET (0): SNTP client is not initialized or has been reset.
SNTP_SYNC_STATUS_IDLE (1): SNTP client is idle and not actively synchronizing time.
SNTP_SYNC_STATUS_IN_PROGRESS (2): SNTP client is currently synchronizing time.
SNTP_SYNC_STATUS_COMPLETED (3): SNTP client has successfully synchronized time.
SNTP_SYNC_STATUS_FAILED (4): SNTP synchronization has failed.
so in my code
// WiFi Connect
void startWiFi(){
int wifiTimeout = 180;
const char* ntpServer = "pool.ntp.org";
const long utcOffsetSeconds = 3600*utcOffset;
sntp_sync_status_t syncStatus;
WiFi.mode(WIFI_STA);
WiFiManager wm;
wm.resetSettings();
wm.setConfigPortalTimeout(wifiTimeout);
if (!wm.startConfigPortal("ENCOM DW 1.0")) {
u8g2.setCursor(0, 45);
u8g2.print("FAILED. WIFI TIMEOUT");
u8g2.sendBuffer();
delay(3000);
}
else {
u8g2.setCursor(0,45);
u8g2.print("CONNECTED.");
u8g2.setCursor(0,60);
u8g2.print("IP "+ WiFi.localIP().toString());
u8g2.sendBuffer();
configTime(utcOffsetSeconds, 3600*dstFlag, ntpServer);
syncStatus = sntp_get_sync_status();
while (syncStatus != SNTP_SYNC_STATUS_COMPLETED) {
syncStatus = sntp_get_sync_status();
delay(100); // Adjust the delay time as per your requirements
}
sntp_stop(); //resets syncStatus to SNTP_SYNC_STATUS_RESET for the next time I initiate
}
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}