SensorsIot / NTPtimeESP

Small library for ESP8266 which returns the actual time adjusted by time zone and summer time
232 stars 65 forks source link

Incorrect dateTime retrieved and shown as valid too #25

Closed VishalDhayalan closed 5 years ago

VishalDhayalan commented 6 years ago

It often returns a dateTime that's incorrect by a huge amount (2036-2-7-5) and the time when that happens is expectedly incorrect as well. But what makes this error really problematic is it classes this incorrect dateTime as valid. It is always the same incorrect dateTime though (as shown below). image

This is the code:

#include <NTPtimeESP.h>

#define DEBUG_ON

NTPtime NTPch("ch.pool.ntp.org");   // Choose server pool as required
char *ssid      = "";               // Set you WiFi SSID
char *password  = "";               // Set you WiFi password

strDateTime dateTime;

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Booted");
  Serial.println("Connecting to Wi-Fi");

  WiFi.mode(WIFI_STA);
  WiFi.begin (ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("WiFi connected");
}

void loop() {
  // check dateTime.valid before using the returned time
  // Use "setSendInterval" or "setRecvTimeout" if required
  if(dateTime.valid){
    NTPch.printDateTime(dateTime);
    Serial.println(dateTime.valid);

    byte actualHour = dateTime.hour;
    byte actualMinute = dateTime.minute;
    byte actualsecond = dateTime.second;
    int actualyear = dateTime.year;
    byte actualMonth = dateTime.month;
    byte actualday =dateTime.day;
    byte actualdayofWeek = dateTime.dayofWeek;
  }
} 
TheFern2 commented 6 years ago

The way to get rid of this issue is to adjust your "setSendInterval" and "setRecvTimeout". I saw the same bleep, and that fixed my issue.

VishalDhayalan commented 6 years ago

I've just changed the pool server to time.google.com and it works without any problems. What does the setSendInterval and set RecvTimeout do?

TheFern2 commented 6 years ago

What is happening is that the default values are a bit too fast, and so if you are trying to request from the NTP server too fast it happens that you might get bad data and that's when you see the erroneous data.

And yeah you should also choose the closest server to you for best results.

larsss83 commented 5 years ago

Hi. I'm quite new at this. How would one use "setSendInterval" and "setRecvTimeout"?

TheFern2 commented 5 years ago

Hi. I'm quite new at this. How would one use "setSendInterval" and "setRecvTimeout"?

larsss83 if you open NTPtimeESP.h you can see those two are public methods.

class NTPtime {
  public:
    NTPtime(String NTPtime);
    strDateTime getNTPtime(float _timeZone, int _DayLightSaving);
    void printDateTime(strDateTime _dateTime);
    bool setSendInterval(unsigned long _sendInterval);  // in seconds
    bool setRecvTimeout(unsigned long _recvTimeout);    // in seconds

So if you set your NTP server like this: NTPtime NTPch("ch.pool.ntp.org");

You would call them as follows, the parameter is how fast you want the interval, and timeout. Set interval accordingly, until you see data OK.

NTPch.setSendInterval(1);
NTPch.setRecvTimeout(1);
larsss83 commented 5 years ago

Thanks!

rgry commented 5 years ago

I use dk.pool.ntp.org and got the same issue. Please fix valid() method

2019-1-24-5 11H 9M 1S 2019-1-24-5 11H 9M 2S 2019-1-24-5 11H 9M 3S 2019-1-24-5 11H 9M 4S 2036-2-7-5 7H 28M 16S 2036-2-7-5 7H 28M 16S 2036-2-7-5 7H 28M 16S 2036-2-7-5 7H 28M 16S 2036-2-7-5 7H 28M 16S 2036-2-7-5 7H 28M 16S 2019-1-24-5 11H 9M 58S 2019-1-24-5 11H 9M 59S 2019-1-24-5 11H 10M 0S 2019-1-24-5 11H 10M 1S 2019-1-24-5 11H 10M 2S 2019-1-24-5 11H 10M 3S

TheFern2 commented 5 years ago

rgry did you try to adjust values like suggested to Lars? Try something higher than 1. 1 is the default.

ghost commented 5 years ago

I am having the same problem. Increasing the send interval and the receive timeout had no effect for me...

TheFern2 commented 5 years ago

@SelfTenure Please post your code, and are you using the closest ntp pool?

ghost commented 5 years ago

@kodaman2 I guess, the problems I had with incorrect times, where temporay glitches – I cannot reproduce the invalid dates and times. Thank you anyways ;)