A few questions and things I've noticed about Wifi.

Resch2061
Posts: 40
Joined: Mon May 01, 2017 1:56 pm

A few questions and things I've noticed about Wifi.

Postby Resch2061 » Thu Sep 13, 2018 10:40 am

Hello again,

So as you may have guessed from the title of this topic, I have a few questions and some other oddities I've noticed about the wifi system.

First, I'm wondering about the return codes of esp_wifi_connect.
The doxygen for esp_wifi_connect mentions a return code ESP_ERR_WIFI_SSID, describing the SSID you are connecting to being invalid:

Code: Select all

/**
  * @brief     Connect the ESP32 WiFi station to the AP.
  *
  * @attention 1. This API only impact WIFI_MODE_STA or WIFI_MODE_APSTA mode
  * @attention 2. If the ESP32 is connected to an AP, call esp_wifi_disconnect to disconnect.
  *
  * @return 
  *    - ESP_OK: succeed
  *    - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  *    - ESP_ERR_WIFI_NOT_START: WiFi is not started by esp_wifi_start
  *    - ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong
  *    - ESP_ERR_WIFI_SSID: SSID of AP which station connects is invalid
  */
esp_err_t esp_wifi_connect(void);
So I just tested this with a bogus SSID which doesn't exist at all, using the console system to switch the SSID's around:

Code: Select all

comms wifi ssid=bla pw=bla
I (68600) WIFI: Attempting to connect to network bla...
I (68602) WIFI: Disconnecting from current network... // Already connected to some other network, I'll show the code later.
I (68619) wifi: state: run -> init (0)
I (68621) wifi: pm stop, total sleep time: 62646848 us / 66897955 us

I (68622) wifi: n:6 0, o:6 0, ap:255 255, sta:6 0, prof:1
I (68634) COMMS: Sta Disconnected // This is printed by the event handler I've defined.
I (68736) MQTT: We lost connection!
I (68737) MQTTmbedtls: NetworkDisconnect
W (68741) MQTT: Reconnecting MQTT, attempt 1 of 2...
I (68741) MQTT: MQTTClientInit (attempt 1 of 2)...
I (68752) MQTT: Sleeping until there is a network connection (Max 30s)
I (69635) WIFI: Waiting for ip address (15 sec)...
I (72044) COMMS: Sta Disconnected
E (84636) WIFI: Timeout.
So as you can see, the wifi system properly disconnects and then attempts to connect to this SSID. But there's no mention of an improper SSID, see the code:

Code: Select all

esp_err_t my_wifi_connect(const char* ssid, int ssid_len, const char* pw, int pw_len)
{
    ESP_LOGI(TAG, "Attempting to connect to network %s...", ssid);
    wifi_config_t conf = { 0 };
    conf.sta.bssid_set = false;

    wifi_ap_record_t curr_station;
    esp_err_t        err = esp_wifi_sta_get_ap_info(&curr_station);
    // check if ESP currently has connection already
    if (err == ESP_OK)
    {
        // already connected with requested SSID
        if (strcmp((const char*)curr_station.ssid, ssid) == 0)
        {
            ESP_LOGI(TAG, "Already connected to network %s", ssid);
            return ESP_OK;
        }
        else
        {
            // not connected with requested SSID
            if (strlen((char*)curr_station.ssid) > 0)
            {
                ESP_LOGI(TAG, "Disconnecting from current network...");
                esp_wifi_disconnect();
                WAIT(1000); // give the event task time to send the appropriate events - vTaskDelay macro wrapper
            }
        }
    }

    wificonf_copy_credentials(&conf, false, ssid, ssid_len, pw, pw_len);
    CHECK_ERR(TAG, esp_wifi_set_config(WIFI_IF_STA, &conf));
    err = esp_wifi_connect();
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "%s wifi connect err %d", __func__, err); // Where is ESP_ERR_WIFI_SSID?
    }
    return err;
}
So there's no logging message about an invalid SSID, as I would have expected. Perhaps this is an oversight of some kind?
EDIT:
Putting the log output in debug gives me the following:

Code: Select all

D (31008) event: SYSTEM_EVENT_STA_DISCONNECTED, ssid:bla, ssid_len:3, bssid:00:00:00:00:00:00, reason:201
D (31009) tcpip_adapter: if0 start ip lost tmr: enter
D (31020) tcpip_adapter: if0 start ip lost tmr: already started
The second thing is that sometimes the ESP32 simply refuses to connect to an AP until you restart the entire system, even when you have de-inited Wifi and re-inited, and you're trying to connect to an AP you have already been connected to in the past, so you know the SSID and password are correct.
We are running this software on a device that is meant to be online as much as possible, and sometimes the connection simply fails. When this happens, a monitoring task attempts to reconnect the Wifi to the given SSID say, 6 times. If after these 6 attempts there is still no connection with the SSID, the task will de-init and reinit the Wifi system entirely and attempt again. Most of the time this will not work, and then the task software-restarts the ESP32. And then it does work. I would expect de-initializing the wifi system through esp_wifi_deinit to work with these function calls:

