NTPClient icon indicating copy to clipboard operation
NTPClient copied to clipboard

NTPClient::update() now always returns `false`

Open allene222 opened this issue 2 years ago • 11 comments

I have NTPClient on a remote ESP32 monitor that checks the time once a day normally. If it fails, it tries again in half an hour. It has been running fine since late 2020.

A few day ago timeClient.update() and timeClient.forceUpdate() quit working. It has been trying and failing every half hour and sending me an email that it failed 48 times a day for several days.

I ran it at home on a different ESP and confirmed that it does not work and that it isn't just the remote environment.

My code is based on the randomnerdtutorials code which seems pretty standard. I added a timeout giving 20 tries. It never completes. At home I gave it infinite tries and it never is successful.

  int counter = 0;
  while(!timeClient.update() && counter < 20) {
    timeClient.forceUpdate();
    delay(500);
    counter++;
    Serial.print("+");
  }

I know this code is good because it worked for over a year.

Additional context

Additional reports

  • https://github.com/arduino-libraries/NTPClient/issues/172#issuecomment-1190472769

allene222 avatar Apr 09 '22 16:04 allene222

I said once a day normally. If it fails, it tries again in half an hour. It has been trying and failing every half hour and sending me an email that it failed 48 times a day for several days.

allene222 avatar Apr 10 '22 11:04 allene222

I have successfully replaced NTPClient in my app. Works perfectly (the replacement not NPTClient). But I am still curious. Is NTPClient working for anyone on an ESP32? And what changed?

allene222 avatar Apr 21 '22 18:04 allene222

It doesn't seem to work for me either. I have used this lib on ESP8266 with success, but I can't get it to work on ESP32.

viktak avatar Jul 20 '22 16:07 viktak

I've recently started using this repo on ESP32. So it's supposed to get the date/time from NTP server and show on an LED dot matrix (like a message board). However after a couple of hours the message board gets super slow. It took me a good amount of time to figure out the problem is - potentially - this repo (initially I was mostly looking at hardware/power failure). It works initially and gets time and date, but after a couple of hours it gets slow. I set the update interval to every 12 hours. But as of now for the 2nd update it gets slow. I added this around the timeClient.update() to be sure:

  // this is part of loop():
  timeClient_millis = millis();
  timeClient.update(); 
  timeClient_millis = millis() - timeClient_millis;

and show this timeClient_millis on the message board periodically. It's zero on the first twelve hours and then gets e.g. 1021 which means the main loop is iterating every one second! I skimmed the source code, and it all makes sense. It looks like for some reason the forceUpdate() function always goes on timeout for some reason. It could be some networking issue from my side. I'm not sure yet. Anyway, if I soft/hard reset the micro, it quickly gets the time and date and works for next couple of hours. I guess I need to take a deeper look at the code or find another easy way to get time and date (for example get it through mqtt).

manunited10 avatar Jul 25 '22 12:07 manunited10

Also not working for me...

But as i have also serious issues with painless mesh (doesn't work at all) i believe the problems are in the esp-core-package...

grenelt avatar Aug 17 '22 08:08 grenelt

For me it is working just fine.

Ales-Svoboda avatar Aug 17 '22 16:08 Ales-Svoboda

@Ales-Svoboda Care to share the code you are using? For me, it worked fine for more than a year 24/7 in a remote application and then one day it just quit working. I would love to see what you are doing that works and how it differs from the code I posted.

I have to assume that the NTP server changed something and that caused issues with what I was doing. There seem to be several people having issues so it would help them. Maybe it works now, I quit using it so don't really know.

allene222 avatar Aug 17 '22 17:08 allene222

No problem, here you go:

#include <Arduino.h>
#include <M5Stack.h>
#include <WiFi.h>
#include <NTPClient.h> // NTPClient@^3.2.1
#include "utility/M5Timer.h"
#include "Free_Fonts.h"

#define T_REFRESH       60000 // ms
#define T_SLEEP         120000 // ms

const char* ssid                = "*****";
const char* password            = "*****";
const long utcOffsetInSeconds   = 7200;

WiFiUDP ntp;
NTPClient timeClient(ntp, "pool.ntp.org", utcOffsetInSeconds);
M5Timer timer;
M5Timer timerSleep;
int monthDay;
int currentMonth;
int weekDay;
unsigned long epochTime;

void refreshScreen(void);

void setup() {
    M5.begin();  
    M5.Power.begin();
    WiFi.begin(ssid, password);  

    while (WiFi.status() != WL_CONNECTED) { 
        delay(1000);
        M5.lcd.print(".");
    }

    timeClient.begin();

    timer.setInterval(T_REFRESH, refreshScreen);
    timerSleep.setInterval(T_SLEEP,esp_light_sleep_start);

    delay(10);
    refreshScreen();
}

void loop() {                  
    M5.update();
    if (M5.BtnA.pressedFor(500)) {     
        M5.Power.powerOFF(); // power on with restart button
    }
    timer.run(); 
}

void refreshScreen(void) {
    timeClient.update();
    // check data validity
    epochTime = timeClient.getEpochTime();

    if ( timeClient.getDay()  == 0 ) weekDay = 7; 
    else weekDay = timeClient.getDay();

    Serial.print("Time: "); Serial.println(timeClient.getFormattedTime());
    Serial.print("WeekDay: "); Serial.println(weekDay);
    Serial.print("EpochTime: "); Serial.println(epochTime);
}

This code runs on M5Stack Core with ESP32.

Ales-Svoboda avatar Aug 17 '22 19:08 Ales-Svoboda

Anyone found a solution to this? I've tried the example code and the code above by @Ales-Svoboda and can't get it to work. It's really odd since a couple days ago it was working.

CcKefa avatar Apr 19 '24 16:04 CcKefa

I gave up on HTPClient. I built a webpage in php that gets time(), does the calculations I need, and I just call that page to get what I need. This has the advantage of dealing with daylight savings time. Much better for my application.

allene222 avatar Apr 19 '24 17:04 allene222