arduino-libraries / NTPClient

Connect to a NTP server
533 stars 366 forks source link

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

Open allene222 opened 2 years ago

allene222 commented 2 years ago

I have NTPClient on a remote ESP32 monitor that checks the time once a day normally. It has been running fine since late 2020.

A few day ago timeClient.update() and timeClient.forceUpdate() quit working. 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.

allene222 commented 2 years ago

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 commented 2 years ago

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?

viktak commented 1 year ago

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.

manunited10 commented 1 year ago

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).

grenelt commented 1 year ago

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...

Ales-Svoboda commented 1 year ago

For me it is working just fine.

allene222 commented 1 year ago

@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.

Ales-Svoboda commented 1 year ago

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.

CcKefa commented 2 months ago

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.

allene222 commented 2 months ago

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.