0

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) && counter <=2){
          Serial.print("NTP CHECK: #");
          Serial.println(counter);
          counter +=1;
          delay(1000);
        };
        NTP.setInterval(clockUpdateInt); // in seconds
}
6
  • which NTP library are you using? never get the expected message NTP CHECK: # and retry number - that suggests that that NTP.begin returns true Commented Mar 21, 2019 at 0:50
  • if you are using github.com/gmag11/NtpClient library, then the only reason begin returns false is if you pass in invalid arguments Commented Mar 21, 2019 at 0:56
  • @JaromandaX - I'm using NtpClientLib- and YES I know that result is true but still date is as noted...
    – guyd
    Commented Mar 21, 2019 at 4:34
  • the point is there's no point in running begin over and over again, since it only returns false if you pass in invalid arguments - as you are passing valid arguments, there's no need to check ... the way I would do it, in the code that relies on valid time, check NTP.getLastNTPSync() and skip the code if the result is 0 Commented Mar 21, 2019 at 4:38
  • But those are the arguments- which are not invalid
    – guyd
    Commented Mar 21, 2019 at 4:40

1 Answer 1

1

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() && counter <=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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.