NodeMCU Internet Clock
Internet Clock
From: http://melissamerritt.epizy.com/internet-clock/internet_clock.html?i=1


Example 1 Example 2


This little project with the NodeMCU and a TM1637 4-Bits Digital LED connects 
to at NTP Server
This is a 12hr clock with Daylight Savings and None Daylight Savings Sketches.
PARTS
1 x WeMos D1 USB NodeMcu Lua V3 CH340G ESP8266 Wireless Internet Development 5-9V
or
1 x NodeMCU Lua ESP8266 ESP-12 WeMos D1 Mini WIFI
1 x TM1637 4-Bits Digital LED Display
1 x 85x58x33mm Waterproof Clear Cover Plastic Project Box
1 x Light Dependent Resister
2 x 10k Ohm Resisters
1 x PUSH BUTTON
1 x Small PCB to mount the component, not needed if you just want to run jumber wires.
Small 5v phone charger, I like to use a genuine brand
In the below picture you can see that I put two standoffs to hold
the LED Display off the NodeMCU and at the other end the pins do the
work where it plugs into the PCB,
You may need to ajust the hights of the standoffs with a file and
solder the pin shocket off the PCB a little, my socket had long
soldering pins, I made this so when the cover is on, the Display is
againt the inside of the cover.
I cut a piece of paper to go between the Display and the cover to
deffuse the Display.

click picture to enlarge
Below are the Sketches for Daylight Savings and none Daylight Savings regones 
that work, it may not be the best put you can change it to suit your own designs
Example 1
/*H*******************************************************
 MerrittSoft Internet Clock with Daylight Savings 30/01/2020
 Language: C++/Arduino
 Thank you to those people that made the Libarays in this project posable.
********************************************************/
#include <TM1637Display.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> // HERE I HAVE USED WiFiManager lIBRARY TO CONNECT TO WIFI,
 // IT DOSEN'T ALWAYS CONNECT AFTER POWER ON, THATS WHY I ADD A RESTET BUTTON
#include <DNSServer.h>
#include <TimeLib.h> // FOR MORE INFORMATION IN TimeLib Library, PLEASE
                                // READ PaulStoffregen LIBRARY Arduino Time
#include <Timezone.h> // TO UNDERSTAND HOW TO SETUP TIMEZONES AND DAYLIGHT SAVING TIMES,
 // PLEASE READ Jack Christensen lIBRARY ARDUINO TIMEZONE.

//************************* DEFINES ************************************
#define  BAUD  9600
#define DIO D5                                    // DISPLAY PINS ON ARDUINO
#define CLK D6
// part of fixed wifi ssid and password
//const char ssid[] = "SSID";                    // YOUR NETWORK SSID (NAME)
//const char pass[] = "PASSWORD";                   // YOUR NETWORK PASSWORD

//************************* PROTOTYPES ************************************
time_t getNtpTime();
void digitalClockDisplay();
void printDigits( int digits );
void sendNTPpacket( IPAddress &address );

//************************* VARIABLES ************************************
// UTC + 11 hours For Sydney Australia, fron Timezone library
TimeChangeRule aEDT = {"AEDT", First, Sun, Oct, 2, 660}; 
// UTC + 10 hours For Sydney Australia fron Timezone library
TimeChangeRule aEST = {"AEST", First, Sun, Apr, 3, 600}; 
Timezone ausET( aEDT, aEST);
int sensorPin = A0;
int formattedTimemin = 0;
int formattedTimehour = 0;
int sensorValue = 0;
TM1637Display display( CLK, DIO);
// NTP Servers:
// REPLACE THIS NTP SERVER WITH ONE THAT IS CLOSER TO YOUR LOCATION
static const char ntpServerName[] = "3.oceania.pool.ntp.org"; 
//static const char ntpServerName[] = "time.nist.gov";
//static const char ntpServerName[] = "time-a.timefreq.bldrdoc.gov";
//static const char ntpServerName[] = "time-b.timefreq.bldrdoc.gov";
//static const char ntpServerName[] = "time-c.timefreq.bldrdoc.gov";
int timeZone = 0; // GMT TIMEZONE HANDLED FROM TIMEZONE LIBRARY, AJUST ABOVE
WiFiUDP   Udp;
unsigned int localPort = 8888;       // LOCAL PORT TO LISTEN FOR UDP PACKETS
TimeChangeRule *tcr;
const int NTP_PACKET_SIZE = 48;  // NTP TIME IS IN THE FIRST 48 BYTES OF MSG
byte packetBuffer[NTP_PACKET_SIZE];   // INCOMING & OUTGOING PACKET BUFF
time_t prevDisplay = 0;                  // WHEN DIGITAL CLOCK WAS DISPLAYED

