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);
}
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.
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.
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 ...).
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.