esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

StefanRvO
Posts: 16
Joined: Sun Mar 06, 2016 9:25 pm

esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby StefanRvO » Wed Feb 15, 2017 5:44 pm

Hi
I'm running my ESP32 in simultaneous access point mode and station mode.
The idea is that a user can connect to the devices access point, and from a web interface select which access point the esp should connect to.
However, i have problems when running esp_wifi_connect() while hosting an access point. When the esp tries to connect, it will at the same time temporarily stop hosting its own access point. Is this expected behaviour and is there something which could be done to work around this issue? The access point works correctly if the esp is successfully connected as a station or if it does not attempt to connect.
It should be possible to mitigate the issue somewhat by not trying to reconnect immediately after a failed attempt. This is however suboptimal.

The relevant code is listed below

Code: Select all

void WifiHandler::initialise_wifi(void)
{
    tcpip_adapter_init();
    WifiHandler::wifi_event_group = xEventGroupCreate();
    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );

    //Set up wifi access point configuration
    wifi_config_t ap_config;
    uint8_t ap_ssid[32] = "ESP32_AP\0";
    uint8_t ap_pass[64] = "\0";
    memcpy(ap_config.ap.ssid, ap_ssid, 32);
    memcpy(ap_config.ap.password, ap_pass, 64);
    ap_config.ap.ssid_len = 0;  //ONLY USED IF NOT 0 teminated.
    ap_config.ap.authmode = WIFI_AUTH_OPEN;
    ap_config.ap.ssid_hidden = 0;
    ap_config.ap.max_connection = 20;
    ap_config.ap.beacon_interval = 100;

    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_APSTA) ); //Configure as both accesspoint and station
    ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_AP, &ap_config));
    this->update_station_config();
    ESP_ERROR_CHECK( esp_wifi_start() );
}

Code: Select all

void WifiHandler::update_station_config()
{
    //Set up wifi station configuration.

    wifi_config_t wifi_config = {
        .sta = {
            "",
            "",
            false,
            "",
        },
    };

    //Retrieve setting from NVS
    size_t len_passwd = 64;
    size_t len_ssid = 32;
    esp_err_t err = s_handler->nvs_get("STA_SSID", (char *)wifi_config.sta.ssid, &len_ssid);
    printf("%d\n", err);
    err = s_handler->nvs_get("STA_PASSWORD", (char *)wifi_config.sta.password, &len_passwd);
    printf("%d\n", err);

    ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
    ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    //ESP_ERROR_CHECK( esp_wifi_set_auto_connect(true) );
    ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
}

Code: Select all

esp_err_t WifiHandler::event_handler(void *ctx, system_event_t *event)
{
    switch (event->event_id) {
    case SYSTEM_EVENT_STA_START:
        esp_wifi_connect();
        break;
    case SYSTEM_EVENT_STA_GOT_IP:
        xEventGroupSetBits(WifiHandler::wifi_event_group, CONNECTED_BIT);
        break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
        /* This is a workaround as ESP32 WiFi libs don't currently
           auto-reassociate. */
        esp_wifi_connect();
        xEventGroupClearBits(WifiHandler::wifi_event_group, CONNECTED_BIT);
        break;
    case SYSTEM_EVENT_AP_STACONNECTED:
        printf("station connected to access point. Now listing connected stations!\n");
        wifi_sta_list_t sta_list;
        ESP_ERROR_CHECK( esp_wifi_ap_get_sta_list(&sta_list));
        for(int i = 0; i < sta_list.num; i++)
        {
            //Print the mac address of the connected station
            wifi_sta_info_t &sta = sta_list.sta[i];
            printf("Station %d MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", i,
                sta.mac[0], sta.mac[1], sta.mac[2], sta.mac[3], sta.mac[4], sta.mac[5]);
        }

    default:
        break;
    }
    return ESP_OK;
}


User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby kolban » Thu Feb 16, 2017 5:32 am

If I am reading correctly, if the ESP32 is being an access point and a station ... if it's station interface is not connected, it will happily be an access point to host new station connections. However, if we try and perform a connection to an access point through the station interface on the ESP32 then for the duration of the connection setup, the ESP32 won't respond/accept new station connections as an access point?

Is that the summary of it?

Your question is ... is this what one should expect?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby ESP_igrr » Thu Feb 16, 2017 10:38 am

This is expected behaviour I think, and the same happens on the ESP8266.
In order to connect to an AP as a station, the chip needs to scan channels. Because the chip can only work on one channel at any given moment, this will cause loss of connection on the channel where other stations are expecting to see the ESP SoftAP.

