Page 1 of 1

About wifi STA - AP mode

Posted: Tue Mar 19, 2024 2:06 am
by sangk82
hi,
I am beginner of ESP.
I was trying to use STA-AP mode.

I've understood how to use each mode seperately but I wanna know how to use STA-AP mode

here's a general code I found on internet.

Code: Select all

	// set APSTA mode
      ESP_ERROR_CHECK (esp_wifi_set_mode (WIFI_MODE_APSTA));
      
      	// configure AP_config and STA_config
      wifi_config_t wifi_ap_config = {
        .ap = {
               .ssid = SOFTAP_ESP_SSID,
               .ssid_len = strlen (SOFTAP_ESP_SSID),
               .channel = 1,.password = SOFTAP_ESP_PWD,
               .max_connection = 4,
               .authmode = WIFI_AUTH_WPA2_PSK,
               .pmf_cfg = {
                           .required = true,
                           }
               }
      };
      ESP_ERROR_CHECK (esp_wifi_set_config (WIFI_IF_AP, &wifi_ap_config));
            wifi_config_t wifi_sta_config = {
      };
      strcpy ((char *) wifi_sta_config.sta.ssid, __SSID);
      strcpy ((char *) wifi_sta_config.sta.password, __PWD);
      ESP_ERROR_CHECK (esp_wifi_set_config (ESP_IF_WIFI_STA, &wifi_sta_config));
      
      	// wifi start
      ESP_ERROR_CHECK (esp_wifi_start ());
set mode as APSTA and configure STA and AP each and start wifi.

but I tested
set mode APSTA and configure only one, STA or AP, and start wifi.
it works. but I don't know how and why.
it supposed to be configured STA and AP, but I don't know how it works STA or AP.
and it works exactly I expected.
( i try to make a function that working as AP for opening http server and connecting with wifi as STA )
if I only configured STA or AP, I believe it works only for STA or AP not both.

if there's something I am missing please tell me.

thank you so much.

I Found a Reason

Posted: Tue Mar 19, 2024 6:27 am
by sangk82
Hi, me again

I think I found a reason.

according to the comments of esp_wifi_set_config(),
the wifi config will be saved on NVS autometically.

that was the reason even tho I skip to put the AP mode config, it remained on NVS.
so it can open the server without setting information.

Thank you

Re: About wifi STA - AP mode

Posted: Tue Mar 19, 2024 10:54 am
by liaifat85
To ensure proper functionality in APSTA mode, you should provide configurations for both the STA and AP settings, even if you only intend to use one of them at a time.

Re: About wifi STA - AP mode

Posted: Fri Mar 22, 2024 12:17 am
by sangk82
Thank you for answering :)