ESP8266 NTP error Year 1970

Can anyone tell me the correct working version of the board manager 3.1.2 version and ntp client version?

I am using NTPclient 3.2.1

/*
Rui Santos
Complete project details at ESP8266 NodeMCU NTP Client-Server: Get Date and Time (Arduino IDE) | Random Nerd Tutorials

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/

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

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

// 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(0);
}

void loop() {
timeClient.update();

time_t 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(2000);
}

+++++++++++++++++++++++++++++++++++++++++++Epoch Time: 1023
Formatted Time: 00:17:03
Hour: 0
Minutes: 17
Seconds: 3
Week Day: Thursday
Month day: 1
Month: 1
Month name: January
Year: 1970
Current date: 1970-1-1

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

1 Like

this is NOT how you should do NTP on an ESP8266.

NTP comes with the ESP Core and does not need external libraries any more.

Please see my page:
https://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm

1 Like

I cannot read your gibberish, take a few and do what you should have already done and read the forum guidelines. UKHeliBob posted a link for that. If I am not asleep, 1970 is the base year for NTP. With that it appears your code is not processing the correct data.

The Epoch for NTP in January 1, 1900. It's a 32-bit unsigned number as opposed to Unix Time which is a 32-bit signed number with an Epoch of Jan 1, 1970.

1 Like

Sorry I was apparently asleep, thanks for the catch.

This is the output I am getting while using the above link.
11:25:26.921 -> year:1970 month:1 day:1 hour:1 min:10 sec:8 wday4 standard

thats after start.
It will take up to one minute before the first NTP call.
Let it run.

07:13:26.432 -> NTP TZ DST - bare minimum
07:13:26.727 -> .................
07:13:30.643 -> WiFi connected
07:13:30.643 -> year:1970	month:1	day:1	hour:1	min:0	sec:4	wday4	standard
07:13:31.650 -> year:2024	month:2	day:6	hour:7	min:13	sec:12	wday2	standard
07:13:32.658 -> year:2024	month:2	day:6	hour:7	min:13	sec:13	wday2	standard
07:13:33.650 -> year:2024	month:2	day:6	hour:7	min:13	sec:14	wday2	standard

P.S.: obviously it will only work when the ESP8266 has an working internet connection.

11:46:43.508 -> year:1970 month:1 day:1 hour:1 min:31 sec:24 wday4 standard
11:46:44.525 -> year:1970 month:1 day:1 hour:1 min:31 sec:25 wday4 standard
11:46:45.531 -> year:1970 month:1 day:1 hour:1 min:31 sec:26 wday4 standard
11:46:46.536 -> year:1970 month:1 day:1 hour:1 min:31 sec:27 wday4 standard
11:46:47.540 -> year:1970 month:1 day:1 hour:1 min:31 sec:28 wday4 standard
11:46:48.542 -> year:1970 month:1 day:1 hour:1 min:31 sec:29 wday4 standard
11:46:49.548 -> year:1970 month:1 day:1 hour:1 min:31 sec:30 wday4 standard
11:46:50.553 -> year:1970 month:1 day:1 hour:1 min:31 sec:31 wday4 standard
11:46:51.532 -> year:1970 month:1 day:1 hour:1 min:31 sec:32 wday4 standard
11:46:52.550 -> year:1970 month:1 day:1 hour:1 min:31 sec:33 wday4 standard
11:46:53.533 -> year:1970 month:1 day:1 hour:1 min:31 sec:34 wday4 standard

Last 10 minutes its showing the same.

I've used exact the sketch from my link - changed only the credentials - it works on my side.

It seems your ESP8266 doesn't get internet access.

Check with the Example ESP8266HTTPClient / BasicHTTPClient if the ESP can access the internet.

ESP8266 core 2.7.4 and 3.0.2

Core 2.7.4 and 3.0.2 wifi are not connecting.

12:09:39.541 -> .......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................{ldܞ��$�| �l�b|���e�{���b��oN�dno���bp�cd{lp�N� � d ��  cn�|��� c��on�$��l �eno�{ln{���o r{�p�n� {��ܜ�� B o�|��"��no� �l�eno�{$Ns���� rl r��o red � �����$��o�l
12:11:39.934 -> NTP TZ DST - bare minimum
12:11:40.118 -> ......................................................................

it seems your credentials are not correct

you should adopt these lines to your wifi

#define STASSID "your-ssid"                            // set your SSID
#define STAPSK  "your-password"                        // set your wifi password

I kept the code intact, Changed the board library versions, and programmed the board.

This basic sketch adds callback, which is explained in that rothschopf link.
It won't display the time until the first valid NTP string is received.

You need to change three or four things in this sketch.

  1. your timezone (web link for that is in the sketch)
  2. closest pool in your country (optional, but recommended)
  3. your network SSID
  4. and passwork

Leo..

#include <ESP8266WiFi.h>
#include <coredecls.h>
unsigned long prevTime;
time_t now;
tm tm;
bool ntp;

void time_is_set() {
  Serial.println(F("time was updated"));
  ntp = true;
}

void setup() {
  Serial.begin(74880);
  Serial.println(F("\nESP8266 NTP clock"));
  configTime("your-timezone", "pool.ntp.org"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  settimeofday_cb(time_is_set);
  WiFi.mode(WIFI_STA);
  WiFi.begin("your-ssid", "your-password");
  while (WiFi.status() != WL_CONNECTED) delay(200);
  while (!ntp); // wait here for timesync
}

void loop() {
  time(&now);
  localtime_r(&now, &tm);
  if (now != prevTime) { // only print when time has changed
    prevTime = now;
    printf("%02u:%02u:%02u\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
  }
}
1 Like

Dear Noiasca,

My ESP8266 is connecting to the internet I can see the correct output with the third-party library after a few minutes. I am getting the correct time.
Any idea where could be the mistake?

Where on your code running continuously but not getting even after a few minutes?
If I change the Board library to lower versions its not connecting to the WIFI.

Thanks and regards
Indrajit

As @noiasca said, you should not use this any more
#include <NTPClient.h>
#include <WiFiUdp.h>
this is the old way.

Did you try my code. It gets the correct time in a matter of seconds.
And it auto-adjust for daylight savings.

You can of course change the way time is printed.

Where do you live (country, region).
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.