


We suggest to use RTOS SDK which is multi-threaded.
ESP8266 SDK without RTOS is single-threaded,so using while(1) or delay_ms() (which is also a while in it) may occupy CPU too long and cause a watchdog reset.
1. If a task occupied CPU for a long time, ESP8266 can't feed the dog, will cause a watchdog reset;
2. Task should not occupy CPU more than 10 ms, otherwise may cause WiFi connection break.
Users have to use a timer to do the checking job, as user_esp_platform_check_ip below.
Notice: os_timer_setfn has to be called to set a callback when using timer.
Code: Select all
LOCAL os_timer_t test_timer;
/******************************************************************************
* FunctionName : user_esp_platform_check_ip
* Description : check whether get ip addr or not
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_esp_platform_check_ip(void)
{
struct ip_info ipconfig;
//disarm timer first
os_timer_disarm(&test_timer);
//get ip info of ESP8266 station
wifi_get_ip_info(STATION_IF, &ipconfig);
if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) {
os_printf("got ip !!! \r\n");
} else {
if ((wifi_station_get_connect_status() == STATION_WRONG_PASSWORD ||
wifi_station_get_connect_status() == STATION_NO_AP_FOUND ||
wifi_station_get_connect_status() == STATION_CONNECT_FAIL)) {
os_printf("connect fail !!! \r\n");
} else {
//re-arm timer to check ip
os_timer_setfn(&test_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL);
os_timer_arm(&test_timer, 100, 0);
}
}
}
/******************************************************************************
* FunctionName : user_set_station_config
* Description : set the router info which ESP8266 station will connect to
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_set_station_config(void)
{
// Wifi configuration
char ssid[32] = SSID;
char password[64] = PASSWORD;
struct station_config stationConf;
//need not mac address
stationConf.bssid_set = 0;
//Set ap settings
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 64);
wifi_station_set_config(&stationConf);
//set a timer to check whether got ip from router succeed or not.
os_timer_disarm(&test_timer);
os_timer_setfn(&test_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL);
os_timer_arm(&test_timer, 100, 0);
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void user_init(void)
{
os_printf("SDK version:%s\n", system_get_sdk_version());
//Set softAP + station mode
wifi_set_opmode(STATIONAP_MODE);
// ESP8266 connect to router.
user_set_station_config();
}
1.在 非OS SDK 由于是单线程的,任何 task 都不能长期 占用 CPU。
(1)如果 一个 task 占用 CPU 不退出,将导致看门狗的喂狗函数无法执行,导致系统重启。
(2) 建议 一个 task 占用 CPU 不要超过 10mS,否则可能导致 WiFi 连接中断。
2. 建议用定时器来实现长时间的查询功能,定时器也可设置为循环调用,参考代码如上。请注意,使用 timer 时,必须调用os_timer_setfn 设置 timer 到时的 callback。
3. 建议用RTOS SDK,OS 会调度不同 task。每个 task 编程可认为独占 CPU。
Thanks for your interest in ESP8266 !