nRF5 IoT SDK
v0.9.0
|
The Simple Network Time Protocol Version 4 (SNTPv4), which is a subset of the Network Time Protocol (NTP), is used to synchronize computer clocks in the Internet. The SNTP client module is is implemented at the Application Layer and uses Nordic's UDP module as transport. The SNTP client also requires the IoT Timer module to be present.
The example application also incorporates Nordic's DNS client module to resolve the hostname of NTP servers to IPv6 addresses.
The setup of the example application is presented in Figure 1.
This example demonstrates how Nordic's IPv6 stack can be used for sending an SNTP query and how the response can be processed. The result of a successful query is used to synchronise the local clock with the NTP server.
The hostname of two public NTP servers and the IPv6 address of a DNS server is hardcoded in main.c
. Triggered by pressing Button 1 or Button 2, the hostname of one of the servers is first resolved to an IPv6 address. Once a response from the DNS server is received, an SNTP query is sent. If the query is successful, the local clock is synchronised with the NTP server. Local time is kept in the Unix time format and can be obtained by pressing Button 3.
The result of the query and the local Unix time is transmitted through a serial device, and a terminal program (for example PUTTY) can be used to monitor the COM port through a terminal session.
The sequence of messages exchanged between the example SNTP client application and the servers is shown in Figure 2.
Figure 3 illustrates the states of the application.
This section summarizes the usage of nRF5x resources and common modules in the examples apart from the IoT 6lowpan and IPv6 stack library.
Module | Inclusion/Usage | Description |
---|---|---|
Timer | 2 | The timer is used to make the LEDs blink on success. |
Buttons | 3 | Buttons are used for initiating queries and printing local time of day. |
LEDs | 4 | LEDs are used to indicate the application states. See the LED assignment section. |
Adv Data Encoder | Yes | The device name used is 'SNTP_Client', IPSP Service UUID is included in the UUID list. |
Scheduler | No | Scheduler is used for processing stack events. |
UART Trace | Yes | Tracing is included to display requests and responses on the terminal. |
The source code and project files of the example can be found in the following folder:
<InstallFolder>/examples/iot/sntp
LED 1 | LED 2 | LED 3 | LED 4 | |
---|---|---|---|---|
Blinking | Off | Off | Off | Device advertising as BLE peripheral. |
On | Blinking | Off | Off | BLE link established, IPv6 interface down. |
Off | On | Off | Off | IPv6 interface up, the device is idle. |
Off | On | Blinking | Off | Device busy: DNS query sent, hostname resolution in progress. |
Off | On | Off | Blinking | Device busy: SNTP query sent, awaiting response. |
On | On | On | On | Assertion failure in the application. |
Button 1 | Button 2 | Button 3 |
---|---|---|
Commence querying NTP server one. | Commence querying NTP server two. | Get local time. |
See Connecting devices to the router for a list of relevant Linux commands.