Search code examples
esp32

Espressif Esp32 Wifi Driver SoftAP+Sta mode ... set config interface?


The esp_wifi_set_config method requires an interface parameter of type wifi_interface_t. This an enum which has values [WIFI_IF_STA || WIFI_IF_AP]

I have my config merged from the two examples on the esp32 examples page with neccesary config params. But I am unsure what interface mode I am meant to pass from the above to set the config. I would expect a WIFI_IF_AP_STA or something.

wifi_config_t wifi_config = {
    .ap = {
        .ssid = ESP_AP_WIFI_SSID,
        .ssid_len = strlen(ESP_AP_WIFI_SSID),
        .channel = ESP_AP_WIFI_CHANNEL,
        .password = ESP_AP_WIFI_PASS,
        .max_connection = AP_MAX_STA_CONN,
        .authmode = WIFI_AUTH_WPA_WPA2_PSK
    },
    .sta = {
        .ssid = ESP_STA_WIFI_SSID,
        .password = ESP_STA_WIFI_PASS,
     .threshold.authmode = WIFI_AUTH_WPA2_PSK,

        .pmf_cfg = {
            .capable = true,
            .required = false
        },
    },
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK(esp_wifi_set_config(
    **WHAT DO I PUT HERE? [WIFI_IF_STA : WIFI_IF_AP]
    , &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());

Solution

  • Firstly, note that wifi_config_t is not a struct with two members. It's a union, meaning you can store either the AP or the STA configuration but not both. So you need to create two different configuration structures, one for each mode.

    As for your confusion, according to the sample here you call esp_wifi_set_config() twice - once for the AP and once for the STA.

    wifi_config_t ap_config = {
        .ap = {
            .ssid = ESP_AP_WIFI_SSID,
            .ssid_len = strlen(ESP_AP_WIFI_SSID),
            .channel = ESP_AP_WIFI_CHANNEL,
            .password = ESP_AP_WIFI_PASS,
            .max_connection = AP_MAX_STA_CONN,
            .authmode = WIFI_AUTH_WPA_WPA2_PSK
        }
    };
    wifi_config_t sta_config = {
        .sta = {
            .ssid = ESP_STA_WIFI_SSID,
            .password = ESP_STA_WIFI_PASS,
            .threshold.authmode = WIFI_AUTH_WPA2_PSK,
            .pmf_cfg = {
                .capable = true,
                .required = false
            }
        }
    };
    
    ...
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config));