Complete code for Dave's simple clock is https://github.com/G6EJD/ESP_Simple_Clock_Functions/blob/master/ESP_Simple_Clock.ino
Just select your time zone and put in your WiFi credentials and it prints exact time to the serial port.
The example gets the NTP time every second but getting the time that often is considered an abuse of the NTP servers. I only get the NTP time once a day and use it to set the RTC.
Six sections were added to the programs to get the date and time corrected for time zone and daylight or standard time. Internet time and date is used to set the DS3231 battery backed real time clock once a day at 3 a.m. Most NTP servers give UTC time only and you have to correct for time zone and daylight savings or standard time yourself. This code makes it easy.
All of my remote home automation controllers use web pages as the HMI instead of local displays.
Programs are also uploaded wirelessly as well.
This code parses the date and time string from the ntp server and breaks out the integers for date and time so they can be used for control.
initialize
//***********NTP time intialize 1 of 6********************************************************
#include "time.h"
const char* Timez; // EST USA corrects for DST
String Date_str, Time_str, Time_format, Year_str, Month_str, Day_str, Hour_str, Min_str, Sec_str;
int Year_integer, Month_integer, Day_integer, Hour_integer, Min_integer, Sec_integer;
//********** end NTP time intialize***********************************************************
bottom of setup
//********** NTP time setup 2 of 6***********************************************
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
setenv("TZ", Timezone, 1);
Time_format = "I"; // or StartTime("I");
//***********end NTP time setup***********************************************
in loop
only run once every second.
//*****Run following every second***********************************************
if(lastSecond != t.Second()) { lastSecond = t.Second();
//********** NTP time loop 3 of 6***********************************************
if ((t.Hour() == 3) && (t.Minute() == 0) && (t.Second() > 10) && (t.Second() < 30))
{ UpdateLocalTime(Time_format);
}
//***********end NTP time loop**************************************************
If also using RTC only update time and set RTC early in the morning. If you have many devices pick different times to update each of them. I had router issues updating every second at the same time on many devices.
//****Auto RTC time set from internet time 4 of 6************************************
if ((t.Hour() == 3) && (t.Minute() == 0) && (t.Second() == 30)) { RtcDateTime t = RtcDateTime(Year_integer, Month_integer, Day_integer, Hour_integer, Min_integer, Sec_integer); //set date and time hour, min, sec from internet Rtc.SetDateTime(t); //configure the RTC with object }
//****end Auto RTC time set from internet time**************************************
below data print on web page
//**************NTP time print time to web page 5 of 6*********************************
client.println(Date_str);
client.println(Time_str);
client.println(Hour_integer);
client.println(Min_integer);
client.println(Sec_integer);
//***************end NTP print time*****************************************
below program
//6 of 6 ###################################################################################
void UpdateLocalTime(String Format){
time_t now;
time(&now);
//See http://www.cplusplus.com/reference/ctime/strftime/
char hour_output[30], day_output[30];
strftime(day_output, 30, "%a %m-%d-%y", localtime(&now)); // Formats date as: Sat Jun-24-17
strftime(hour_output, 30, "%T", localtime(&now)); // Formats time as: 2:05:49pm
Date_str = day_output;
Time_str = hour_output;
//*** parse date and time string to get individual date and time integers to set RTC or use to turn devices on or off based on date and time.
strftime(day_output, 30, "%Y", localtime(&now));
Year_str = day_output;
Year_integer = Year_str.toInt();
strftime(day_output, 30, "%m", localtime(&now));
Month_str = day_output;
Month_integer = Month_str.toInt();
strftime(day_output, 30, "%e", localtime(&now));
Day_str = day_output;
Day_integer = Day_str.toInt();
strftime(hour_output, 30, "%H", localtime(&now));
Hour_str = hour_output;
Hour_integer = Hour_str.toInt();
strftime(hour_output, 30, "%M", localtime(&now));
Min_str = hour_output;
Min_integer = Min_str.toInt();
strftime(hour_output, 30, "%S", localtime(&now));
Sec_str = hour_output;
Sec_integer = Sec_str.toInt();
strftime(day_output, 30, "%a", localtime(&now));
Dow_str = day_output;
if (Dow_str == "Sun") Dow_integer = 0; // day of week integer calculated from Dow_str
if (Dow_str == "Mon") Dow_integer = 1;
if (Dow_str == "Tue") Dow_integer = 2;
if (Dow_str == "Wed") Dow_integer = 3;
if (Dow_str == "Thu") Dow_integer = 4;
if (Dow_str == "Fri") Dow_integer = 5;
if (Dow_str == "Sat") Dow_integer = 6;
}
//##################################################################################
I also display internet time and date from here:
https://free.timeanddate.com/clock/i5ry49b9/n4925/tlca/tt0/tw1/tm1/tb4
to compare to NodeMCU time and date on my HMI webpages served from the NodeMCU. Example is in the gallery.