esp8266 wifi doesn’t connect
*SEE UPDATE BELOW*
I spent quite some time debugging an issue where the wifi wouldn’t connect in station mode on the esp8266 today. wifi_station_get_connect_status() would return either 1 or 3 (which means CONNECTING or AP_NOT_FOUND). The issue /appears/ to be that wifi_station_set_config does not work reliably when called outside user_init (even if called from a function called from user_init. I.e.
void ICACHE_FLASH_ATTR wifi_config() { // Wifi configuration char ssid[32] = SSID; char password[64] = SSID_PASSWORD; struct station_config stationConf; //Set station mode wifi_set_opmode( 0x1 ); //Set ap settings os_memcpy(&stationConf.ssid, ssid, 32); os_memcpy(&stationConf.password, password, 64); wifi_station_set_config(&stationConf); //wifi_station_connect(); // should not be required debug("init wifi"); debug(SSID); debug(SSID_PASSWORD); } //Init function void ICACHE_FLASH_ATTR user_init() { // Set UART Speed (default appears to be rather odd 77KBPS) uart_init(BIT_RATE_9600,BIT_RATE_9600); wifi_config(); //Start os task system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); system_os_post(user_procTaskPrio, 0, 0 ); network_init(); }
Does NOT work consistantly. Whereas:
//Init function void ICACHE_FLASH_ATTR user_init() { // Set UART Speed (default appears to be rather odd 77KBPS) uart_init(BIT_RATE_9600,BIT_RATE_9600); //wifi_config(); // Wifi configuration char ssid[32] = SSID; char password[64] = SSID_PASSWORD; struct station_config stationConf; //Set station mode wifi_set_opmode( 0x1 ); //Set ap settings os_memcpy(&stationConf.ssid, ssid, 32); os_memcpy(&stationConf.password, password, 64); wifi_station_set_config(&stationConf); //wifi_station_connect(); // should not be required debug("init wifi"); debug(SSID); debug(SSID_PASSWORD); //Start os task system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); system_os_post(user_procTaskPrio, 0, 0 ); network_init(); }
Works as expected. The reason that wifi_config() above may sometimes work is that I would guess it occasionally gets inlined. I’ve no idea why the calls need to be in user_init directly though, there’s perhaps some funky stack related stuff going on.
UPDATE
Wow, Espressif saw my post and emailed me out of the blue. There suggestion was that stationConf.bssid_set should also be set to 0 in this case. Otherwise it will also check the AP mac address i.e it should be:
void wifi_config() { // Wifi configuration char ssid[32] = SSID; char password[64] = PASSWORD; struct station_config stationConf; //Set station mode wifi_set_opmode( 0x1 ); stationConf.bssid_set = 0; //Set ap settings os_memcpy(&stationConf.ssid, ssid, 32); os_memcpy(&stationConf.password, password, 64); wifi_station_set_config(&stationConf); } //Init function void user_init() { wifi_config(); }
This also makes sense to me in terms of random fiddling fixed this issue, I guess in some cases the uninitialized memory is 0 by chance.