In this project, we will learn the basics of User Datagram Protocol (UDP). We will request and receive time from the NTP server using the above communication protocol. The 4Duino display is utilised to print the received time in both, digital and analogue clock format.
Transfer of Data over Internet
The 2 most common ways to transfer chunks of data (also known as packets) over the internet are Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). This is built on top of the IP layer. This means whether you are sending a packet via TCP or UDP, that packet is sent to an IP address.
An Internet Protocol address (IP address) is a numerical label assigned to each device in a network. IP addresses are usually written and displayed in human-readable notation, such as 220.127.116.11. When you type the name of the website (www.google.com) you are actually referring to the IP address of the server associated with that website name.
When a service is initially started it is required to bind to a designated port number. This is requested by the client. Higher level application that uses TCP/IP such as Web protocol, Hypertext Transfer Protocol, have ports preassigned numbers. These are called “well-known ports”. For the HTTP service, port 80 is defined as default. The widely used term “TCP/IP” refers to TCP over IP could just as well be referred to as “UDP/IP”, although this isn’t a common term.
More on UDP
Unlike TCP, while the packets are transferred over UDP the sender won’t wait to make sure if the recipient received the packet. It will merely continue sending the next packets. This means if the receiver missed some packets, it is lost and the receiver would not have that information. There is no guarantee on the reliability of the communication over UDP and this overhead increases the communication speed compared to TCP.
UDP is used when speed is desirable over reliability of the communication. For example, UDP is frequently used for live broadcasts and online games.
HOW IT WORKS
NTP stands for Network Time Protocol, and it is an Internet protocol used to synchronize the clocks of computers to of Coordinated Universal Time (UTC) reference. In operation since before 1985, NTP was originally designed by David L. Mills of the University of Delaware.
NTP is intended to synchronize all participating computers to within a few milliseconds of UTC. In this project, using NTP we are going to synchronize our 4Duino with UTC and display this time on the 4Duino screen.
In the skeleton code, there is a section commented to initialise the ESP8266 Wi-Fi Module. All you need to do is uncomment these lines of code and the initialisation is done for you. Now the Wi-Fi modules is ready to be controlled using AT Commands.
The default baud rate is set to 115200 bps, however the maximum baud rate at which Software serial actually works is 19200 bps. So in the code it requires changing the baud rate and this is done for you in the skeleton code.
ESP8266 is set to station mode and joins the AP, which can be your home router. SSID and PASSWORD are defined in the code respectively. Also, the program sets the modules to single connection mode. In single connection mode, only one TCP or UDP communication can be created.
Simply change the SSID and the PASSWORD to suit your router.
The code below is used draw the face of the clock on the 4Duino display.
The functions presented here are called upon in the flow chart later in this document. It is best to present the purpose of each functions earlier. This will make the comprehension of the flowchart easier, showing how the program works as a whole.
Function: unsigned long GetTime ( )
Return: UNIX time (seconds from the date, January 1 1970)
4Duino initiates communication with the NTP webserver over UDP and binds the service to the port 123.
4Duino sends an NTP request to Server over UDP.
Server in response will send packet of data, in which the bytes 40 to 43 contains the timestamp.
This data is extracted and combined to calculate the NTP time (seconds from the date, January 1 1900).
This is further converted to UNIX time (seconds from the date, January 1 1970) and the function returns this value.
Function: String GMTTime (int GMT)
Input: Time difference from GMT
Return: Time of the location as String in the format of HH:MM:SS
This function converts UNIX time to hours, minutes and seconds and returns a string with this information in the format of (HH:MM:SS).
Additionally, it receives the time difference of the location from GMT as an input, allowing function to account for difference in GMT and calculate the time of the particular location.
Function: void DrawHand (int length, int angle, int colour, int size)
Input: Length of the hand, Angle, Colour of the hand, Radius of the filled circle at the end of the hand
This function allows to draw the hands (hour, minute, second) of the clock corresponding to the time calculated.
This projects only utilizes the internal circuitry of the 4Duino. Hence you do not require any components for this project. However, a micro USB cable is required to program the 4Duino.
4Duino has an inbuilt ESP8266 Wi-Fi module. Hence, there is no external circuitry required for this project.
Read more: NTP Clock
JLCPCB – Prototype 10 PCBs for $2 + 2 days Lead Time
China’s Largest PCB Prototype Enterprise, 300,000+ Customers & 10,000+ Online Orders Per Day
Inside a huge PCB factory: https://www.youtube.com/watch?v=_XCznQFV-Mw