ESP32 (ESP IDF) Fails to change to other STA config (generated by user) at runtime

26 Views Asked by At

The issue at hand is that when trying to connect to a new network using espressif's mcu dev framework is that it didnt display any errors besides the fact that STA mode never fired off any events. as you will be able to see in the code below my wifi firmware runs in APSTA mode and while the AP initializes just fine when I try running the new config on the STA part it seems to "not respond" and not output any logs to the event handler. I've got no errors so far, when using the new config I get a ESP_OK response from esp_wifi_connect but never goes trough.

typedef struct {
   char* ssid;
   char* psk;
} wpa_attempt;

wifi_config_t ap_config = {
  .ap = {
  .ssid = APssid,
  .password = APpsk,
  .max_connection = 4,
  .authmode = WIFI_AUTH_WPA2_PSK,
  .ssid_hidden = 0,
  }
};
wifi_config_t sta_config = {
  .sta = {
  .ssid = "",
  .password = "nullnull",
  }
};

void APMode_wpaconn_attmpt( void *parameters){
  wpa_attempt *params = (wpa_attempt *)parameters;
    char *psk = strdup(params->psk); 
    char *ssid = strdup(params->ssid); 
    if (psk == NULL || ssid == NULL) {
        ESP_LOGE(NETAG, "Failed to duplicate strings");
        free(psk);
        free(ssid);
        vTaskDelete(NULL);
    }

    vTaskDelay(1000 / portTICK_PERIOD_MS);
  ESP_LOGW(NETAG, "Attempting network switch...");
  //Wifi Sanity Check
  if (strlen(psk) < 8 || strlen(psk) > 64) {
        ESP_LOGW(NETAG, "ABORTING: Wi-Fi passphrase must be at least 8 characters long (and no bigger than 64)");
        ESP_LOGW(NETAG, "%s",psk);
        free(psk);
        free(ssid);
        vTaskDelete(NULL); 
    }
    if (strlen(ssid) == 0 || strlen(ssid) > 32) {
        ESP_LOGW(NETAG, "ABORTING: Cannot insert a NULL ssid");
        ESP_LOGW(NETAG, "%s",ssid);
        free(psk);
        free(ssid);
        vTaskDelete(NULL); 
    }

 wifi_config_t nsta_config = {
  .sta = {
        .bssid_set = 0,
        .bssid = {0},   
        .channel = 0,   
        .scan_method = WIFI_ALL_CHANNEL_SCAN,  
        .listen_interval = 0,  
        .sort_method = WIFI_CONNECT_AP_BY_SIGNAL,  
    },
 };
 strncpy((char*)nsta_config.sta.ssid, ssid,  sizeof(nsta_config.sta.ssid) - 1);
 strncpy((char*)nsta_config.sta.password,psk,  sizeof(nsta_config.sta.password) - 1);
 ESP_LOGI(NETAG, "Getting Wi-Fi Config... ");
 ESP_LOGI(NETAG,"Connection configuration to Wi-Fi AP on ssid: %s / pass: %s",nsta_config.sta.ssid,nsta_config.sta.password);
 ESP_ERROR_CHECK(esp_wifi_disconnect());
 ESP_ERROR_CHECK(esp_wifi_stop());
 vTaskDelay(1000 / portTICK_PERIOD_MS);
 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
 ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP,&ap_config));
 esp_err_t wifi_set_conf_err = esp_wifi_set_config(ESP_IF_WIFI_STA,&nsta_config);
 if (wifi_set_conf_err != ESP_OK) {
    ESP_LOGE(NETAG, "Wi-Fi Conf error code %d", wifi_set_conf_err);
 }  else ESP_LOGI(NETAG, "Wi-Fi Conf OK..." );
 ESP_ERROR_CHECK(esp_wifi_start());
 esp_err_t wifi_connect_err = esp_wifi_connect();
 if (wifi_connect_err != ESP_OK) {
    ESP_LOGE(NETAG, "Wi-Fi connection attempt failed with error code %d", wifi_connect_err);
 }  else ESP_LOGI(NETAG, "Wi-Fi connection attempt passed..." );
 APwpa_flag = 1;
 AP_wpa_retries = 0;
 free(psk);
 free(ssid);
 vTaskDelete(NULL); 
};

...
// JSON parse code here and HTTP server stuff ( <esp_http_server.h> )

...
void app_main(void)
{
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
esp_netif_create_default_wifi_ap();
esp_netif_create_default_wifi_sta();
wifi_init_config_t wifi_config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_config));
 ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
 ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP,&ap_config));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA,&sta_config));
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(APTAG, "Access Point started");
vTaskDelay(1000 / portTICK_PERIOD_MS);
httpd_handle_t server = start_webserver();
}

Sorry in advance for the code dump & bad spaghetti code here and there as this is after round 10 of trying to debug my issue.

Thanks for the reading & help!

Even though the code snippet provided did not feature it I tried full deinit and reinit of the wifi module with no luck, it also seems that doing this gives me sometimes another error like "strlen in ROM" and such triggering the Watchdog.

Also other things that have been tried are:

  1. getting my test networks credentials "hardcoded" at the beginning skipping all the json parsing in app_main ( and worked ) but ultimatley did not help me reach my goal of getting the network info via the user input.
  2. running wifi module with "WiFi NVS" off so as not to cache the old credentials and cause conflict
  3. verbose logging did not provide any other leads towards finding the problem
0

There are 0 best solutions below