Getting Date & Time From NTP Server With ESP8266 NodeMCU
-----------------------------------------------------------------------------------
Email: elafe1888@gmail.com
linkden: www.linkedin.com/in/elaf-a-saeed-97bbb6150
facebook: https://www.facebook.com/profile.php?id=100004305557442
twitter: https://twitter.com/ElafASaeed1
github: https://github.com/ElafAhmedSaeed
youtube: https://youtube.com/channel/UCE_RiXkyqREUdLAiZcbBqSg
slideshare: https://www.slideshare.net/ElafASaeed
Slideplayer: https://slideplayer.com/search/?q=Elaf+A.Saeed
Google Scholar: https://scholar.google.com/citations?user=VIpVZKkAAAAJ&hl=ar&gmla=AJsN-F7PIgAjWJ44Hzb18fwPqJaaUmG0XzbLdzx09
2. The Contents
• NTP Server With ESP8266 NodeMCU.
• Getting Day & Time From NTP Server With ESP8266 NodeMCU.
• Getting Current Day and Time from NTP Server.
• Getting Current Date and Time from NTP Server.
3. Getting Day & Time & date From NTP
Server With ESP8266 NodeMCU
5. NTP Server With ESP8266 NodeMCU
(cont.)
• NTP stands for Network Time Protocol and it is a networking protocol for clock
synchronization between computer systems. In other words, it is used to synchronize
computer clock times in a network.
• The protocol can be used to synchronize all networked devices to Coordinated
Universal Time (UTC) within a few milliseconds ( 50 milliseconds over the public
Internet and under 5 milliseconds in a LAN environment).
• Coordinated Universal Time (UTC) is a world-wide time standard, closely related to
GMT (Greenwich Mean Time). UTC does not vary, it is the same world wide.
6. NTP Server With ESP8266 NodeMCU
(cont.)
❑NTPArchitecture
• NTP uses a hierarchical architecture. Each level in the
hierarchy is known as a stratum.
• At the very top are high-precision timekeeping devices,
such as atomic clocks, GPS or radio clocks, known as
stratum 0 hardware clocks.
• Stratum 1 servers have a direct connection to a stratum 0
hardware clock and therefore have the most accurate time.
• Each stratum in the hierarchy synchronizes to the stratum
above and act as servers for lower stratum computers.
7. NTP Server With ESP8266 NodeMCU
(cont.)
❑How NTP Works?
• NTP can operate in a number of ways. The most common configuration
is to operate in client-server mode. The basic working principle is as
follows:
1. The client device such as ESP8266 connects to the server using the
User Datagram Protocol (UDP) on port 123.
2. A client then transmits a request packet to a NTP server.
3. In response to this request the NTP server sends a time stamp packet.
4. A time stamp packet contains multiple information like UNIX
timestamp, accuracy, delay or time zone.
5. A client can then parse out current date & time values.
8. NTP Server With ESP8266 NodeMCU
(cont.)
❑Installing the NTPClient Library
• We’ll use the NTPClient library to get time. In your Arduino IDE, go to
Sketch > Library > Manage Libraries. The Library Manager should open.
• Search for NTPClient and install the library by Fabrice Weinber as shown in
the following image.
9. Getting Day & Time From NTP Server
With ESP8266 NodeMCU
10. Getting Day & Time From NTP Server
With ESP8266 NodeMCU
❑NTPClient Library Time Functions
• getDay() – returns an int number that corresponds to the the week day (0 to 6)
starting on Sunday;
• getHours() – returns an int number with the current hour (0 to 23) in 24 hour format;
• getMinutes() – returns an int number with the current minutes (0 to 59);
• getSeconds() – returns an int number with the current second;
• getEpochTime() – returns an unsigned long with the epoch time (number of seconds
that have elapsed since January 1, 1970 (midnight GMT);
• getFormattedTime() – returns a String with the time formatted like HH:MM:SS;
11. Getting Day & Time From NTP Server
With ESP8266 NodeMCU (cont.)
14. Getting Current Date and Time from
NTP Server
❑Function that is used in Code
1. year()➔ Set or read the RTC year value.
Syntax
year(unsigned long year)
2. month() ➔ Set or read the RTC month value.
Syntax
month(unsigned long month)
3. day() ➔ Set or read the RTC day value.
Syntax
day(unsigned long day)
15. Getting Current Date and Time from
NTP Server (cont.)
❑Function that is used in Code
4. minute() ➔ Set or read the RTC minute value.
Syntax: minute(unsigned long minute)
5. second() ➔ Set or read the RTC second value.
Syntax: second(unsigned long second) .