/*F********************************************************************
*
**********************************************************************/
void 
setup()
{
    WiFiManager wifiManager;                      // PART OF WIFIMANAGER lIB
    Serial.begin( BAUD );
     while( !Serial);                            // NEEDED FOR LEONARDO ONLY
         delay( 250 );
    Serial.println( "TimeNTP Example");
     // PART OF THE wifimanager Lib
     wifiManager.autoConnect( "Internet-Clock");
     // PART OF FIXED WIFI SSID AND PASSWORD
     // Serial.print("Connecting to ");
     //Serial.println(ssid);
     // WiFi.begin(ssid, pass);
     // while( WiFi.status() != WL_CONNECTED) 
     //{
     // delay(500);
     // Serial.print(".");
     // }
    Serial.print("IP number assigned by DHCP is ");
    Serial.println( WiFi.localIP());
    Serial.println( "Starting UDP");
     Udp.begin( localPort );
    Serial.print( "Local port: ");
    Serial.println( Udp.localPort());
    Serial.println( "waiting for sync");
     setSyncProvider( getNtpTime );
     setSyncInterval( 300 );
}
/*F********************************************************************
*
**********************************************************************/
void 
loop()
{
    delay( 10000 );                         // TIME UPDATE EVERY 10 SECONDS
    time_t utc = now();
    time_t local = ausET.toLocal(utc, &tcr);
    Serial.println();
    printDateTime(local, tcr -> abbrev);
    sensorValue = analogRead(sensorPin);
    if( sensorValue < 15) 
        display.setBrightness( 0x01 );
    else if( sensorValue < 50 ) 
        display.setBrightness( 0x03 );
    else 
        display.setBrightness( 0x07 );
    if( timeStatus() != timeNotSet) 
    {
        if( now() != prevDisplay) // UPDATE DISPLAY ONLY IF TIME HAS CHANGED
            prevDisplay = now();
    }
    // CHANGE 24HR TO 12HR
    if( formattedTimehour > 12 ) 
        morethen12();
    if( formattedTimehour < 1 ) 
        lessthenone();
    digitalClockDisplay();
}
/*F********************************************************************
* DIGITAL CLOCK TIME DISPLAY 
**********************************************************************/
void 
digitalClockDisplay()
{
    Serial.println() ;
    Serial.print( hour() ) ;
    printDigits( minute() ) ;
    printDigits( second() ) ;
    Serial.print( " ") ;
    Serial.print( day() ) ;
    Serial.print( ".") ;
    Serial.print( month() ) ;
    Serial.print( ".") ;
    Serial.print( year() ) ;
    Serial.println() ;
    Serial.print( sensorValue) ;  
    Serial.print( "sensorValue ") ;  
    Serial.print( sensorValue) ;
    display.showNumberDecEx( formattedTimehour * 100 + formattedTimemin
        , 0b01000000 , false, 4, 0) ;                     // SEND TO DISPLAY
}
/*F********************************************************************
* UTILITY FOR DIGITAL CLOCK DISPLAY: PRINTS PRECEDING COLON AND LEADING 0
**********************************************************************/
void 
printDigits( int digits )
{
    Serial.print(":");
    if( digits < 10 )
        Serial.print('0');
    Serial.print( digits );
}
/*F********************************************************************
* FUNCTION TO RETURN THE COMPILE DATE AND TIME AS A TIME_T VALUE
**********************************************************************/
time_t 
compileTime()
{
    const time_t FUDGE(10); // ADJ TO ALLOW FOR COMPILE TIME (SEC, YMMV)
    const char *compDate = __DATE__, *compTime = __TIME__
        , *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
    char chMon[4], *m;
    tmElements_t tm;
    strncpy( chMon, compDate, 3);
    chMon[3] = '\0';
    m = strstr( months, chMon);
    tm.Month = ( (m - months) / 3 + 1);
    tm.Day = atoi( compDate + 4);
    tm.Year = atoi( compDate + 7) - 1970;
    tm.Hour = atoi( compTime);
    tm.Minute = atoi( compTime + 3);
    tm.Second = atoi( compTime + 6);
    time_t t = makeTime( tm );
    return( t + FUDGE );       // ADD FUDGE FACTOR TO ALLOW FOR COMPILE TIME
}
/*F********************************************************************
* format and print a time_t value, with a time zone appended.
**********************************************************************/
void 
printDateTime( time_t t, const char *tz)
{
    char buf[32];
    char m[4]; // TEMP STG FOR MONTH STRING (DateStrings.cpp USES SHARED BUFF)
    strcpy( m, monthShortStr( month( t )));
    sprintf( buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
        hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t)
        , m, year(t), tz);
    // PUT HOURS AND MINUTES INTO FORMATTEDtIMEHOUR AND formattedTimemin FOR 
	// SENDING TO DISPLAY AFTER ITS CONVERTED TO 12HR TIME
    formattedTimemin = minute(t);
    formattedTimehour = hour(t);
    Serial.println( buf );
}
/*F********************************************************************
*
**********************************************************************/
time_t 
getNtpTime()
{
    IPAddress ntpServerIP;                        // NTP SERVER'S IP ADDRESS
    while( Udp.parsePacket() > 0) ;       // DISCARD PREVIOUSLY RCVE PKTS
    Serial.println("Transmit NTP Request");
    WiFi.hostByName( ntpServerName, ntpServerIP );// GET RANDOM SERVER FROM POOL
    Serial.print( ntpServerName );
    Serial.print( ": ");
    Serial.println( ntpServerIP );
    sendNTPpacket( ntpServerIP );
    uint32_t beginWait = millis();
    while( millis() - beginWait < 1500) 
	{
		int size = Udp.parsePacket();
		if( size >= NTP_PACKET_SIZE) 
		{
			Serial.println( "Receive NTP Response");
			Udp.read( packetBuffer, NTP_PACKET_SIZE );// READ PACKET INTO BUFF
			unsigned long secsSince1900;
			// CONVERT FOUR BYTES STARTING AT LOCATION 40 TO A LONG INTEGER
			secsSince1900 = (unsigned long)packetBuffer[40] << 24;
			secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
			secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
			secsSince1900 |= (unsigned long)packetBuffer[43];
			return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
		}
    }
    Serial.println("No NTP Response :-(");
    return( 0 );                           // RETURN 0 IF UNABLE TO GET TIME
}
/*F********************************************************************
* send an NTP request to the time server at the given address
**********************************************************************/
void 
sendNTPpacket( IPAddress & address )
{
    memset( packetBuffer, 0, NTP_PACKET_SIZE );   // SET ALL BUFF BYTES TO 0
    // INIT VALS FOR NTP REQUEST (SEE URL ABOVE FOR DETAILS ON PACKETS)
    packetBuffer[0] = 0b11100011;                       // LI, Version, Mode
    packetBuffer[1] = 0;                        // STRATUM, OR TYPE OF CLOCK
    packetBuffer[2] = 6;                                 // POLLING INTERVAL
    packetBuffer[3] = 0xEC;                          // PEER CLOCK PRECISION
    // 8 BYTES OF ZERO FOR Root Delay & Root Dispersion
    packetBuffer[12] = 49;
    packetBuffer[13] = 0x4E;
    packetBuffer[14] = 49;
    packetBuffer[15] = 52;
    // ALL NTP FIELDS HAVE BEEN GIVEN VALUES, NOW
    // YOU CAN SEND A PACKET REQUESTING A TIMESTAMP:
    Udp.beginPacket( address, 123);           // NTP REQUESTS ARE TO PORT 123
    Udp.write( packetBuffer, NTP_PACKET_SIZE);
    Udp.endPacket();
}
/*F********************************************************************
*
**********************************************************************/
void 
morethen12() 
{
    formattedTimehour = formattedTimehour - 12;
}
/*F********************************************************************
*
**********************************************************************/
void 
lessthenone() 
{
    formattedTimehour = formattedTimehour + 12;
}

