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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.

3 Comments

  1. Elia says:

    I tried your code but when I compile it i get this error:

    user/user_main.c:22:23: error: storage size of ‘stationConf’ isn’t known

    struct station_config stationConf;

    Do you have any idea what to do?

  2. Elia says:

    I now found out that this is a problem with my .h file
    Coud you upload the content of your .h file?

  3. new299 says:

    Hi Elia, the .h is the standard one from the SDK examples it contains the following 2 lines:

    #define SSID “SSIDNAME”
    #define SSID_PASSWORD “SSIDPASSWORD”

    You should obviously fill in your own access point details. You can also just include these define’s in your main C file.

Leave a Reply