NTP time acquisition fails about 30% of the time
domschl opened this issue · 1 comments
domschl commented
Retries are necessary, some async version of:
uint8_t time_retry=0; // Counter retry counts time server
setenv("TZ", "CET-1CEST,M3.5.0/02,M10.5.0/03", 1);
struct tm initial; // temp struct for checking if year==1970
initial.tm_year=70;
while (initial.tm_year == 70 && time_retry < 15) {
configTime(0, 0, "pool.ntp.org"); // get time from NTP server (ESP8266)
delay(500);
time_t now = time(&now);
localtime_r(&now, &initial);
Serial.print("Time Server connection attempt: ");
Serial.println(time_retry + 1);
Serial.print("current year: ");
Serial.println(1900 + initial.tm_year);
time_retry++;
}
if (time_retry >=15) {
Serial.println("Connection to time server failed");
} else {
time_t now = time(&now);
localtime_r(&now, &tm);
strftime (timeshow, sizeof(timeshow), "%H:%M", &tm);
Serial.print("Successfully requested current time from server: ");
Serial.println(timeshow);
}
domschl commented
Root cause is an unresolved issue in arduino/esp8266: esp8266/Arduino#7056
The current configTime API assumes that the hostname parameters remain valid and keeps referencing those pointers,
which went out of scope in our old implementation.
Fixed by making vars persistent as workaround.