Example 2
/*H******************************************************* 
MerrittSoft Internet Clock, No Daylight Savings 30/01/2020
Language: C++/Arduino Thank you to those people that made Libarays in this 
project posable.
********************************************************/
#include <TM1637Display.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp .h>
#include <ESP8266WebServer.h>
//#include <WiFiManager.h> 
// HERE i HAVE USED WiFiManager Library TO CONNECT TO WIFI,
// IT DOSEN'T ALWAYS CONNECT AFTER POWER ON, SO ADDED RESET BUTTON
//#include <DNSServer.h> // NEEDED FOR WiFiManager lib
#include <TimeLib.h> 
// For more info on TimeLib Library, Please read PaulStoffregen lib Arduino Time

//************************* DEFINES ************************************
#define DIO D5#define CLK D6
typedef unsinged long ulong;
typedef unsinged int  uint;

//************************* PROTOTYPES ************************************
void digitalClockDisplay();
void printDigits( int digits );
time_t getNtpTime();
void sendNTPpacket( IPAddress &address );
void morethen12(); 
void lessthenone(); 

//************************* VARIABLES ************************************
int sensorPin = A0;
int formattedTimemin = 0;
int formattedTimehour = 0;
int sensorValue = 0;
TM1637Display display( CLK, DIO);
const char ssid[] = "ssid";                      // YOUR NETWORK SSID (NAME)
const char pass[] = "password";                     // YOUR NETWORK PASSWORD
// NTP Servers:static const char ntpServerName[] = "3.oceania.pool.ntp.org"; 
// replace this NTP Server with one that is closer to your location
//static const char ntpServerName[] = "time.nist.gov";
//static const char ntpServerName[] = "time-a.timefreq.bldrdoc.gov";
//static const char ntpServerName[] = "time-b.timefreq.bldrdoc.gov";
//static const char ntpServerName[] = "time-c.timefreq.bldrdoc.gov";
int timeZone = +11;                          // CHANGE THIS TO YOUR TIMEZONE
// const int timeZone = -5;                   // Eastern Standard Time (USA) 
// const int timeZone = -4;                   // Eastern Daylight Time (USA)
// const int timeZone = -8;                   // Pacific Standard Time (USA)
// const int timeZone = -7;                   // Pacific Daylight Time (USA)
WiFiUDP Udp;
unsigned int localPort = 8888;       // LOCAL PORT TO LISTEN FOR UDP PACKETS
time_t getNtpTime();
void digitalClockDisplay();
void printDigits( int digits);
void sendNTPpacket( IPAddress &address);
time_t prevDisplay = 0;                  // WHEN DIGITAL CLOCK WAS DISPLAYED
const int NTP_PACKET_SIZE = 48;     // NTP TIME IN FIRST 48 BYTES OF MESSAGE
byte packetBuffer[NTP_PACKET_SIZE];       // INCOMING & OUTGOING PACKETS

