tonyp7 / esp32-wifi-manager

Captive Portal for ESP32 that can connect to a saved wireless network or start an access point where you can connect to existing wifis.
MIT License
648 stars 214 forks source link

Connect to new AP while already connected error #131

Open pablacho opened 3 years ago

pablacho commented 3 years ago

Prerequisites

Description

While connected to an access point, it is not possible to change to another one unless I disconnect from the first one

Steps to Reproduce

1) Connect module to wifi SSID "A" 2) Find the server page inside "A" network, shows connected to wifi SSID "A" 3) Try to change credentials to connect to SSID "B"

System Configuration

FreeRTOS with custom app running, forked esp32-wifi-manager with changes due to app, recently rebased and issue started right after. The mod only affects information loaded by user and stored in NVM. The issue seems to be here:

            case WM_ORDER_CONNECT_STA:
                ESP_LOGI(TAG, "MESSAGE: ORDER_CONNECT_STA");

                /* very important: precise that this connection attempt is specifically requested.
                 * Param in that case is a boolean indicating if the request was made automatically
                 * by the wifi_manager.
                 * */
                if((BaseType_t)msg.param == CONNECTION_REQUEST_USER) {
                    xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_STA_CONNECT_BIT);
                }
                else if((BaseType_t)msg.param == CONNECTION_REQUEST_RESTORE_CONNECTION) {
                    xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
                }

                uxBits = xEventGroupGetBits(wifi_manager_event_group);
                if( ! (uxBits & WIFI_MANAGER_WIFI_CONNECTED_BIT) ){
                    /* update config to latest and attempt connection */
                    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_manager_get_wifi_sta_config()));

                    /* if there is a wifi scan in progress abort it first
                       Calling esp_wifi_scan_stop will trigger a SCAN_DONE event which will reset this bit */
                    if(uxBits & WIFI_MANAGER_SCAN_BIT){
                        esp_wifi_scan_stop();
                    }
                    ESP_ERROR_CHECK(esp_wifi_connect());
                }

                /* callback */
                if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);

                break;

while connected, WIFI_MANAGER_WIFI_CONNECTED_BIT is set so it never enters and the web application just hangs as never gets an OK/NOK answer.