Verifiy NTP server update - at start I have function to start an NTP service, right after obtaining wifi connection ( using ESP8266 board ).

from time to time, I get an error saying date and time is01-01-1970... which I assume is due to not reaching the server as intended.

A retry counter was added to give few more retries, but somehow I does not do the trick since I never get the expected message NTP CHECK: # and retry number ( while get an invalid date ).
I know that NTP.begin() returns a bool to describe success/ failure obtaining a NTP server

EDIT1: using NtpClientLib

How can it be done ?


void startNTP() {
        int counter = 0;
        while ( !NTP.begin("pool.ntp.org", 2, true) &amp;&amp; counter &lt;=2){
          Serial.print("NTP CHECK: #");
          Serial.println(counter);
          counter +=1;
          delay(1000);
        };
        NTP.setInterval(clockUpdateInt); // in seconds
}
Having read the NTPClientLib code, I would say that the first sync is not even attempted for 1 second after NTP.begin() is called anyway.


You could do this

void startNTP() {
    NTP.begin("pool.ntp.org", 2, true);
    delay(2000); // there seems to be a 1 second delay before first sync will be attempted, delay 2 seconds allows request to be made and received
    int counter = 2;
    while ( !NTP.getLastNtpSync() &amp;&amp; counter &lt;=2) {
        Serial.print("NTP CHECK: #");
        Serial.println(counter);
        counter +=1;
        delay(1000);
    };
    NTP.setInterval(clockUpdateInt); // in seconds
}


or, you could do this

void loop() {
    // code that doesn't rely on valid time
    //
    // lets say this bit of code requires sync'd time
    if (NTP.getLastNtpSync()) {
        // this will only run once NTP has been sync'd at least once
    }
    // more code that doesn't require sync'd time
}


Now, you may want to check how long time hasn't been sync'd for in your code and do something if it's been too long