Skip to content

Commit

Permalink
WiFi sta connect fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dzurikmiroslav committed Feb 11, 2025
1 parent 9c4d67b commit 73afe3f
Showing 1 changed file with 49 additions and 30 deletions.
79 changes: 49 additions & 30 deletions components/network/src/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,22 @@ static esp_netif_t* ap_netif;

EventGroupHandle_t wifi_event_group;

static void sta_try_connect(void)
{
EventBits_t mode_bits = xEventGroupGetBits(wifi_event_group);
if (!(mode_bits & WIFI_STA_SCAN_BIT) && mode_bits & WIFI_STA_MODE_BIT) {
esp_err_t err = esp_wifi_connect();
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_wifi_connect returned 0x%x", err);
}
}
}

static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT) {
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
ESP_LOGI(TAG, "STA connected");
ESP_LOGI(TAG, "AP STA connected");
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*)event_data;
ESP_LOGI(TAG, "WiFi AP " MACSTR " join, AID=%d", MAC2STR(event->mac), event->aid);
xEventGroupClearBits(wifi_event_group, WIFI_AP_DISCONNECTED_BIT);
Expand All @@ -51,15 +62,11 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_
ESP_LOGI(TAG, "STA disconnected");
xEventGroupClearBits(wifi_event_group, WIFI_STA_CONNECTED_BIT);
xEventGroupSetBits(wifi_event_group, WIFI_STA_DISCONNECTED_BIT);
if (!(xEventGroupGetBits(wifi_event_group) & WIFI_STA_SCAN_BIT)) {
esp_wifi_connect();
}
sta_try_connect();
}
if (event_id == WIFI_EVENT_STA_START) {
ESP_LOGI(TAG, "STA start");
if (!(xEventGroupGetBits(wifi_event_group) & WIFI_STA_SCAN_BIT)) {
esp_wifi_connect();
}
sta_try_connect();
}
} else if (event_base == IP_EVENT) {
if (event_id == IP_EVENT_STA_GOT_IP || event_id == IP_EVENT_GOT_IP6) {
Expand Down Expand Up @@ -88,8 +95,25 @@ static esp_err_t wifi_restart(void)

EventBits_t mode_bits = xEventGroupGetBits(wifi_event_group);

// sta
wifi_config_t sta_config = {
wifi_mode_t mode;
if (mode_bits & WIFI_AP_MODE_BIT) {
mode = WIFI_MODE_APSTA; // STA is need for scan
} else if (mode_bits & WIFI_STA_MODE_BIT) {
mode = WIFI_MODE_STA;
} else {
mode = WIFI_MODE_NULL;
}

err = esp_wifi_set_mode(mode);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_wifi_set_mode returned 0x%x", err);
return err;
}

// STA config
if (mode_bits & (WIFI_AP_MODE_BIT | WIFI_STA_MODE_BIT)) {
// STA config is needed also in AP mode for scanning
wifi_config_t sta_config = {
.sta =
{
.pmf_cfg =
Expand All @@ -99,13 +123,19 @@ static esp_err_t wifi_restart(void)
},
},
};
if (mode_bits & WIFI_STA_MODE_BIT) {
wifi_get_ssid((char*)sta_config.sta.ssid);
wifi_get_password((char*)sta_config.sta.password);
if (mode_bits & WIFI_STA_MODE_BIT) {
wifi_get_ssid((char*)sta_config.sta.ssid);
wifi_get_password((char*)sta_config.sta.password);
}

err = esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_wifi_set_config returned 0x%x", err);
return err;
}
}
esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config);

// ap
// AP config
if (mode_bits & WIFI_AP_MODE_BIT) {
wifi_config_t ap_config = {
.ap =
Expand All @@ -118,22 +148,11 @@ static esp_err_t wifi_restart(void)
esp_wifi_get_mac(ESP_IF_WIFI_AP, mac);
sprintf((char*)ap_config.ap.ssid, AP_SSID, mac[3], mac[4], mac[5]);

esp_wifi_set_config(ESP_IF_WIFI_AP, &ap_config);
}

wifi_mode_t mode;
if (mode_bits & WIFI_AP_MODE_BIT) {
mode = WIFI_MODE_APSTA; // STA is need for scan
} else if (mode_bits & WIFI_STA_MODE_BIT) {
mode = WIFI_MODE_STA;
} else {
mode = WIFI_MODE_NULL;
}

err = esp_wifi_set_mode(mode);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_wifi_set_mode returned 0x%x", err);
return err;
err = esp_wifi_set_config(ESP_IF_WIFI_AP, &ap_config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_wifi_set_config returned 0x%x", err);
return err;
}
}

if (mode != WIFI_MODE_NULL) {
Expand Down

0 comments on commit 73afe3f

Please sign in to comment.