logo
NodeMCU Internet Clock
Internet Clock

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
1x 85x58x33mm Waterproof Clear Cover Plastic Project Box
1x Light Dependent Resister
2x 10k Ohm Resisters
1x 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








nodemcu_inetrnet_clock        nodemcu_inetrnet_clock

nodemcu_inetrnet_clock        nodemcu_inetrnet_clock

nodemcu_inetrnet_clock        nodemcu_inetrnet_clock

nodemcu_inetrnet_clock       nodemcu_inetrnet_clock

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.

nodemcu_inetrnet_clock       

nodemcu_relay_gpo_wiring_s
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

/*

  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 the WiFiManager Library to connect to the wifi,
                        //I have found that 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 the TimeLib Library, Please read PaulStoffregen library Arduino Time.

#include <Timezone.h> // To understand how to setup the timezones and daylight saving times,
                     //please read Jack Christensen Library Arduino Timezone.

TimeChangeRule aEDT = {"AEDT", First, Sun, Oct, 2, 660};    // UTC + 11 hours For Sydney Australia, fron Timezone library
TimeChangeRule aEST = {"AEST", First, Sun, Apr, 3, 600};    // UTC + 10 hours For Sydney Australia fron Timezone library
Timezone ausET(aEDT, aEST);

// Display pins on Arduino
#define DIO D5
#define CLK D6


int sensorPin = A0;
int formattedTimemin = 0;
int formattedTimehour = 0;
int sensorValue = 0;

TM1637Display display(CLK, DIO);


// part of the fixed wifi ssid and password
//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 = 0;     // GMT--- Timezone handled from the Timezone library, ajust there above



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


TimeChangeRule *tcr;

void setup()
{

 // part of the wifimanager Library
 WiFiManager wifiManager;


 Serial.begin(115200);
 while (!Serial) ; // Needed for Leonardo only
 delay(250);
 Serial.println("TimeNTP Example");


 // part of the wifimanager Library
 wifiManager.autoConnect("Internet-Clock");


 // part of the 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);

}

time_t prevDisplay = 0; // when the digital clock was displayed

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 the display only if time has changed
     prevDisplay = now();
   }
 }
 // change 24hr to 12hr
 if (formattedTimehour > 12 ) {
   morethen12();
 }

 if (formattedTimehour < 1 ) {
   lessthenone();
 }

 digitalClockDisplay();
}

void digitalClockDisplay()
{
 // digital clock display of the time
 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);

 // send to display
 display.showNumberDecEx(formattedTimehour * 100 + formattedTimemin, 0b01000000 , false, 4, 0);
}

void printDigits(int digits)
{
 // utility for digital clock display: prints preceding colon and leading 0
 Serial.print(":");
 if (digits < 10)
   Serial.print('0');
 Serial.print(digits);
}

// Function to return the compile date and time as a time_t value
time_t compileTime()
{
 const time_t FUDGE(10);     // fudge factor to allow for compile time (seconds, 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

}

// 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];    // temporary storage for month string (DateStrings.cpp uses shared buffer)
 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 the 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);
}



/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
 IPAddress ntpServerIP; // NTP server's ip address

 while (Udp.parsePacket() > 0) ; // discard any previously received packets
 Serial.println("Transmit NTP Request");
 // get a random server from the 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);  // read packet into the buffer
     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 the time
}

// 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);
 // Initialize values needed to form NTP request
 // (see URL above for details on the 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();

}


void morethen12() {
 formattedTimehour = formattedTimehour - 12;
}


void lessthenone() {
 formattedTimehour = formattedTimehour + 12;

}

Below is the Sketch for None Daylight Savings
/*
 MerrittSoft
 Internet Clock, No 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 the WiFiManager Library to connect to the wifi,
//I have found that it dosen't always connect after power on, thats why i add a restet button.

//#include <DNSServer.h> // Needed for the WiFiManager library.

#include <TimeLib.h> //For more information in the TimeLib Library, Please read PaulStoffregen librart Arduino Time.

#define DIO D5
#define CLK D6

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

void setup()
{
 // part of the wifimanager Library
 //WiFiManager wifiManager;

 // part of the wifimanager Library
 //wifiManager.autoConnect("Internet-Clock");

 Serial.begin(115200);
 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);
}

time_t prevDisplay = 0; // when the digital clock was displayed

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 the display only if time has changed
     prevDisplay = now();
     //digitalClockDisplay();
   }
 }

 formattedTimehour = hour();

 if (formattedTimehour > 12 ) {
   morethen12();
 }

 if (formattedTimehour < 1 ) {
   lessthenone();
 }

 digitalClockDisplay();
}

void digitalClockDisplay()
{
 // digital clock display of the time
 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);

 // send to display
 display.showNumberDecEx(formattedTimehour * 100 + minute(), 0b01000000 , false, 4, 0);
}

void printDigits(int digits)
{
 // utility for digital clock display: prints preceding colon and leading 0
 Serial.print(":");
 if (digits < 10)
   Serial.print('0');
 Serial.print(digits);
}

/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
 IPAddress ntpServerIP; // NTP server's ip address

 while (Udp.parsePacket() > 0) ; // discard any previously received packets
 Serial.println("Transmit NTP Request");
 // get a random server from the 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);  // read packet into the buffer
     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 the time
}

// 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);
 // Initialize values needed to form NTP request
 // (see URL above for details on the 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();

}


void morethen12() {
 formattedTimehour = formattedTimehour - 12;
}


void lessthenone() {
 formattedTimehour = formattedTimehour + 12;

}

www.000webhost.com