If the channel of the external AP is different from the initial channel of the ESP32 SoftAP, ESP32 will change the channel of SoftAP to match the channel of the external AP (so it can run as both AP and STA on the same channel). This will cause external devices connected to the ESP32 to temporarily loose connection (until they do a new scan and reconnect to the SoftAP on the new channel).

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby WiFive » Thu Feb 16, 2017 10:45 am

ESP_igrr wrote:If the channel of the external AP is different from the initial channel of the ESP32 SoftAP, ESP32 will change the channel of SoftAP to match the channel of the external AP (so it can run as both AP and STA on the same channel). This will cause external devices connected to the ESP32 to temporarily loose connection (until they do a new scan and reconnect to the SoftAP on the new channel).
So will sta connect scan routine check the current channel first or always start with lowest channel? If the ap to connect is found on the current channel will this avoid disconnection of ap clients?

HardwireIO
Posts: 10
Joined: Mon Mar 06, 2017 5:34 pm

Re: esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby HardwireIO » Thu May 04, 2017 3:53 pm

We are facing absolutely the same problem.
If wifi configuration is not valid, or in general ESP32 cannot connect to a wifi network, we launch an hybrid mode (WIFI_MODE_APSTA), trying to periodically connect as a station to the desired wifi network, and in the meantime launching an access point in order to let user connect and change wifi settings.

Once ESP32 tries to connect to a Wifi network, it stops working on AP, disconnecting all users.

This leads to have a useless WIFI_MODE_APSTA.
If I want to try to connect to a wifi network, and have access point working, how can I do that?
Hardwire.io / The IoT Platform

User avatar
iot-bits.com
Posts: 25
Joined: Wed Dec 21, 2016 6:14 am
Location: India
Contact:

Re: esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby iot-bits.com » Fri Sep 15, 2017 6:41 am

The WiFi radio is the same for both the station and AP interface, that's why it is a softAP. So when the station is tuned to a certain frequency, the AP s forced to be tuned to the exact same frequency.
Now if you were to restrict the station to one channel only, the AP will not be disrupted. But switching channels switches channel on the whole radio, thus disconnecting the AP.
- Pratik
:geek: Just another hobbyist and consultant

forkus2000
Posts: 5
Joined: Wed Nov 08, 2017 6:50 pm

Re: esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby forkus2000 » Tue Jan 30, 2018 5:35 pm

Nobody of espressif respond to this issue ?
I am very sad with espressif, i have posted many times this issue and nobody respond, this talk very bad form the engenieer of espressif... I send email to all the crew of espressif and nobody respond..

I have the same issue i try to connect to wifi sta mode + ap mode to receive esp_now data !!! when conect sta mode ap mode stop to receive esp_now data !!!!

Very sad !!!!!

ESP_Sprite
Posts: 9723
Joined: Thu Nov 26, 2015 4:08 am

Re: esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby ESP_Sprite » Wed Jan 31, 2018 2:22 am

Erm, what do you want an Espressif engineer to tell you, aside from what iggr already said? There's only one radio in the ESP32, it's impossible to listen on two channels simultaneously.

Sergey
Posts: 2
Joined: Thu May 24, 2018 1:09 pm

Re: esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby Sergey » Thu Aug 30, 2018 6:05 am

If I understand Ok, mode WIFI_MODE_APSTA possible to use when IP address used part of STA and part of SoftAP is identical.
Another words, you can't use this mode with two different networks simultaneously.
STA part is 192.168.1.102 SoftAP 192.168.1.102 - possible.
For example STA part IP is 192.168.1.102 SoftAP 10.0.0.201 - impossible.
It's pity.

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: esp_wifi_connect() while running in WIFI_MODE_APSTA causes access point to close

Postby Ritesh » Wed May 15, 2019 7:43 am

Hi All,

I just want to clarify that AP connection will be dropped while working with AP+STA mode as per my understanding.

Let me give on example in which we are doing below stuffs as per our requirement.

1) Set AP configuration with channel as 6
2) Set STA configuration as per requirement
3) Set AP+STA mode
4) Start WiFi
5) Connect WiFi as per configuration provided
6) Connect one mobile to ESP32 Device into AP Mode with creating socket as well
7) wait for STA to connect with configured router
8) Then update AP+STA configuration with just changing STA configuration and keep AP configuration as it is

So, I just want to clarify that existing AP mode connection will be re-connected with mobile when ESP32 STA is trying to connect with configured router based on different channel switching?

ESP32 AP will also switch to different channel when ESP32 STA will try to connect configured router with all channels till it will not be connected successfully.

Please confirm same from your end and let me know if I have misunderstood it.
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: Majestic-12 [Bot] and 88 guests