18 Clock with NTP server by NodeMCU

Introduction

The Network Time Protocol (NTP) is used by hundreds of millions of computers and devices to synchronize their clocks over the Internet. If your computers sets its own clocks, it likely uses NTP. NTP is one of the oldest Internet protocols in current use. NTP was designed by David L. Mills of the university of  Delaware.
NTP can usually maintain time to within tens of milliseconds over the public Internet, and can achieve better than one millisecond accuracy in local area networks under ideal conditions. Asymmetric routes and network congestion can cause errors of 100 ms or more.

Synapsis

We will make program to print Time and day on Serial monitor by using NodeMCU. So we need only one NodeMCU board and one cable to connect Board and PC. 

Programming

Open you Arduino IDE. Now we have to install NTP Client library so go to Sketch >> Include Library >> Manage Libraries.
Now search NTP Client and and Install NTP Client  developed by Fabrice Weinberg.
Download NTPClient Library

Now go to File >> Examples >> NTPClient >>  Advanced

Include NTPClient, ESP8266WiFi and WiFIUpd  libraries. NTPClient.h is time library to synchronization with NTP server. ESP8266WiFi.h library connect wifi with NodeMCU board. WiFiUdp.h library handles UDP protocol like opening a UDP port. "priyo" and "Priyo789" is SSID name and Password of my local network so you have to replace with SSID and Password of  your local network. In timeClient object we have to pass UPD, poolserver name, timeoffset and updateinterval time.
 #include <NTPClient.h>  
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char *ssid = "priyo"; // WiFi name
const char *password = "Priyo789"; // WiFi password
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "in.pool.ntp.org", 19800, 60000);
// NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);
char dayWeek [7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
This is void setup function it will run only one time. I initialized Serial protocols with 115200 bps. WiFI.begin function start connectivity with wifi by using SSID and Password. NTP server will start by timeClient.begin function.
 void setup(){  
Serial.begin(115200);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
This void loop function it runs continue. timeClient.update() function update the current time. timeClient.getday() function return Day and print on serial monitor. timeClient.getFormattedTime() function return current time.
 void loop() {  
timeClient.update();
Serial.print(dayWeek[timeClient.getDay()]);
Serial.print(" ");
Serial.println(timeClient.getFormattedTime());
delay(1000);
}

Code


Now upload this code in NodeMCU board. After  completed uploading code open Serial monitor and set 115200 bps. this is output. There is printing Day and Time on serial monitor. 
You can interface 16x2 LCD, OLED and other type of display with NodeMCU to print time and Day. 

👇👇watch this video for more details👇👇
Thanks for visiting🙏

No comments:

Powered by Blogger.