ESP-IDF: Configure wifi connection over http server

nicollo
Posts: 6
Joined: Sun Jan 13, 2019 7:50 am

ESP-IDF: Configure wifi connection over http server

Postby nicollo » Sun Jan 13, 2019 8:01 am

Hi guys! I need for some help :D
I'm trying to create an ESP32 application using ESP-IDF framework, and I want to make wifi STA configuration over http server. Device checks if config already setted in flash on startup. If so - it tries to connect to access point, otherwise it run access point and http server, then user need to connect wifi and go to 192.168.4.1 in browser, to be able to select ssid and enter password.

After form submit i'm trying to connect selected wifi access point but get 201 error code (no ap found)

index html

Code: Select all

    <html>
      <head>
        <title>Esp32</title>
        <meta charset='UTF-8'>
      </head>
      <body>
       <h1>Choose access point</h1>
       <form method="POST" action="/">{{p}}
         <br/><input type="text" name="password" placeholder="Wifi password"/>
         <br/><input type="submit" value="Save"/>
       </form>
      </body>
    </html>
Access point scanning:
  1. esp_err_t index_get_handler(httpd_req_t *req)
  2. {
  3.     wifi_scan_config_t scanConf = {
  4.         .ssid = NULL,
  5.         .bssid = NULL,
  6.         .channel = 0,
  7.         .show_hidden = 1
  8.     };
  9.     ESP_ERROR_CHECK(esp_wifi_scan_start(&scanConf, 0));
  10.     uint16_t apCount;
  11.     if( xQueueReceive( wifi_stations_count, &( apCount ), ( TickType_t ) 1000 ) == pdFALSE)
  12.     {
  13.         httpd_resp_send(req, (char *) not_found_html_start, not_found_html_html_end - not_found_html_start);
  14.         ESP_LOGI(TAG, "Stations not found");
  15.         return ESP_OK;
  16.     }
  17.     ESP_LOGI(TAG, "%d stations found", apCount);
  18.  
  19.     char *p = strstr((char *) index_html_start, "{{p}}");
  20.     httpd_resp_send_chunk(req, (char *) index_html_start, p - (char *) index_html_start);
  21.     wifi_ap_record_t *list = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount);
  22.     ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, list));
  23.     char outbuf[100];
  24.     uint8_t i;
  25.     for (i=0; i<apCount; i++) {
  26.         char *authmode;
  27.         switch(list[i].authmode) {
  28.             case WIFI_AUTH_OPEN:
  29.                 authmode = "OPEN";
  30.                 break;
  31.             case WIFI_AUTH_WEP:
  32.                 authmode = "WEP";
  33.                 break;
  34.             case WIFI_AUTH_WPA_PSK:
  35.                 authmode = "WPA_PSK";
  36.                 break;
  37.             case WIFI_AUTH_WPA2_PSK:
  38.                 authmode = "WPA2_PSK";
  39.                 break;
  40.             case WIFI_AUTH_WPA_WPA2_PSK:
  41.                 authmode = "WPA/2_PSK";
  42.                 break;
  43.             default:
  44.                 authmode = "Unknown";
  45.                 break;
  46.         }
  47.         snprintf(outbuf, sizeof(outbuf),"<br/><input type='radio' name='ssid' value='%s' />", list[i].ssid);
  48.         httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
  49.         snprintf(outbuf, sizeof(outbuf),"&nbsp<b>ssid:</b>&nbsp<i>%s</i>", list[i].ssid);
  50.         httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
  51.         snprintf(outbuf, sizeof(outbuf),"&nbsp<b>rssi:</b>&nbsp<i>%d</i>", list[i].rssi);
  52.         httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
  53.         snprintf(outbuf, sizeof(outbuf),"&nbsp<b>authmode:</b>&nbsp<i>%s</i>", authmode);
  54.         httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
  55.     }
  56.     free(list);
  57.  
  58.     p = p + 5;
  59.     httpd_resp_send_chunk(req, p, (char *) index_html_end - p);
  60.  
  61.     httpd_resp_send_chunk(req, NULL, 0);
  62.  
  63.     return ESP_OK;
  64. }
