Project 19:NTP-Synced Network Clock

The Internet, a critical data-transmission medium for the IoT, supports massive devices' time synchronization needs. The NTP protocol, with high precision and stability, is the core technology for ensuring system timing consistency. In this lesson, we'll use the NTP protocol to get network time and build a network time alarm clock. 
At the end of the project, we will also combine practical application scenarios to learn about common network transmission protocols used in the IoT.


Project Objectives


Knowledge Objectives


1.Understand NTP and other common network protocols.
2.Learn to use APls to obtain online data.
3.Connect the UNIHIKER K10 to Wi-Fi.


Practical Objective 


Use the UNIHIKER K10 to connect to a wireless network, obtain network time, and implement a timed reminder function.


Materials List

HARDWARE LIST
1 UNIHIKER K10
1 USB Cable

Preparations


Hardware


Connect the UNIHIKER K10 to the computer using a USB Cable.

Software


Open Mind+, switch to“Upload Mode”, connect the UNIHIKER K10 as shown in the figure below, and load the UNIHIKER K10 library.

 

Hands-on Practice


Next, we'll gradually implement the functions of wireless network connection, time acquisition, and alarm reminder at the set time.
Task 1: Connect to a Wireless Network
First, we'll learn to connect the UNIHIKER K10 to a network and add connection reminders.
Task 2: Obtain Time and Build a Network Time Alarm Clock
Then, we'll get network time, set time judgment through programming, and finish the timed reminder function.


Task 1: Connect to a Wireless Network


Code  


First, let's locate the graphical library for wireless connection.Click "Extension", search for the "Wi-Fi" in "Module", find the corresponding module and click to load it. Return to the block area, and you will see the WiFi connection blocks.

Next, we can follow the core logic of "connect to the network→wait for successful connection→output the IP address" to complete writing the core code, as shown below:

To better display the WiFi connection status, we can add text information prompts. The complete reference code is as follows:

Run the Code


Click the“Upload”button.After the upload is complete, wait a moment. You'll see“Network connection successful”and UNIHIKER K10's IP address on the screen, indicating a successful network connection. 


Code Review


 

Category

Blocks

Function

Enter the hotspot name and password to connect the UNIHIKER K10 to the specified Wi-Fi hotspot.
Detect the Wi-Fi connection status of the UNIHIKER K10 to determine whether it has successfully connected to the network.
Read the IP address information assigned to the UNIHIKER K10 after it connects to the network.


Task 2: Obtain Time and Build a Network Time Alarm Clock


Code


Similarly,load the graphical library for time acquisition. Click "Extension", search for the "NTP" in "Module", find the corresponding module and click to load it. Return to the block area, and you will see the NTP-related blocks.

Next, we can set up the server, then obtain the network time (such as "hours" and "minutes"), and store them in variables for easy subsequent use. It should be noted that the obtained content must be stored in string variables:

After obtaining the time, we can display it and set the alarm time through programming.For easier testing, you can set it to a nearby time.You may also set a suitable time interval to prevent the alarm from reminding repeatedly.The complete reference code is as follows:

Run the Code


Click the“Upload”button. After waiting for the upload to complete, wait until the set time is reached, and the prompt music will play.


Code Review
 

Category

Blocks

Function

Specify the time zone and time server, and synchronize the network time for the UNIHIKER K10 via the NTP protocol.
Read the synchronized current time data (including year, month, day, hour, etc.) of the UNIHIKER K10.


Knowledge Base


Next, let's learn and summarize the hardware knowledge used in this lesson.
Today we used the NTP protocol to obtain the time. So what is a protocol, and what is the NTP protocol?


What Is a Network Communication Protocol


Strictly speaking, today's protocol is a network communication protocol, a set of pre-agreed rules and standards for smooth device communication. Similar to two people from different countries needing to agree on language, speed, and end - of - sentence signals when talking, in the online world, computers, mobile phones, and servers from different manufacturers with different operating systems must follow the same "language" rules to send information successfully. This set of rules is the network communication protocol. 
Its core purpose is to ensure smooth data transmission between devices. Engineers design different protocols for different needs. For instance, we use HTTP for web browsing, SMTP for email sending, and NTP for time-obtaining. 


What Is the NTP Protocol


The full Chinese name of NTP is "Network Time Protocol". It's a set of rules for network computer time synchronization. If a school's classroom clocks ran at different speeds, classes and breaks would be chaotic. NTP is like "Beijing Time", ensuring all school clocks show the same accurate time. 
As in previous practice, we requested time from a server over the network. The NTP protocol's core is client-server interaction. The URL in the program is the server address, and the UNIHIKER K10 is the client. After the UNIHIKER K10 connects to the network, it sends a request to the server. Then, the server sends back the accurate time upon receiving the request.

The NTP protocol is mainly used to keep the time consistent across mobile phones, computers and campus networks.With its built-in time offset calibration mechanism, it can provide relatively accurate time information.


General Methods for Obtaining Network Data


Like normal web browsing, we can let the UNIHIKER K10 obtain other network information via the Internet. Besides using the NTP protocol for time acquisition, we can use the HTTP protocol for tasks, get network service links (network APIs), and complete data acquisition. The acquisition steps are as follows.

Challenge Task


Try to use the month and day from the obtained time to add a schedule reminder function to your alarm clock.For example, set a reminder for December 22nd to remind yourself to plan Christmas gifts in advance.
Use the month and day from the obtained time to add a schedule reminder to your alarm clock. For example, set a reminder for December 22nd to plan Christmas gifts in advance.
The sample code is as follows:

icon Project 19.zip 11KB Download(0)
License
All Rights
Reserved
licensBg
0