This article shows how to use the NIST Internet Time Service (ITS) to synchronize the Wizard Gecko WGM110 Wi-Fi module's internal RTC (Real Time Clock).
There are different ways to sync up time through the internet but for this article we selected the Daytime Protocol (RFC-867) over TCP. For this protocol the NIST ITS is listening to TCP port 13 and once a client connects it will send to the client the data representing the current time.
The standard does not specify an exact format for the Daytime Protocol, but requires that the time is sent using standard ASCII characters. Please visit the NIST link above to learn more about format used by NIST.
Requesites
This project is in many ways similar to the gmail example from the SDK.You will need an AP with internet access and the WGM110 WSTK because the LEDs on the kit are used to signal AP connection and on-going NIST query, and the push buttons are used to print the RTC time over UART and initiate the NIST query.
BGScript Project
Before building and flashing the project to the WGM110 it must first be modified with the credentials of your AP with internet access. The file sta.bgs contains the BGScript code that manages the AP connection and the comments there will guide you to write your AP's SSID and password.
Once you flash it to the module on the WSTK it will turn on LED1 which indicates that it has successfully connected to the AP. The application will now be waiting for the user to push PB0 to initiate the NIST query.
NIST Query
To retrieve the current time from the NIST ITS server it is first needed to resolve the hostname with the command below where nist_server_name = "time.nist.gov":
When the hostname has been resolved the event tcpip_dns_gethostbyname_result is raised where the hostname's IP is given. Then the TCP connection can be established on port 13 using the command:
Once this is done it's just a matter of a few seconds and the server will send the ascii string with the current time which is captured with endpoint_data event. When the data is received the string is parsed and the values are used to set the time in the RTC.
# The response starts with '\n', so the first year character is on index 7
call util_atoi(2, data_from_server(7:2))(year)
call util_atoi(2, data_from_server(10:2))(month)
call util_atoi(2, data_from_server(13:2))(day)
call util_atoi(2, data_from_server(16:2))(hour)
call util_atoi(2, data_from_server(19:2))(minute)
call util_atoi(2, data_from_server(22:2))(second)
# Year needs to be added to 2000
call hardware_rtc_set_time((year+2000),month,day,hour,minute,second)(result)
The NIST response is also fully printed out to the UART when it's received for visual confirmation by the user.
Printing RTC Time
When the user presses PB1 the WGM110 will print out the current RTC time. This way it is possible to see the time before and after the NIST ITS sync to confirm that it has been done correctly.
If you press PB1 before the NIST ITS sync you'll see that the default time after RTC initialization is 1/1/1970 0:00:00. This has not been selected randomly, it's the starting point for the UNIX time which counts the number of seconds elapsed since this point in time.
After sync with NIST ITS you can press PB1 again to confirm that the data was correctly parsed and the RTC was updated correctly. The NIST gives the current UTC time so it needs to be adjusted to your particular time zone.
WGM110 - Sync with NIST Internet Time Service