wifi connect

FrankHald
Posts: 1
Joined: Sat Mar 25, 2023 10:03 pm

wifi connect

Postby FrankHald » Sun Mar 26, 2023 8:44 pm

Can anyone explain, why I can not connect with this code as is, but I can, if I comment out the lines
int idx_best = wifi_scan(networks, 3);
ESP_LOGI(TAG, "Best AP: %d", idx_best);
and dont do a scan before connecting. The scan works fine and returns the index of the AP in my struct array with the best signal.

My main.c:

Code: Select all

#include "Wifi.h"

esp_err_t getMacAddress(char* mac, int len) {

    uint8_t mac_byte_buffer[6] = {0};

    esp_err_t status = esp_efuse_mac_get_default(mac_byte_buffer);

    if (ESP_OK == status) {
        snprintf(mac, len, "%02X%02X%02X%02X%02X%02X", 
            mac_byte_buffer[0],
            mac_byte_buffer[1],
            mac_byte_buffer[2],
            mac_byte_buffer[3],
            mac_byte_buffer[4],
            mac_byte_buffer[5]);
    }

    return status;
}

void connect_to_best_wifi(void) {

    network networks[3];
    esp_err_t status = ESP_OK;

Here I am hardcoding my 3 AP's in my struct array networks


    wifi_init_sta();

    int idx_best = wifi_scan(networks, 3);
    ESP_LOGI(TAG, "Best AP: %d", idx_best);

    if (ESP_OK == status) {
        wifi_begin(networks[1].ssid, networks[1].pwd);
    }
}

void app_main(void)
{

    esp_err_t status = ESP_OK;
    //Initialize NVS
    status |= nvs_flash_init();
    if (status == ESP_ERR_NVS_NO_FREE_PAGES || status == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      status = nvs_flash_init();
    }
    ESP_ERROR_CHECK(status);

    char mac[13];
    status |= getMacAddress(mac, sizeof(mac));
    if (ESP_OK == status) {
        ESP_LOGI(TAG, "Mac address: %s", mac);
    }

    connect_to_best_wifi();

}

My wifi.c:

Code: Select all

#include "Wifi.h"

static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
    if (WIFI_EVENT == event_base) {
        if (WIFI_EVENT_STA_START == event_id) {
            wifi_started = true;
        } else if (WIFI_EVENT_STA_DISCONNECTED == event_id) {
            if (retries < MAX_CONNECT_RETRIES) {
                esp_wifi_connect();
                retries++;
                ESP_LOGI(TAG, "retry to connect to the AP");
            } else {
                xEventGroupSetBits(wifi_event_group, WIFI_FAIL_BIT);
            }
            ESP_LOGI(TAG,"connect to the AP fail");
        } else if (WIFI_EVENT_SCAN_DONE == event_id) {
            scanning = false;
            ESP_LOGI(TAG,"Scan finished");
        }
    } else if (IP_EVENT == event_base) {
        if (IP_EVENT_STA_GOT_IP == event_id) {
            ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
            ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
            retries = 0;
            xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
        }
    }
}


void wifi_init_sta(void)
{
    wifi_event_group = xEventGroupCreate();

    ESP_ERROR_CHECK(esp_netif_init());

    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_sta();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    esp_event_handler_instance_t instance_any_id;
    esp_event_handler_instance_t instance_got_ip;
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_any_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_got_ip));

    ESP_ERROR_CHECK(esp_wifi_start());
}

esp_err_t wifi_begin(char* ssid, char* password) {
    esp_err_t status = ESP_FAIL;
    wifi_config_t wifi_config;


    strcpy((char*)(wifi_config.sta.ssid), ssid);
    strcpy((char*)(wifi_config.sta.password), password);

    wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
    wifi_config.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH;

    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));

    ESP_LOGI(TAG, "wifi_init_sta finished.");

    esp_wifi_connect();

    EventBits_t bits = xEventGroupWaitBits(wifi_event_group, // Waiting until either the connection is established (WIFI_CONNECTED_BIT)
            WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, // or connection failed for the maximum number of re-tries (WIFI_FAIL_BIT)
            pdFALSE,
            pdFALSE,
            portMAX_DELAY);

    if (bits & WIFI_CONNECTED_BIT) {
        ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", ssid, password);
        status = ESP_OK;
        wifi_connected = true;
    } else if (bits & WIFI_FAIL_BIT) {
        ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", ssid, password);
        wifi_connected = false;
    } else {
        ESP_LOGE(TAG, "UNEXPECTED EVENT");
    }

    return status;
}

int wifi_scan(network* networks, int len) {
    uint16_t number = SCAN_LIST_SIZE;
    wifi_ap_record_t ap_info[SCAN_LIST_SIZE];
    uint16_t ap_count = 0;
    esp_err_t status = ESP_OK;
    int best_idx = -1;
    int best_signal = -100;

    scanning = true;

    memset(ap_info, 0, sizeof(ap_info));

    status |= esp_wifi_scan_start(NULL, true);
    if (ESP_OK == status) {
        status |= esp_wifi_scan_get_ap_records(&number, ap_info);
    }
    if (ESP_OK == status) {
        status |= esp_wifi_scan_get_ap_num(&ap_count);
    }
    ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);

    for (int i = 0; (i < SCAN_LIST_SIZE) && (i < ap_count); i++) {
        ESP_LOGI(TAG, "SSID \t\t%s", ap_info[i].ssid);
        ESP_LOGI(TAG, "RSSI \t\t%d", ap_info[i].rssi);
        for (int n = 0; n < len; n++) {
            if (strcmp(networks[n].ssid, (char*)(ap_info[i].ssid)) == 0) {
                networks[n].signal = ap_info[i].rssi;
                if (networks[n].signal > best_signal) {
                    best_signal = networks[n].signal;
                    if (best_signal > -85) {
                        best_idx = n;
                    }
                }
                break;
            }
        }
    }
    return best_idx;
}

Who is online

Users browsing this forum: No registered users and 132 guests