ESP-IDF:通过http服务器配置wifi连接

时间:2019-01-12 22:16:39

标签: c http wifi esp32

我正在尝试使用ESP-IDF框架创建ESP32应用程序,我想通过http服务器进行wifi STA配置。设备会在启动时检查Flash中是否已设置配置。如果是这样,它将尝试连接到访问点,否则它将运行访问点和http服务器本身,然后用户需要连接wifi并在浏览器中转到192.168.4.1,才能选择ssid并输入密码。

提交表单后,我尝试连接选定的wifi接入点,但收到201错误代码(未找到ap)

访问点扫描:

const pathString = "links.video_link";
const getVideoURL = obj => pathString
  .split('.')
  .reduce((a, prop) => a[prop], obj);

配置保存功能:

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),"&nbsp<b>ssid:</b>&nbsp<i>%s</i>", list[i].ssid);
        httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
        snprintf(outbuf, sizeof(outbuf),"&nbsp<b>rssi:</b>&nbsp<i>%d</i>", list[i].rssi);
        httpd_resp_send_chunk(req, outbuf, strlen(outbuf));
        snprintf(outbuf, sizeof(outbuf),"&nbsp<b>authmode:</b>&nbsp<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_wifi_connect()后触发事件“ SYSTEM_EVENT_STA_DISCONNECTED”

我(21611)wifi softAP:STA已断开连接,ssid:尼克,原因:201

0 个答案:

没有答案
相关问题