Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
allene222 opened this issue Apr 9, 2022 · 11 comments
Open

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

allene222 opened this issue Apr 9, 2022 · 11 comments
Labels
type: imperfection Perceived defect in any part of project

Comments

@allene222
Copy link

allene222 commented Apr 9, 2022

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

@per1234 per1234 added the type: imperfection Perceived defect in any part of project label Apr 9, 2022
@allene222

This comment was marked as duplicate.

@allene222

This comment was marked as off-topic.

@viktak

This comment was marked as duplicate.

@manunited10
Copy link

manunited10 commented Jul 25, 2022

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
Copy link

grenelt commented Aug 17, 2022

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
Copy link

For me it is working just fine.

@allene222
Copy link
Author

@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
Copy link

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.

@per1234 per1234 changed the title NTPClient on ESP32 quit working NTPClient::update() now always returns false May 26, 2023
@CcKefa
Copy link

CcKefa commented Apr 19, 2024

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
Copy link
Author

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.

@step135

This comment was marked as duplicate.

@arduino-libraries arduino-libraries locked as too heated and limited conversation to collaborators Sep 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

8 participants