How to connect my ESP32 to different Wi-Fi networks in sta mode

souler
Posts: 4
Joined: Sun May 26, 2024 12:01 am

How to connect my ESP32 to different Wi-Fi networks in sta mode

Postby souler » Fri May 31, 2024 11:14 am

Hello, I am trying to get my ESP32 in sta mode to connect to different wifi networks, the esp initially connects to an ESP32 in AP mode, that is, an ESP32 in server mode, the code does all the configuration so that it can connect via wifi to the ESP32 in ap mode and also for my wifi network, thanks to the labels I know that the esp ends up disconnecting from the server and switches to my wifi network

but after confirming the network change it shows me this error:

Code: Select all

//Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x400e1153  PS      : 0x00060130  A0      : 0x800d7818  A1      : 0x3ffc8460  
0x400e1153: esp_http_client_set_header at C:/Users/luis_/esp/esp-idf/components/esp_http_client/esp_http_client.c:338

A2      : 0x00000000  A3      : 0x3f405534  A4      : 0x3f405510  A5      : 0x3ffc8920
A6      : 0x3ffc8900  A7      : 0x0000000c  A8      : 0x801576bc  A9      : 0x3ffc8110
A10     : 0x0000005c  A11     : 0xffffffff  A12     : 0x0000005c  A13     : 0x3ffc8320  
A14     : 0x3ffc8120  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000020  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff
0x400014fd: strlen in ROM

0x4000150d: strlen in ROM



Backtrace: 0x400e1150:0x3ffc8460 0x400d7815:0x3ffc8480 0x400d795e:0x3ffc8940 0x400e49d9:0x3ffc89d0 0x400e3931:0x3ffc8a10 0x400e39d0:0x3ffc8aa0 0x400e4108:0x3ffc8ac0 0x400e2948:0x3ffc8ae0 0x40165ea7:0x3ffc8b00 0x400e3066:0x3ffc8b20 0x400e30ce:0x3ffc8b70 0x4008bad5:0x3ffc8b90
0x400e1150: esp_http_client_set_header at C:/Users/luis_/esp/esp-idf/components/esp_http_client/esp_http_client.c:337

.
.
.

I don't know how this error can be decoded in esp32

and my code:

Code: Select all

#include "conexion.h"

const int WIFI_CONNECTED_BIT = BIT0;
const int WIFI_FAIL_BIT = BIT1;
EventGroupHandle_t s_wifi_event_group;
static const char *TAG_CX = "wifi_station";
 

static void wifi_event_handler(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
        wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *) event_data;
        ESP_LOGI(TAG_CX, "Station " MACSTR " joined, AID=%d", MAC2STR(event->mac), event->aid);
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) {
        wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *) event_data;
        ESP_LOGI(TAG_CX, "Station " MACSTR " left, AID=%d", MAC2STR(event->mac), event->aid);
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
        esp_wifi_connect();
        ESP_LOGI(TAG_CX, "Station started");
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
        if (s_retry_num < 5) {
            esp_wifi_connect();
            s_retry_num++;
            ESP_LOGI(TAG_CX, "Retrying to connect to the AP");
        } else {
            xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
        }
        ESP_LOGI(TAG_CX, "Failed to connect to the AP");
    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
        ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
        ESP_LOGI(TAG_CX, "Got IP:" IPSTR, IP2STR(&event->ip_info.ip));
        s_retry_num = 0;
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
    }
}

esp_netif_t *wifi_init_softap(void) 
{
    esp_netif_t *esp_netif_ap = esp_netif_create_default_wifi_ap();

    printf("************ wifi_init_softap ************ \n");

    wifi_config_t wifi_ap_config = {
        .ap = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
            .channel = EXAMPLE_ESP_WIFI_CHANNEL,
            .password = EXAMPLE_ESP_WIFI_PASS,
            .max_connection = EXAMPLE_MAX_STA_CONN,
            .authmode = WIFI_AUTH_WPA2_PSK,
            .pmf_cfg = {
                .required = false,
            },
        },
    };

    if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
        wifi_ap_config.ap.authmode = WIFI_AUTH_OPEN;
    }

    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_ap_config));
    ESP_LOGI(TAG_CX, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
    return esp_netif_ap;
}

void wifi_init_sta(int wifi_conectado) 
{   
    esp_err_t ret = esp_wifi_disconnect();
    if (ret != ESP_OK && ret != ESP_ERR_WIFI_NOT_STARTED) {
        ESP_LOGE(TAG_CX, "Failed to disconnect WiFi: %s", esp_err_to_name(ret));
    }

    wifi_config_t wifi_sta_config = { 0 };
    if (wifi_conectado == WIFI_SERVIDOR || wifi_conectado == WIFI_SOLO) {
        strncpy((char *)wifi_sta_config.sta.ssid, WIFI_SSID, sizeof(wifi_sta_config.sta.ssid) - 1);
        strncpy((char *)wifi_sta_config.sta.password, WIFI_PASS, sizeof(wifi_sta_config.sta.password) - 1);
        wifi_sta_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
    } else if (wifi_conectado == WIFI_MAIN) {
        strncpy((char *)wifi_sta_config.sta.ssid, WIFI_SSID2, sizeof(wifi_sta_config.sta.ssid) - 1);
        strncpy((char *)wifi_sta_config.sta.password, WIFI_PASS2, sizeof(wifi_sta_config.sta.password) - 1);
        wifi_sta_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
    } else {
        ESP_LOGE(TAG_CX, "Invalid WiFi configuration role");
        return;
    }

    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config));
   // ESP_ERROR_CHECK(esp_wifi_connect());
    ESP_LOGI(TAG_CX, "wifi_init_sta: configured with SSID:%s", wifi_sta_config.sta.ssid);
}

void conectarWIFI(int rol) 
{
    printf("************ conectarWIFI ************ \n");
    printf("Role: %d (1:servidor 2:main 3:solo)\n", rol);


    static esp_netif_t *esp_netif = NULL;
    if (esp_netif == NULL) {
        esp_netif = esp_netif_create_default_wifi_sta();
    }

    s_wifi_event_group = xEventGroupCreate();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, NULL));

    if (rol == WIFI_SERVIDOR) {
        ESP_LOGI(TAG_CX, "Configuring ESP32 as server");
        ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
        ESP_ERROR_CHECK(esp_wifi_start());
        wifi_init_softap();
        wifi_init_sta(rol);
    } else if (rol == WIFI_MAIN || rol == WIFI_SOLO) {
        ESP_LOGI(TAG_CX, "Configuring ESP32 as client or solo");
        ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
        ESP_ERROR_CHECK(esp_wifi_start());
        wifi_init_sta(rol);
    } else {
        ESP_LOGE(TAG_CX, "Invalid WiFi role");
    }
}


What can I do or what other ways are there to change the wifi network of my esp32 in sta mode?

MicroController
Posts: 1760
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to connect my ESP32 to different Wi-Fi networks in sta mode

Postby MicroController » Sat Jun 01, 2024 10:57 am

The part of the decoded backtrace you didn't omit indicates that the problem occurs in esp_http_client_set_header(), i.e. with the HTTP client, and not with the WiFi.
So check where and how you're using the HTTP client.

Who is online

Users browsing this forum: No registered users and 80 guests