`I am working on a project where I am trying to configure WiFi settings through a web interface using an ESP32-based device. However, I am encountering issues with displaying the SSID and password in the command prompt and subsequently connecting to the WiFi router. Here's an overview of my code and the problem I'm facing:

/*  WiFi softAP Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_mac.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_http_client.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include <string.h>
#include <sys/param.h>
#include "esp_netif.h"
#include <esp_http_server.h>
#include "Extern.h"
#include "wifiscan.h"

static const char *TAG = "webserver";

static char ssid_buffer[32] = "";       // Buffer to store SSID
static char password_buffer[64] = "";   // Buffer to store password


char ssid_option_list_buff[4096]={0};
char html_file_buff[10000]={0};
char html_file_buff1[10000]={0};
char ssid_list[DEFAULT_SCAN_LIST_SIZE][32]={0};

static esp_err_t ledOFF_handler(httpd_req_t *req)
{
    esp_err_t error;
    ESP_LOGI(TAG, "LED Turned OFF");
    const char *response= (const char *) req->user_ctx;
    error=httpd_resp_send(req, response, strlen(response));
    if(error!= ESP_OK)
    {
        ESP_LOGI(TAG, "Error %d while sending Response", error);

    }
    else ESP_LOGI(TAG, "Response sent Successfully");
    return error;
}

static const httpd_uri_t ledoff = {
    .uri       = "/connect",
    .method    = HTTP_GET,
    .handler   = ledOFF_handler,
    /* Let's pass response string in user
     * context to demonstrate it's usage */
    .user_ctx  = (void *) html_file_buff
};

static esp_err_t Connecting_handler(httpd_req_t *req)
{
    esp_err_t error;

    // Check if this is a POST request
    if (req->method == HTTP_POST)
    {
        char post_data[128]; // Adjust the buffer size as needed
        int content_length = httpd_req_get_hdr_value_len(req, "Content-Length");

        if (content_length > 0)
        {
            // Read the POST data
            int read_len = httpd_req_recv(req, post_data, content_length);
            if (read_len <= 0)
            {
                ESP_LOGI(TAG, "Error reading POST data");
                // Handle error
                return ESP_FAIL;
            }
            post_data[read_len] = '\0'; // Null-terminate the data

            // Now, parse the POST data to get the SSID and password
            // Modify the parsing logic according to your data format
            // For example, you can use strtok or custom parsing functions

            // Example: parsing "ssid=MySSID&password=MyPassword"
            char *ssid_start = strstr(post_data, "ssid=");
            char *password_start = strstr(post_data, "password=");

            if (ssid_start && password_start)
            {
                // Extract SSID and password
                sscanf(ssid_start + 5, "%s", ssid_buffer);
                sscanf(password_start + 9, "%s", password_buffer);

                ESP_LOGI(TAG, "Received SSID: %s, Password: %s", ssid_buffer, password_buffer);

                // Here, you can use the obtained SSID and password to connect to the WiFi network.
                // Implement your WiFi connection logic here.
            }
        }
    }

    // Respond to the POST request (e.g., with a success message)
    const char *response = "Credentials received successfully";
    error = httpd_resp_send(req, response, strlen(response));
    if (error != ESP_OK)
    {
        ESP_LOGI(TAG, "Error %d while sending Response", error);
    }
    else
    {
        ESP_LOGI(TAG, "Response sent Successfully");
    }

    return error;
}


static const httpd_uri_t Connecting = {
    .uri       = "/Sendcredentials",
    .method    = HTTP_POST,
    .handler   = Connecting_handler,
    /* Let's pass response string in user
     * context to demonstrate it's usage */
    .user_ctx  = NULL
};

esp_err_t http_404_error_handler(httpd_req_t *req, httpd_err_code_t err)
{

    /* For any other URI send 404 and close socket */
    httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Some 404 error message");
    return ESP_FAIL;
}

static httpd_handle_t start_webserver(void)
{
    httpd_handle_t server = NULL;
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
#if CONFIG_IDF_TARGET_LINUX
    // Setting port as 8001 when building for Linux. Port 80 can be used only by a priviliged user in linux.
    // So when a unpriviliged user tries to run the application, it throws bind error and the server is not started.
    // Port 8001 can be used by an unpriviliged user as well. So the application will not throw bind error and the
    // server will be started.
    config.server_port = 8001;
#endif // !CONFIG_IDF_TARGET_LINUX
    config.lru_purge_enable = true;

    // Start the httpd server
    ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
    if (httpd_start(&server, &config) == ESP_OK) {
        // Set URI handlers
        ESP_LOGI(TAG, "Registering URI handlers");
        httpd_register_uri_handler(server, &ledoff);
        httpd_register_uri_handler(server, &Connecting);
        return server;
    }

    ESP_LOGI(TAG, "Error starting server!");
    return NULL;
}

#if !CONFIG_IDF_TARGET_LINUX
static esp_err_t stop_webserver(httpd_handle_t server)
{
    // Stop the httpd server
    return httpd_stop(server);
}

static void disconnect_handler(void* arg, esp_event_base_t event_base,
                               int32_t event_id, void* event_data)
{
    httpd_handle_t* server = (httpd_handle_t*) arg;
    if (*server) {
        ESP_LOGI(TAG, "Stopping webserver");
        if (stop_webserver(*server) == ESP_OK) {
            *server = NULL;
        } else {
            ESP_LOGE(TAG, "Failed to stop http server");
        }
    }
}

static void connect_handler(void* arg, esp_event_base_t event_base,
                            int32_t event_id, void* event_data)
{
    httpd_handle_t* server = (httpd_handle_t*) arg;
    if (*server == NULL) {
        ESP_LOGI(TAG, "Starting webserver");
        *server = start_webserver();
    }
}
#endif

/* The examples use WiFi configuration that you can set via project configuration menu.

   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID      CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS      CONFIG_ESP_WIFI_PASSWORD
#define EXAMPLE_ESP_WIFI_CHANNEL   CONFIG_ESP_WIFI_CHANNEL
#define EXAMPLE_MAX_STA_CONN       CONFIG_ESP_MAX_STA_CONN



static void wifi_event_handler(void* arg, esp_event_base_t event_base,
                                    int32_t event_id, void* event_data)
{
    if (event_id == WIFI_EVENT_AP_STACONNECTED) {
        wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
        ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
                 MAC2STR(event->mac), event->aid);
    } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
        wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
        ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
                 MAC2STR(event->mac), event->aid);
    }
}

void wifi_init_softap(void)
{
    //ESP_ERROR_CHECK(esp_netif_init());
    //ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_ap();

    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));

    wifi_config_t wifi_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,
#ifdef CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT
            .authmode = WIFI_AUTH_WPA3_PSK,
            .sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
#else /* CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT */
            .authmode = WIFI_AUTH_WPA2_PSK,
#endif
            .pmf_cfg = {
                    .required = true,
            },
        },
    };
    if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
        wifi_config.ap.authmode = WIFI_AUTH_OPEN;
    }

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());

    ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
}

static void wifi_scan(void)
{
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
    assert(sta_netif);

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

    uint16_t number = DEFAULT_SCAN_LIST_SIZE;
    //wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
    
    memset(ap_info, 0, sizeof(ap_info));

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_start());
    esp_wifi_scan_start(NULL, true);
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
    ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);
    for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < ap_count); i++) {
        //ESP_LOGI(TAGwifi, "SSID \t\t%s", ap_info[i].ssid);
        //ESP_LOGI(TAGwifi, "RSSI \t\t%d", ap_info[i].rssi);
        memcpy(&ssid_list[i][0],ap_info[i].ssid,sizeof(ap_info[i].ssid));
    }
    int ssid_list_len= (ap_count>DEFAULT_SCAN_LIST_SIZE) ? DEFAULT_SCAN_LIST_SIZE:ap_count;
    //printf("SSID_LIST_LENGTH %d \n",ssid_list_len);
    load_ssid_list_into_html_buff(ssid_list,ssid_list_len);
}

void app_main(void)
{

    static httpd_handle_t server = NULL;

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

    ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
    wifi_scan();
    for (int i = 0; (i < 20) && (i < ap_count); i++) {
        ESP_LOGI(TAG, "SSID \t\t%s", ap_info[i].ssid);
    }

    vTaskDelay(10000 / portTICK_PERIOD_MS);

    wifi_init_softap();

    ESP_ERROR_CHECK(esp_netif_init());
    
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &connect_handler, &server));
    //ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));

}

void load_ssid_list_into_html_buff(char list[][32],uint8_t list_size)
{
    int inc=0;
    for(int i=0;i<list_size;i++)
    {
        sprintf(&ssid_option_list_buff[inc],"<option value=\"SSID%d\">%s</option>",i,&list[i][0]);
        inc+=strlen(&ssid_option_list_buff[inc]);
    }
    sprintf(html_file_buff,
        "<!DOCTYPE html>\
        <html>\
        <head>\
            <title>WiFi Configuration</title>\
            <style>\
                /* ... your CSS styles ... */\
            </style>\
        </head>\
        <body>\
            <div class=\"container\">\
                <h2>WiFi Configuration</h2>\
                <form action=\"/Sendcredentials\" method=\"POST\">\
                    <label for=\"ssid\">Select SSID:</label>\
                    <select id=\"ssid\" name=\"ssid\">\
                        %s\
                    </select>\
                    <label for=\"password\">Password:</label>\
                    <input type=\"password\" id=\"password\" name=\"password\">\
                    <button type=\"submit\">Connect</button>\
                </form>\
            </div>\
        </body>\
        </html>\
        ", ssid_option_list_buff);

        /*sprintf(html_file_buff1,
        "<!DOCTYPE html>\
        <html>\
<body>\
\
Field1: <input type=\"text\" id=\"field1\" value=\"Hello World!\"><br>\
Field2: <input type=\"text\" id=\"field2\"><br><br>\
\
<button onclick=\"myFunction()\">Copy Text</button>\
\
<p>A function is triggered when the button is clicked. The function copies the text from Field1 into Field2.</p>\
\
<script>\
function myFunction() {\
  document.getElementById(\"field2\").value = document.getElementById(\"field1\").value;\
}\
</script>\
\
</body>\
</html>");*/
}

I have implemented a web server using the ESP-IDF framework to collect SSID and password inputs from an HTML form. When a user submits the form, the SSID and password are extracted from the POST request and stored in the ssid_buffer and password_buffer, respectively. The code appears to execute without errors, but I am unable to see the SSID and password in the command prompt, and the device is not connecting to the WiFi network. I have verified that the HTML form is being displayed correctly, and the user inputs are being sent via a POST request to the server. I have checked that the ssid_buffer and password_buffer variables are correctly assigned with the values extracted from the POST request. I have verified that the WiFi configuration settings are correct, including SSID, password, and channel. I have attempted to connect to the WiFi network using the esp_wifi_start() function, but it does not seem to establish a connection.`

0

There are 0 best solutions below