ESP-IDF: Configure wifi connection over http server
Posted: Sun Jan 13, 2019 8:01 am
Hi guys! I need for some help
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
Access point scanning:
Config saving function:
Event "SYSTEM_EVENT_STA_DISCONNECTED" fires after esp_wifi_connect() invoking
I (21611) wifi softAP: STA disconnected, ssid: Nick, reason: 201
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>
- esp_err_t index_get_handler(httpd_req_t *req)
- {
- wifi_scan_config_t scanConf = {
- .ssid = NULL,
- .bssid = NULL,
- .channel = 0,
- .show_hidden = 1
- };
- ESP_ERROR_CHECK(esp_wifi_scan_start(&scanConf, 0));
- uint16_t apCount;
- if( xQueueReceive( wifi_stations_count, &( apCount ), ( TickType_t ) 1000 ) == pdFALSE)
- {
- httpd_resp_send(req, (char *) not_found_html_start, not_found_html_html_end - not_found_html_start);
- ESP_LOGI(TAG, "Stations not found");
- return ESP_OK;
- }
- ESP_LOGI(TAG, "%d stations found", apCount);
- char *p = strstr((char *) index_html_start, "{{p}}");
- httpd_resp_send_chunk(req, (char *) index_html_start, p - (char *) index_html_start);
- wifi_ap_record_t *list = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount);
- ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, list));
- char outbuf[100];
- uint8_t i;
- for (i=0; i<apCount; i++) {
- char *authmode;
- switch(list[i].authmode) {
- case WIFI_AUTH_OPEN:
- authmode = "OPEN";
- break;
- case WIFI_AUTH_WEP:
- authmode = "WEP";
- break;
- case WIFI_AUTH_WPA_PSK:
- authmode = "WPA_PSK";
- break;
- case WIFI_AUTH_WPA2_PSK:
- authmode = "WPA2_PSK";
- break;
- case WIFI_AUTH_WPA_WPA2_PSK:
- authmode = "WPA/2_PSK";
- break;
- default:
- authmode = "Unknown";
- break;
- }
- snprintf(outbuf, sizeof(outbuf),"<br/><input type='radio' name='ssid' value='%s' />", list[i].ssid);
- httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
- snprintf(outbuf, sizeof(outbuf)," <b>ssid:</b> <i>%s</i>", list[i].ssid);
- httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
- snprintf(outbuf, sizeof(outbuf)," <b>rssi:</b> <i>%d</i>", list[i].rssi);
- httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
- snprintf(outbuf, sizeof(outbuf)," <b>authmode:</b> <i>%s</i>", authmode);
- httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
- }
- free(list);
- p = p + 5;
- httpd_resp_send_chunk(req, p, (char *) index_html_end - p);
- httpd_resp_send_chunk(req, NULL, 0);
- return ESP_OK;
- }
- esp_err_t index_post_handler(httpd_req_t *req)
- {
- uint8_t buffer[100];
- wifi_config_t wifi_config;
- httpd_req_recv(req, (char *) buffer, 100);
- if (httpd_query_key_value((char *) buffer, "ssid", (char *) wifi_config.sta.ssid, 32) == ESP_ERR_NOT_FOUND) {
- httpd_resp_set_status(req, "400");
- httpd_resp_send(req, "SSID required", -1);
- return ESP_OK;
- }
- if (httpd_query_key_value((char *) buffer, "password", (char *) wifi_config.sta.password, 64) == ESP_ERR_NOT_FOUND) {
- httpd_resp_set_status(req, "400");
- httpd_resp_send(req, "Password is required", -1);
- return ESP_OK;
- }
- if (strlen((char *) wifi_config.sta.ssid) < 1) {
- httpd_resp_set_status(req, "400");
- httpd_resp_send(req, "<p>Invalid ssid</p>", -1);
- return ESP_OK;
- }
- ESP_LOGI(TAG, "SSID: %s, Pass: %s", wifi_config.sta.ssid, wifi_config.sta.password);
- httpd_resp_send(req, "<h1>OK</h1>", -1);
- ESP_ERROR_CHECK(esp_wifi_stop());
- ESP_ERROR_CHECK(esp_wifi_restore());
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
- ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
- ESP_ERROR_CHECK(esp_wifi_start());
- return ESP_OK;
- }
I (21611) wifi softAP: STA disconnected, ssid: Nick, reason: 201