[Solved] sntp issue

doswork
Posts: 6
Joined: Wed May 20, 2015 3:20 pm

[Solved] sntp issue

Postby doswork » Wed Mar 16, 2016 11:22 am

I call sntp_stop after getting the time. That's ok when the router is connecting with Internet. When the router is disconnected from Internet, sntp_get_current_timestamp() always print "please start sntp first" and return 0. I call sntp_stop and set a timer to restart ntp. Then my application crash.

Code: Select all

Fatal exception 28(LoadProhibitedCause):
epc1=0x40226769, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000012, depc=0x00000000

Is there something wrong after calling sntp_stop? How can I stop sntp safely?

Code: Select all

#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "driver/uart.h"
#include "mem.h"

LOCAL os_timer_t sntp_timer;
LOCAL uint32 ntp_retry_counter = 0;
LOCAL void user_start_ntp(void);

void ICACHE_FLASH_ATTR user_check_sntp_stamp(void *arg)
{
   uint32 current_stamp;
   current_stamp = sntp_get_current_timestamp();
   if(current_stamp == 0)
   {
      if(++ntp_retry_counter < 200)
         os_timer_arm(&sntp_timer, 100, 0);
      else
      {
         sntp_stop();    // ??
         os_printf("sleep...\n");
         os_timer_disarm(&sntp_timer);
         os_timer_setfn(&sntp_timer, (os_timer_func_t *)user_start_ntp, NULL);
         os_timer_arm(&sntp_timer, 10*1000, 0);
      }
   }
   else
   {
      os_printf("sntp: %d, %s \n", current_stamp, sntp_get_real_time(current_stamp));
      sntp_stop();
      os_timer_disarm(&sntp_timer);
      os_timer_setfn(&sntp_timer, (os_timer_func_t *)user_start_ntp, NULL);
      os_timer_arm(&sntp_timer, 10*1000, 0);
   }
}

LOCAL void ICACHE_FLASH_ATTR
user_start_ntp(void)
{
   os_printf("%s\n", __func__);
   ip_addr_t *addr = (ip_addr_t *)os_zalloc(sizeof(ip_addr_t));
   sntp_setservername(0, "us.pool.ntp.org"); // set server 0 by domain name
   sntp_setservername(1, "ntp.sjtu.edu.cn"); // set server 1 by domain name
   ipaddr_aton("210.72.145.44", addr);
   sntp_setserver(2, addr); // set server 2 by IP address
   sntp_init();
   os_free(addr);
   ntp_retry_counter = 0;
   os_timer_setfn(&sntp_timer, (os_timer_func_t *)user_check_sntp_stamp, NULL);
   os_timer_arm(&sntp_timer, 100, 0);
}

LOCAL void ICACHE_FLASH_ATTR
user_ntp_checkip_cb(void)
{
   os_printf("%s\n", __func__);
   os_timer_disarm(&sntp_timer);
   if(wifi_station_get_connect_status() != STATION_GOT_IP)
   {
      os_timer_setfn(&sntp_timer, (os_timer_func_t *)user_ntp_checkip_cb, NULL);
      os_timer_arm(&sntp_timer, 500, 0);
   }
   else
   {
      user_start_ntp();
   }
}

LOCAL void ICACHE_FLASH_ATTR
user_start(void)
{
   os_printf("%s\n", __func__);
   wifi_station_connect();
   os_timer_disarm(&sntp_timer);
   os_timer_setfn(&sntp_timer, (os_timer_func_t *)user_ntp_checkip_cb, NULL);
   os_timer_arm(&sntp_timer, 500, 0);
}

void user_rf_pre_init(void)
{
}

void user_init(void)
{
   uart_init(9600, 115200);
   os_printf("SDK version:%s\n", system_get_sdk_version());
   wifi_set_opmode(STATION_MODE);
   struct station_config sta_config;
   os_memset(&sta_config, 0, sizeof(sta_config));
   os_sprintf(sta_config.ssid, "%s", "WiFiTest");
   os_sprintf(sta_config.password, "%s", "12345678");
   wifi_station_set_config(&sta_config);
   system_init_done_cb(user_start);
}

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: sntp issue

Postby ESP_Faye » Wed Mar 16, 2016 8:09 pm

Hi,

Please try this:

Code: Select all

void ICACHE_FLASH_ATTR user_check_sntp_stamp(void *arg)
{
   uint32 current_stamp;
   current_stamp = sntp_get_current_timestamp();
   if(current_stamp == 0)
   {
      if(++ntp_retry_counter < 200)
         os_timer_arm(&sntp_timer, 100, 0);
      else
      {
   
        sntp_setservername(0, NULL);
       sntp_setservername(1, NULL);
       sntp_setserver(2, NULL);
         sntp_stop();    // ??
         os_printf("sleep...\n");
      
         os_timer_disarm(&sntp_timer);
         os_timer_setfn(&sntp_timer, (os_timer_func_t *)user_start_ntp, NULL);
         os_timer_arm(&sntp_timer, 10*1000, 0);
      }
   }
   else
   {
      os_printf("sntp: %d, %s \n", current_stamp, sntp_get_real_time(current_stamp));
      sntp_stop();
      os_timer_disarm(&sntp_timer);
      os_timer_setfn(&sntp_timer, (os_timer_func_t *)user_start_ntp, NULL);
      os_timer_arm(&sntp_timer, 10*1000, 0);
   }
}

doswork
Posts: 6
Joined: Wed May 20, 2015 3:20 pm

Re: sntp issue

Postby doswork » Wed Apr 06, 2016 8:50 pm

It works fine. Thanks.

Who is online

Users browsing this forum: Google [Bot] and 21 guests