2

I'm developing a small project, based on a ESP8266 module, to get date & time via an NTP server, since a RTC module wouldn't work for me. Iv'e tested a code where I get date & time using the NTPClient library in my circuit.

My system prints time just fine, it's accurate and synchronized with the timezone in my country. The problem comes regarding the date, because it gets inconsistent (and maybe random) data.

I mean, every time I load the code to my board, I get a different month, day and year, like if it was randomized so I can't just use an "offset" to set a date, because I don't know if the next time I load it it's gonna be the same number (it could print the current month is either january, september, may...).

Some examples from the Seril Monitor are these:

06:15:16.430 -> Epoch Time: 1640153824
06:15:16.430 -> Formatted Time: 06:17:04
06:15:16.430 -> Hour: 6
06:15:16.430 -> Minutes: 17
06:15:16.430 -> Seconds: 4
06:15:16.430 -> Week Day: Wednesday
06:15:16.430 -> Month day: 17
06:15:16.430 -> Month: 12
06:15:16.430 -> Month name: December
06:15:16.430 -> Year: 1339088
06:15:16.430 -> Current date: 1339088-12-17

BTW, I've tried changing the NTP server "link" to other than just "pool.ntp.org", to "us.pool.ntp.org", "fr.pool.ntp.org", among many others, but the result is the same...

So, I would like to know, what's wrong with my code regarding the date, and what may I do to improve it? Or if I could just change the NTP server...

This is my current code:

#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

// Replace with your network credentials
const char *ssid     = "mySSID";
const char *password = "mypass";

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

//Week Days
String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

//Month names
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  
  // Connect to Wi-Fi
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

// Initialize a NTPClient to get time
  timeClient.begin();
  // Set offset time in seconds to adjust for your timezone, for example:
  // GMT +1 = 3600
  // GMT +8 = 28800
  // GMT -1 = -3600
  // GMT 0 = 0
  timeClient.setTimeOffset(-14400);
}

void loop() {
  timeClient.update();

  unsigned long epochTime = timeClient.getEpochTime();
  Serial.print("Epoch Time: ");
  Serial.println(epochTime);
  
  String formattedTime = timeClient.getFormattedTime();
  Serial.print("Formatted Time: ");
  Serial.println(formattedTime);  

  int currentHour = timeClient.getHours();
  Serial.print("Hour: ");
  Serial.println(currentHour);  

  int currentMinute = timeClient.getMinutes();
  Serial.print("Minutes: ");
  Serial.println(currentMinute); 
   
  int currentSecond = timeClient.getSeconds();
  Serial.print("Seconds: ");
  Serial.println(currentSecond);  

  String weekDay = weekDays[timeClient.getDay()];
  Serial.print("Week Day: ");
  Serial.println(weekDay);    

  //Get a time structure
  struct tm *ptm = gmtime ((time_t *)&epochTime); 

  int monthDay = ptm->tm_mday;
  Serial.print("Month day: ");
  Serial.println(monthDay);

  int currentMonth = ptm->tm_mon+1;
  Serial.print("Month: ");
  Serial.println(currentMonth);

  String currentMonthName = months[currentMonth-1];
  Serial.print("Month name: ");
  Serial.println(currentMonthName);

  int currentYear = ptm->tm_year+1900;
  Serial.print("Year: ");
  Serial.println(currentYear);

  //Print complete date:
  String currentDate = (String) currentYear + "-" + (String) currentMonth + "-" + (String) monthDay;
  Serial.print("Current date: ");
  Serial.println(currentDate);
  Serial.println();
  delay(1000);
}
Yasin Br
  • 1,675
  • 1
  • 6
  • 16

1 Answers1

0

This is a bug in recent versions of the NTPClient library on the esp8266.

Downgrading to 2.7.4 of NTPClient appears to resolve the issue for some users.

See https://github.com/arduino-libraries/NTPClient/issues/149

Ben T
  • 4,656
  • 3
  • 22
  • 22