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);
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.
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;
}
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
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.