SNTP Slow and sometimes wrong

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
NaFi_2915566
Level 3
Level 3
First like received First like given

I am running SNTP after establishing a WiFi connection.  SNTP takes 15-30 seconds and sometimes sets the time to an invalid value (or fail and leaves it alone, I have not debugged that aspect)

I am comparing my code to snip.http_server_sent_events.c, which works much faster and I have never seen it fail.

from snip.http_server_sent_events.c

    /* Initialise the device */

    wiced_init();

    /* Bring up the STA (client) interface */

    wiced_network_up( WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL );

    /* Timestamp is needed for server-sent event (SSE) data.

     * Start automatic time synchronisation and synchronise once every day.

     */

    sntp_start_auto_time_sync( 1 * DAYS );

my code

   int wifiConnectCount=0;

   do

   {

      result = wiced_network_up(WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL);

      if ( result != WICED_SUCCESS )

      {

         WPRINT_APP_ERROR( ( "***Error: wiced_network_up failed: %u\n", result) );

         // see if we failed enough times to declare an error

         if (wifiConnectCount < 1)

         {

            wifiConnectCount++;

         }

         else

         {

            // we've tried enough times, start showing error indication

            // this will clear later when we find wifi

            setUiState(LED_UI_CONNECTION_ERROR);

         }

      }

   } while (result != WICED_SUCCESS);

   // we found WiFi, go back to "starting" indication

   setUiState(LED_UI_STARTUP);

  

   /* Print out network info */

   WPRINT_APP_INFO((" **NETWORK INFO\r\n"));

   wiced_ip_get_ipv4_address(WICED_STA_INTERFACE, &myAddress);

   printIp("  *IP addr: ", myAddress);

   wiced_ip_get_netmask(WICED_STA_INTERFACE, &ipAddress);

   printIp("  *Netmask: ", ipAddress);

   wiced_ip_get_gateway_address(WICED_STA_INTERFACE, &ipAddress);

   printIp("  *Gateway: ", ipAddress);

   wwd_wifi_get_rssi (&myrssi);

   WPRINT_APP_INFO(("  *RSSI: %d\n", (int)myrssi));

  

   // get SNTP going

   WPRINT_APP_INFO(("Start SNTP\n"));

   sntp_start_auto_time_sync( TIME_SYNC_PERIOD );

   wiced_time_get_utc_time_ms (&utc_time_ms);

   WPRINT_APP_INFO(("SNTP started at %llu\n", utc_time_ms));

   sntpTimeReceived = true; 

TIME_SYNC_PERIOD is defined as 1*DAYS

I cannot see a difference between the sample code and mine.  they are using the same WiFI network.  Any ideas?

Message was edited by: Nadi Findikli (removing some product-name items)

0 Likes
1 Solution

1. When the function sntp_start_auto_time_sync() is called, there is an initial random delay which is required to prevent thundering herd scenario as per RFC4330. That should explain the initial delay of 15-30 seconds after bringing up the Wi-Fi network.

2. You can enable WPRINT_ENABLE_LIB_DEBUG in wiced_defaults.h to enable debug printing in sntp.c. SNTP basically utilizes UDP protocol which is unreliable by design due to lack of ACK. You can consider increasing the number of NTP attempts defined by MAX_NTP_ATTEMPTS to 6 and check if that reduces the frequency of your issue. You can also print out the return value of wiced_udp_receive(&socket, &packet, WICED_NTP_REPLY_TIMEOUT) to check the error code. Also the return value of wiced_time_get_utc_time( &utc_time ) should be checked so you can print out the return value of this function and check if it is non-zero.

View solution in original post

5 Replies
GrCa_1363456
Level 6
Level 6
50 likes received Beta tester First comment on KBA

Check if there is a "wiced_init()" command in the lower code to properly initialize the device.

0 Likes

there is a wiced_init() in our application_start().

One difference between the sample and out code is:

  • the sample code runs all of the above code in application_start()
  • our code runs wiced_init() in application_start(); then wiced_network_up() and sntp_start_auto_time_sync() in a wifi manager thread.
0 Likes

There is a related issue due to thread safety of WICED time WICED: the time is very inaccurate It had been escalated internally for review.

I will monitor the other thread for any updates, thank you.

The not-thread-safe parts mentioned in the other thread are related to time library functions, not directly SNTP.  Are you suggesting moving SNTP to application_start()?  This is non-trivial as it will make the wifi search and sntp blocking at startup.

0 Likes

1. When the function sntp_start_auto_time_sync() is called, there is an initial random delay which is required to prevent thundering herd scenario as per RFC4330. That should explain the initial delay of 15-30 seconds after bringing up the Wi-Fi network.

2. You can enable WPRINT_ENABLE_LIB_DEBUG in wiced_defaults.h to enable debug printing in sntp.c. SNTP basically utilizes UDP protocol which is unreliable by design due to lack of ACK. You can consider increasing the number of NTP attempts defined by MAX_NTP_ATTEMPTS to 6 and check if that reduces the frequency of your issue. You can also print out the return value of wiced_udp_receive(&socket, &packet, WICED_NTP_REPLY_TIMEOUT) to check the error code. Also the return value of wiced_time_get_utc_time( &utc_time ) should be checked so you can print out the return value of this function and check if it is non-zero.