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.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: 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.
Step 2: Part 1 of HOW IT WORKS
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.
Step 3: Part 2 of HOW IT WORKS (SSID and PASSWORD)
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. (As shown in the first picture)
Step 4: Part 3 of HOW IT WORKS (changing SSID and PASSWORD)
Simply change the SSID and the PASSWORD to suit your router.
Step 5: Part 4 of HOW IT WORKS
The code above is used draw the face of the clock on the 4Duino display.
Step 6: The Functions
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.
Step 7: 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.
Step 8: Function: Unsigned Long GetTime ( ) (part1)
4Duino sends an NTP request to Server over UDP.
Step 9: Function: Unsigned Long GetTime ( ) (part2)
Server in response will send packet of data, in which the bytes 40 to 43 contains the timestamp.
Step 10: Function: Unsigned Long GetTime ( ) (part3)
This data is extracted and combined to calculate the NTP time (seconds from the date, January 1 1900).
Step 11: Function: Unsigned Long GetTime ( ) (part4)
This is further converted to UNIX time (seconds from the date, January 1 1970) and the function returns this value.
Step 12: 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.
Step 13: 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.
Step 14: FLOWCHART
Step 15: COMPONENTS
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.
Step 16: IMPLEMENTATION
4Duino has an inbuilt ESP8266 Wi-Fi module. Hence, there is no external circuitry required for this project.
Step 17: IMPLEMENTATION
Workshop 4 – 4Duino Basic Graphics environment is used to program this project. (The same could be implemented in the latest Arduino IDE)
This project requires the Arduino IDE to be installed as Workshop calls the Arduino IDE for compiling the Arduino sketches. The Arduino IDE however is not required to be opened or modified to program the 4Duino. (Shown in the first figure.)
Open this file using Workshop 4.
Change the SSID and the PASSWORD to suit your router, in the program. (Shown in the second figure)
Note: Download the project here.
Step 18: IMPLEMENTATION
3. Comms Port
Connect the 4Duino to the PC using µUSB cable. Then navigate to the Comms tab and select the Comms port to which the 4Duino connected.
Step 19: IMPLEMENTATION
4. Compile and Upload
Finally, go back to “Home” tab and now click on the “Comp’nLoad” button.
Step 20: DEMONSTRATION
After uploading 4Duino communicates to the NTP server over UDP and displays synchronised time on the 4Duino screen.