/*F********************************************************************
*
**********************************************************************/
void 
setup()
{
    // WiFiManager wifiManager;                   // PART OF WIFIMANAGER LIB
    // wifiManager.autoConnect( "Internet-Clock" );// PART OF WIFIMANAGER lIB
    Serial.begin( BAUD );
    while( !Serial );                            // NEEDED FOR LEONARDO ONLY
        delay( 250 );
    Serial.println( "TimeNTP Example");
    Serial.print( "Connecting to ");
    Serial.println( ssid );
    WiFi.begin( ssid, pass );
    while( WiFi.status() != WL_CONNECTED) 
    { 
        delay(500); 
        Serial.print("."); 
    }
    Serial.print( "IP number assigned by DHCP is "); 
    Serial.println( WiFi.localIP()); 
    Serial.println( "Starting UDP"); Udp.begin(localPort); 
    Serial.print( "Local port: "); 
    Serial.println( Udp.localPort()); 
    Serial.println( "waiting for sync"); 
    setSyncProvider( getNtpTime ); 
    setSyncInterval( 300 );
}
/*F********************************************************************
*
**********************************************************************/
void 
loop()
{
    delay( 10000 );                          // TIME UPDATE EVERY 10 SECONDS
    sensorValue = analogRead( sensorPin );
    if( sensorValue < 15 ) 
        display.setBrightness( 0x01 ); 
    else if( sensorValue < 50 ) 
        display.setBrightness( 0x03 ); 
    else 
        display.setBrightness( 0x07 ); 
    if( timeStatus() != timeNotSet) 
    { 
        if( now() != prevDisplay) // UPDATE DISPLAY ONLY IF TIME HAS CHANGED
            prevDisplay = now();                   // digitalClockDisplay(); 
    }
    formattedTimehour = hour();
    if( formattedTimehour > 12 ) 
        morethen12(); 
    if( formattedTimehour < 1 ) 
        lessthenone(); 
    digitalClockDisplay();
}
/*F********************************************************************
* DIGITAL CLOCK DISPLAY OF TIME
**********************************************************************/
void 
digitalClockDisplay()
{
    Serial.println(); 
    Serial.print( hour()); 
    printDigits( minute()); 
    printDigits( second()); 
    Serial.print( " ");
    Serial.print( day()); 
    Serial.print( "."); 
    Serial.print( month()); 
    Serial.print( "."); 
    Serial.print( year()); 
    Serial.println(); 
    Serial.print( "sensorValue "); 
    Serial.print( sensorValue);
    display.showNumberDecEx( formattedTimehour * 100 + minute(), 0b01000000
        , false, 4, 0);                                   // SEND TO DISPLAY
}
/*F********************************************************************
* digital clock display: prints preceding colon and leading 0 
**********************************************************************/
void 
printDigits( int digits )
{
    Serial.print( ":" ); 
    if( digits < 10 ) 
    Serial.print( '0' ); 
    Serial.print( digits );
}
/*F********************************************************************
*
**********************************************************************/
time_t 
getNtpTime()
{ 
    IPAddress ntpServerIP;                        // NTP SERVER'S IP ADDRESS
    while( Udp.parsePacket() > 0) ;                     // CLEAR PKT BUFF
    Serial.println( "Transmit NTP Request");  // GET RANDOM SERVER FROM POOL
    WiFi.hostByName( ntpServerName, ntpServerIP); 
    Serial.print( ntpServerName ); 
    Serial.print( ": "); 
    Serial.println( ntpServerIP ); 
    sendNTPpacket( ntpServerIP ); 
    uint32_t beginWait = millis(); 
    while( millis() - beginWait < 1500) 
    { 
        int size = Udp.parsePacket(); 
        if( size >= NTP_PACKET_SIZE) 
        {
            Serial.println( "Receive NTP Response"); 
            Udp.read( packetBuffer, NTP_PACKET_SIZE);                  // RD PKT
            ulong secsSince1900; // chng 4 bytes at location 40 to a 
            long integer secsSince1900 = (ulong)packetBuffer[40] << 24;
            secsSince1900 |= (ulong)packetBuffer[41] << 16; 
            secsSince1900 |= (ulong)packetBuffer[42] << 8; 
            secsSince1900 |= (ulong)packetBuffer[43]; 
            return( secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR ); 
        } 
    } 
    Serial.println( "No NTP Response :-("); 
    return( 0 );                           // RETURN 0 IF UNABLE TO GET TIME
}
/*F********************************************************************
* send an NTP request to the time server at the given address
**********************************************************************/
void 
sendNTPpacket( IPAddress &address )
{ // set all bytes in the buffer to 0 
    memset( packetBuffer, 0, NTP_PACKET_SIZE); // Init vals for NTP request 
    // (see URL above for details on the packets) 
    packetBuffer[0] = 0b11100011; 
    packetBuffer[1] = 0; // LI, Version, Mode 
    packetBuffer[2] = 6; // Polling Interval // Stratum, or type of clock 
    packetBuffer[3] = 0xEC; // Peer Clock Precision 
    // 8 bytes of zero for Root Delay & Root Dispersion 
    packetBuffer[12] = 49; 
    packetBuffer[13] = 0x4E; 
    packetBuffer[14] = 49; 
    packetBuffer[15] = 52; // all NTP fields have been given values, now 
    // you can send a packet requesting a timestamp: 
    Udp.beginPacket( address, 123); //NTP requests are to port 123 
    Udp.write( packetBuffer, NTP_PACKET_SIZE); 
    Udp.endPacket();
}
/*F********************************************************************
*
**********************************************************************/
void 
morethen12() 
{ 
    formattedTimehour = formattedTimehour - 12;
}
/*F********************************************************************
*
**********************************************************************/
void 
lessthenone() 
{ 
    formattedTimehour = formattedTimehour + 12;
}