Comments on: Get Epoch/Unix Time with the ESP8266 NodeMCU (Arduino) https://randomnerdtutorials.com/epoch-unix-time-esp8266-nodemcu-arduino/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Thu, 11 Mar 2021 10:38:02 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: Sara Santos https://randomnerdtutorials.com/epoch-unix-time-esp8266-nodemcu-arduino/#comment-569365 Thu, 11 Mar 2021 10:38:02 +0000 https://randomnerdtutorials.com/?p=101600#comment-569365 In reply to DDTech.

Thanks for sharing.
Regards,
Sara

]]>
By: DDTech https://randomnerdtutorials.com/epoch-unix-time-esp8266-nodemcu-arduino/#comment-569322 Thu, 11 Mar 2021 08:45:49 +0000 https://randomnerdtutorials.com/?p=101600#comment-569322 When I implemented ntp updates in my esp8266 projects about two years ago, I realized that dns resolve to the time servers sometimes takes a good amount of time.
So I got the IP-address on the first call and used it directly on subsequent calls. That made a huge difference.

Unfortunately most of the time servers seem to be available only temporarily and the IP address becomes invalid from time to time and I had to implement a mechanism that detects that and then requests a new address.

Another issue is that requests tend to fail quite often, probably due to high trafic. You might have realized that on Your windows pc, when You try to manually force an update of the time by a time server. Sometimes You have to click several time or change to a different server until it reports success.

I then, more or less accidentally, realized that my router – typically the gateway address – also provides ntp services by default. Since that day I am updating my time with that “server”. It’s blazing fast, almost a 100% reliable and more than good enough for all my projects.

I use the “time” – lib by Paul Stoffregen(https://github.com/PaulStoffregen/Time) together with my ntp implementation. This makes handling with time data very easy and takes care of regular ntp updates.

Hope this is of help to anyone.

Regards

Frank

]]>
By: Jan https://randomnerdtutorials.com/epoch-unix-time-esp8266-nodemcu-arduino/#comment-550959 Sat, 06 Feb 2021 14:55:44 +0000 https://randomnerdtutorials.com/?p=101600#comment-550959 In reply to ed.

if (actualMonth > 2 && actualyear %4==0 && actualyear %100 !=0)
{d=d+1;}
return d;

To complete it, it should be :
if (actualMonth > 2 && ((actualyear %4==0 && actualyear %100 !=0) || (actualyear %400==0)))
{d=d+1;}
return d;

]]>
By: ed https://randomnerdtutorials.com/epoch-unix-time-esp8266-nodemcu-arduino/#comment-548992 Wed, 03 Feb 2021 00:03:50 +0000 https://randomnerdtutorials.com/?p=101600#comment-548992 Forgot one thing: I use this function to calculte the ‘day of year’

int doy()
{
d=actualday;//actualday
//Serial.print(d);
for (int i=0;i<actualMonth-1;i++){
d=d+daysInMonth[i];
//Serial.println(d);
}
if (actualMonth > 2 && actualyear %4==0 && actualyear %100 !=0)
{d=d+1;}
return d;
}

That can come in handy if you want to start or and an action on a specific date. No need to check on day and month but just on say ‘day 200’

]]>
By: ed https://randomnerdtutorials.com/epoch-unix-time-esp8266-nodemcu-arduino/#comment-548991 Tue, 02 Feb 2021 23:56:01 +0000 https://randomnerdtutorials.com/?p=101600#comment-548991 Stefan,I do that too. But I use the time server to set the internal clock of the esp. Like this
In declarations:
#include <SNTPtime.h>
SNTPtime NTPch(“nl.pool.ntp.org”);
strDateTime dateTime;
//for timeserver
byte actualHour;
byte actualMinute;
byte actualsecond;
int actualyear;
byte actualMonth;
byte actualday;
byte actualdayofWeek;
//int dayofyear;
int d;
uint16_t actualdayofYear;//actualdayofYear
const uint8_t daysInMonth [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

String maandNaam [] ={“Jan”, “Feb”, “Mrt”, “Apr”, “Mei”, “Juni”, “Juli”,”Aug”,”Sep”,”Okt”,”Nov”,”Dec”};

In Setup()
NTPch.setSNTPtime()

in loop()
//dateTime = NTPch.getNTPtime(1.0, 1); //from NTP server use this if you want ESP to continuously fetch new time
dateTime = NTPch.getTime(1.0, 1); // get time from internal clock
if (dateTime.valid) {
//NTPch.printDateTime(dateTime);
actualHour = dateTime.hour;
actualMinute = dateTime.minute;
actualsecond = dateTime.second;
actualyear = dateTime.year;
actualMonth = dateTime.month;
actualday = dateTime.day;
actualdayofWeek = dateTime.dayofWeek;
}

]]>
By: Said https://randomnerdtutorials.com/epoch-unix-time-esp8266-nodemcu-arduino/#comment-548990 Tue, 02 Feb 2021 23:54:32 +0000 https://randomnerdtutorials.com/?p=101600#comment-548990 Hi Sara & Rui,
Thank you for your tutorials.
I tried the code on a wemos d1 mini. It did not work. It worked when I replaced timeClient.update() by :
while(!timeClient.update()) {
timeClient.forceUpdate();
}
BR
Said

]]>
By: Stefan Ludwig https://randomnerdtutorials.com/epoch-unix-time-esp8266-nodemcu-arduino/#comment-548788 Tue, 02 Feb 2021 12:32:56 +0000 https://randomnerdtutorials.com/?p=101600#comment-548788 Hi Sara and Rui,

epoc-time does contain all the information so that it is possible to calculate
the actual date and time.
Therefore I discovered a pretty small demo-code written by Werner Rotschopf
https://werner.rothschopf.net/201802_arduino_esp8266_ntp.htm

It uses the time.h-library and another structure that makes it easier to access day. month, year, hour and a number of day of week etc.

it inkludes cofiguration of the timezome through a define

and as an additional hint:
if you want to use a if-condition like switch something on from 7:54 to 21:19
caclulating minute of day makes life easier

actualTime = hour * 60 + minute

SwitchOnTime = 7 * 60 + 54 = 474
SwitchOFFTime = 21*60 + 19 = 1279

so a otherwise complicated if-condition about hours and minutes boils down to

if (actualTime > SwitchOnTime && actualTime < SwitchOFFTime) {
//take action;
}

best regards Stefan

]]>