Code: Select all

    
    esp_wifi_disconnect();
    esp_wifi_stop();
    esp_wifi_deinit();
    vTaskDelay(2500 / portTICK_PERIOD_MS); // to give the event task time to send the appropriate events
    // esp_wifi_init, set_storage, set_mode, start, etc.

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: A few questions and things I've noticed about Wifi.

Postby fly135 » Thu Sep 13, 2018 3:34 pm

Resch2061 wrote:So I just tested this with a bogus SSID which doesn't exist at all, using the console system to switch the SSID's
This is the error you got.... WIFI_REASON_NO_AP_FOUND. i.e. reason:201. Were you expecting something else?

Code: Select all

D (31008) event: SYSTEM_EVENT_STA_DISCONNECTED, ssid:bla, ssid_len:3, bssid:00:00:00:00:00:00, reason:201

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: A few questions and things I've noticed about Wifi.

Postby fly135 » Thu Sep 13, 2018 3:36 pm

The second thing is that sometimes the ESP32 simply refuses to connect to an AP until you restart the entire system, even when you have de-inited Wifi and re-inited, and you're trying to connect to an AP you have already been connected to in the past, so you know the SSID and password are correct.
What is the reason code you are getting when this happens?

Resch2061
Posts: 40
Joined: Mon May 01, 2017 1:56 pm

Re: A few questions and things I've noticed about Wifi.

Postby Resch2061 » Fri Sep 14, 2018 8:42 am

fly135 wrote:This is the error you got.... WIFI_REASON_NO_AP_FOUND. i.e. reason:201. Were you expecting something else?

Code: Select all

D (31008) event: SYSTEM_EVENT_STA_DISCONNECTED, ssid:bla, ssid_len:3, bssid:00:00:00:00:00:00, reason:201
Well, I was expecting the function esp_wifi_connect() to return an error code. From what I've seen so far, it always returns ESP_OK no matter what.

As for the second thing, I just tested it again today, can't seem to reproduce it.

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: A few questions and things I've noticed about Wifi.

Postby fly135 » Fri Sep 14, 2018 4:25 pm

I'm guessing that the call to connect returns before is actual finds out that it can't find the AP.
As for the second thing, I just tested it again today, can't seem to reproduce it.
I have experienced connection issues with connecting to WPA2. The reason code is 203. It will get a random number of these error codes before it connects (assuming it does). I have several APs in my house and it appears to happen mostly (or maybe only) with a Trendnet TEW432. That could be the issue you are seeing with failing to connect. My queries here indicate no one else is seeing this 203 issue. But it could be that it happens infrequently enough for most people to not see it.

John A

Fromen
Posts: 4
Joined: Sat Nov 27, 2021 10:49 pm

Re: A few questions and things I've noticed about Wifi.

Postby Fromen » Wed Dec 08, 2021 5:20 am

An answer to the first question about ESP_ERR_WIFI_SSID, in case anyone wants to know (I was curious myself). esp_wifi_connect() only checks whether the SSID is plausibly valid, it does NOT check whether there's an access point with that name around. So when you tried connecting to a network with SSID of "bla", that's a perfectly valid SSID even if there's no AP around with that name, so it returned ESP_OK

ESP_ERR_WIFI_SSID will be returned if the SSID is impossible. For example, setting the SSID to nothing "" returns ESP_ERR_WIFI_SSID, because it's impossible for the SSID to be nothing. esp_wifi_connect() may also check for invalid characters, but I've not been able to confirm that.

If you want to know whether the ESP32 successfully connected to an AP, you'll have to look into WiFi event handling, specifically WIFI_EVENT_STA_CONNECTED and WIFI_EVENT_STA_DISCONNECTED. The former will be called after a successful connection, the latter after a timeout period (or something else, such as the AP saying the password is wrong). There's very thorough documentation of WiFi events and more available here, though I didn't find it very friendly as a beginner: https://docs.espressif.com/projects/esp ... /wifi.html

Something a bit more beginner friendly would be the station example provided with the IDF: https://github.com/espressif/esp-idf/bl ... ple_main.c You can see the event handler at the top, which looks at those events to determine whether connection was successful.

Hope this helps!

Who is online

Users browsing this forum: No registered users and 201 guests