Config saving function:
  1. esp_err_t index_post_handler(httpd_req_t *req)
  2. {
  3.     uint8_t buffer[100];
  4.     wifi_config_t wifi_config;
  5.     httpd_req_recv(req, (char *) buffer, 100);
  6.  
  7.     if (httpd_query_key_value((char *) buffer, "ssid", (char *) wifi_config.sta.ssid, 32) == ESP_ERR_NOT_FOUND) {
  8.         httpd_resp_set_status(req, "400");
  9.         httpd_resp_send(req, "SSID required", -1);
  10.         return ESP_OK;
  11.     }
  12.     if (httpd_query_key_value((char *) buffer, "password", (char *) wifi_config.sta.password, 64) == ESP_ERR_NOT_FOUND) {
  13.         httpd_resp_set_status(req, "400");
  14.         httpd_resp_send(req, "Password is required", -1);
  15.         return ESP_OK;
  16.     }
  17.  
  18.     if (strlen((char *) wifi_config.sta.ssid) < 1) {
  19.         httpd_resp_set_status(req, "400");
  20.         httpd_resp_send(req, "<p>Invalid ssid</p>", -1);
  21.         return ESP_OK;
  22.     }
  23.  
  24.     ESP_LOGI(TAG, "SSID: %s, Pass: %s", wifi_config.sta.ssid, wifi_config.sta.password);
  25.     httpd_resp_send(req, "<h1>OK</h1>", -1);
  26.  
  27.     ESP_ERROR_CHECK(esp_wifi_stop());
  28.     ESP_ERROR_CHECK(esp_wifi_restore());
  29.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  30.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  31.     ESP_ERROR_CHECK(esp_wifi_start());
  32.  
  33.     return ESP_OK;
  34. }
Event "SYSTEM_EVENT_STA_DISCONNECTED" fires after esp_wifi_connect() invoking

I (21611) wifi softAP: STA disconnected, ssid: Nick, reason: 201

nicollo
Posts: 6
Joined: Sun Jan 13, 2019 7:50 am

Re: ESP-IDF: Configure wifi connection over http server

Postby nicollo » Sun Jan 13, 2019 6:00 pm

I guess I found solution:

Code: Select all

static wifi_config_t wifi_config = {};
solves the problem.

cnshibo
Posts: 1
Joined: Mon Jan 04, 2016 1:22 pm

Re: ESP-IDF: Configure wifi connection over http server

Postby cnshibo » Fri Feb 01, 2019 1:44 am

nicollo wrote:
Sun Jan 13, 2019 6:00 pm
I guess I found solution:

Code: Select all

static wifi_config_t wifi_config = {};
solves the problem.
want to do same thing. Thanks for sharing

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: ESP-IDF: Configure wifi connection over http server

Postby GeorgeFlorian1 » Thu Feb 21, 2019 10:54 am

Can this be "translated" to simpler, Arduino IDE code ?

I'm trying to make something similar.
GeorgeFlorian1 wrote:
Thu Feb 21, 2019 9:28 am
I've been trying to save some values from two input fields inside a HTML page.

Here is the main.cpp.
Here is the index.html.

As you can see I've been trying to save those params in other const *char just to be able to Serial.print(); them but it doesn't work. They show a blank space in Serial Monitor.

Also, the request->redirect("/url"); does not work as intended. I've pressed on the Sign In or Restart buttons and the Enter key on my keyboard and nothing happens.

Looking at the sketch I realize that I have no idea how to implement an action when pressing, for example, Sign In button.
How do I link those two buttons to actions in my sketch ? I haven't been able to found anything that resembles my project.

Does anybody have any idea ? Because I really don't.

Thank you !

chrisruppen
Posts: 1
Joined: Fri Feb 22, 2019 12:02 am

Re: ESP-IDF: Configure wifi connection over http server

Postby chrisruppen » Fri Feb 22, 2019 12:07 am

Hey guys, good evening!

I've been trying several alternatives to get to a solution similar to yours but had no luck.

Would you mind sharing your repository so I can take a look? ;)

Cheers!

nicollo
Posts: 6
Joined: Sun Jan 13, 2019 7:50 am

Re: ESP-IDF: Configure wifi connection over http server

Postby nicollo » Wed Mar 27, 2019 8:22 am

chrisruppen wrote:
Fri Feb 22, 2019 12:07 am
...
Would you mind sharing your repository so I can take a look? ;)
I've decided to do in another way. For now I use BLE for wifi configuration.
Google Chrome allows to access device bluetooth using javascript.

So no need to store html in flash

lemmi25
Posts: 1
Joined: Wed Jul 31, 2019 8:00 pm

Re: ESP-IDF: Configure wifi connection over http server

Postby lemmi25 » Wed Jul 31, 2019 8:06 pm

Hi,

maybe my answer comes a little bit late but this is from my last project. I hope it is of help for anyone.

https://github.com/lemmi25/ESP32WiFiAP

In this project you can connect to an AP opened by the ESP32 that has a static IP.
After providing ssid and psw of your AP you will be connected to the Internet.
For demonstration the RGB LED on the ESP32 Wroover-Kit starts to shine.

Cheers ;)

Shakir55
Posts: 2
Joined: Tue Mar 07, 2023 7:10 am

Re: ESP-IDF: Configure wifi connection over http server

Postby Shakir55 » Wed Apr 26, 2023 9:33 am

Can you show me whole code on this.

Who is online

Users browsing this forum: No registered users and 13 guests