NTPClient Local USA Eastcoast time - SOLVED

hello, I'm working ona serial monitor clock for my local time. I'm using the NTPClient to be the clock part. I don't have a RTC module of any kind. I'm having a problem trying to get the local time. So I searched online for the ntp address for usa east cost time. I have found many websites but no matter which one I have tried I can not get the correct time and date. It is eather 3 or 4 hours ahead or a different day alltogether. I'm not sure what to do to get a local time and date for the USA eastcost Maryland time. Can someone please help me out? The sketch I'm using below is the example one that came with the library.

#include <NTPClient.h>
// change next line to use with another board/shield
#include <ESP8266WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>

const char *ssid     = "********";
const char *password = "********";

WiFiUDP ntpUDP;

// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionally you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "us.pool.ntp.org", 3600, 60000);

void setup(){
  Serial.begin(115200);

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    
    Serial.print ( "." );
  }

  timeClient.begin();
}

void loop() {
  timeClient.update();

  Serial.println(timeClient.getFormattedTime());

  delay(1000);
}

Joseph

What's the time there now? I'll have a play around.

From the NTPClient library...

    NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);

Looks like you are setting the offset to 1 hour (+3600)... is that what you want?

I would expect -5 hours... -18,000.

Hello time here is 7:24pm and it shows on serial monitor 00:24:10

Did you try an offset of -18,000 ? or maybe -14,400 (4 hours). GMT is currently 23:32 (Sat 3 Sep).

I did that and got this error.

sketch_sep03a:16:62: error: no matching function for call to 'NTPClient::NTPClient(WiFiUDP&, const char [16], int, int, int)'
16 | NTPClient timeClient(ntpUDP, "us.pool.ntp.org", 3600, -18,000);

You are putting the offset in the wrong parameter... the offset is the 3rd parameter. The 4th is the update frequency (which is unsigned). Also omit the commas from the numbers.

NTPClient timeClient(ntpUDP, "us.pool.ntp.org", -18000, 3600);

Okay now it the time is 7:43pm and it is showing 18:43:54.

Try -14400

Hello, That works perfectly. I can see the correct time now. My last thing is trying to figure out how to show the month, day and year next. That is my goal. thank you for everything.

Hello, I saw a way to get the day of the week. But I can not see a way to get the month or year is that possible?

The library does not come with functions to get the day, month, or year. However, you can work these out by using the epoch time. The epoch time is the number of seconds since 00:00:00 1/1/1970.
unsigned long epochTime = timeClient.getEpochTime();

then create a time structure using this...
struct tm *ptm = gmtime ((time_t *)&epochTime);

Then you have access to the following variables...

  • tm_sec: seconds after the minute;
  • tm_min: minutes after the hour;
  • tm_hour: hours since midnight;
  • tm_mday: day of the month;
  • tm_mon: month of the year (0-11);
  • tm_year: years since 1900;
int monthDay = ptm->tm_mday;
int currentMonth = ptm->tm_mon+1;  // Add 1 because months are numbered 0-11
int currentYear = ptm->tm_year+1900; // Add 1900, as years start at 1900.

you don't need an external library for NTP on an ESP8266. Everything is already implemented in the core and basically you just add a one liner to your user sketch (well, there are 3 lines ...).

See here: NTP for the ESP8266 including day light saving Time (DST) without 3rd party library

Thank you very much looking at it now.

you still use the NTPClient library on an esp8266?

Hey juraj, Yes I'm using the NTPClinet library.

Hello, I'm trying that code But one thing I don't understand is this line.

#define MY_TZ "CET-1CEST,M3.5.0/02,M10.5.0/03"

what shows up in serial monitor is.

year:2022 month:9 day:5 hour:6 min:33 sec:51 wday1 DST

the year is correct the month is correct the hour is not correct the minutes are correct.

my time is 12:34am after midnight and this shows 6:33.

Just an update. I searched online and searched for CET-1CEST,M3.5.0/02,M10.5.0/03 i found this site https://sites.google.com/a/usapiens.com/opnode/time-zones I saw USA east coast time
Eastern Time EST5EDT,M3.2.0,M11.1.0 I put that in and now it shows correct time. Everything else works great. Thank you.

like the URI beside the line says ... :wink:
CENTRAL EASTERN TIME - good old Europe.
And here it is time to have breakfast now ...

My breakfest starts at 5am. my sleep is always 3am normally. But not right now this thing got me messed up a little lol

As they say I lost track of time.