0

The basic example for ESP32 NTP is very rough:

void setup()
{
  Serial.begin(115200);
  
  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

But it tells nothing about the success of the operation. I need to know if it was able to update the time from the NTP server... is there a method to know that?

I can't rely on the time struct because I must init it manually at startup, say at 1st Jan 2020 00:00.

2
  • Out of curiosity: why is the time set manually at startup, when you will be updating it using NTP anyway? – ocrdu Nov 13 '20 at 11:12
  • Because if the NTP is not reachable (i.e. no WiFi connection) the system must begin anyway the operations. The user can set the default datetime value for this scenario. – Mark Nov 13 '20 at 13:50
1

Did you follow that manual?

https://lastminuteengineers.com/esp32-ntp-server-date-time-tutorial/

The printLocalTime(); function does all you ask for.

void printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

getLocalTimereturns false on failure. Let the printLocalTime do the same.

boolean printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return false;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
  return true;
}

Now the print function tells you if it failed (returns false) or not (returns true).

2
  • Please, look at the implementation of getLocalTime(). It just checks if the year if < 2016. But I said at power up I set the current time to a specific value (i.e. 1/1/2020). So I cannot rely on this. I need to know whether the NTP was ok or failed. – Mark Nov 13 '20 at 10:32
  • @Mark Sorry I completely misunderstood your problem. I also interpreted the code completely wrong. My answer does not fit to your question. I now have looked into the Arduino ESP code and probably there is a chance to find out if the time had formerly been set by a NTP Server call. But actually I have no time to evaluate that. I'll change my answer as soon as I could investigate a little bit. Hint: sntp_sync_status_t sntp_get_sync_status(void); in arduino-esp32/tools/sdk/include/lwip/sntp.h In any case the answer ocrdu gave is worth to try and should be the accepted answer if it works. – Peter Paul Kiefer Nov 13 '20 at 14:38
0

I'm not entirely sure there is a good solution for that with the normal ESP32 library, as it only checks to see if a valid year has been set, and the user has already set a valid year.

You could try using the NTPClient library or a similar one to get the time; its functions do a different check and actually return TRUE or FALSE. On success, you can then use the newly retrieved time value to update the ESP32's RTC.

3
  • I'm going to try this solution, hoping the ESP32 framework is compatible with this library. – Mark Nov 13 '20 at 13:52
  • @Mark: I had a quick look at the code, and looks like it is compatible with all the Arduino-esk WiFi libraries. No guarantees, though 8-). I wonder why somebody downvoted my answer. Oh well. Please accept the answer so the question doesn't remain open. If it works and you actually do accept the answer, of course. – ocrdu Nov 13 '20 at 13:59
  • 1
    I upvoted your answer. To accept one of the answers I'm going to test them before. – Mark Nov 13 '20 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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