esp32c3-usb_serial/main/wifi/wifi_html.c

313 lines
11 KiB
C

#include "wifi_task.h"
#include "esp_http_server.h"
#include "esp_vfs.h"
#include "cJSON.h"
static const char *TAG = "wifi_html";
uint8_t* home_html_data;
wifi_html_t home_html = {
.html_name = "home.html",
.html_addr = 0xb84,
.html_size = 4081,
};
wifi_html_t wifi_config_html = {
.html_name = "wifi_config.html",
.html_addr = 0x44b9,
.html_size = 6671,
};
wifi_html_t favicon_ico = {
.html_name = "favicon.ico",
.html_addr = 0,
.html_size = 106008,
};
static esp_err_t index_get_handler(httpd_req_t *req)
{
/* Add file upload form and script which on execution sends a POST request to /upload */
httpd_resp_set_type(req,HTTPD_TYPE_TEXT);
httpd_resp_send(req, (const char *)wifi_config_html.html_data, wifi_config_html.html_size);
return ESP_OK;
}
static esp_err_t Home_get_handler(httpd_req_t *req)
{
/* Add file upload form and script which on execution sends a POST request to /upload */
httpd_resp_set_type(req,HTTPD_TYPE_TEXT);
httpd_resp_send(req, (const char*)home_html.html_data, home_html.html_size);
return ESP_OK;
}
/* Handler to respond with an icon file embedded in flash.
* Browsers expect to GET website icon at URI /favicon.ico.
* This can be overridden by uploading file with same name */
static esp_err_t favicon_get_handler(httpd_req_t *req)
{
httpd_resp_set_type(req, "image/x-icon");
httpd_resp_send(req, (const char *)favicon_ico.html_data, favicon_ico.html_size);
return ESP_OK;
}
char wifi_name[20] = {0};
char wifi_password[12] = {0};
static esp_err_t wifi_config_handler(httpd_req_t *req)
{
char buf[100];
// char ssid[10];
// char pswd[10];
bool wifi_write = false;
int ret, remaining = req->content_len;
ESP_LOGI(TAG,"req->content_len:%d",req->content_len);
while (remaining > 0) {
/* Read the data for the request */
if ((ret = httpd_req_recv(req, buf,
MIN(remaining, sizeof(buf)))) <= 0) {
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
/* Retry receiving if timeout occurred */
continue;
}
return ESP_FAIL;
}
// /* Send back the same data */
// httpd_resp_send_chunk(req, buf, ret);
remaining -= ret;
cJSON *root = cJSON_Parse(buf);
// int ssid = cJSON_GetObjectItem(root, "wifi_name")->valueint;
// int code = cJSON_GetObjectItem(root, "wifi_code")->valueint;
char *ssid = cJSON_GetObjectItem(root, "wifi_name")->valuestring;
char *code = cJSON_GetObjectItem(root, "wifi_code")->valuestring;
int len1 = strlen(ssid);
int len2 = strlen(code);
memcpy(wifi_name,ssid,strlen(ssid));
memcpy(wifi_password,code,strlen(code));
wifi_name[len1] = '\0';
wifi_password[len2] = '\0';
cJSON_Delete(root);
// ESP_LOGI(TAG, "json load finished. SSID:%d password:%d ",ssid,code);
// ESP_LOGI(TAG, "json load finished. SSID:%s password:%s ",user_id,user_code);
printf("\r\nwifi_ssid:");
for(int i = 0;i<len1;i++){
printf("%c",wifi_name[i]);
}
printf("\r\nwifi_code:");
for(int i = 0;i<len2;i++){
printf("%c",wifi_password[i]);
}
printf("\r\n");
// esp_err_t e = httpd_query_key_value(buf,"wifi_name",wifi_name,sizeof(wifi_name));
// if(e == ESP_OK) {
// printf("ssid = %s\r\n",wifi_name);
// }
// else {
// printf("error = %d\r\n",e);
// }
// e = httpd_query_key_value(buf,"wifi_code",wifi_password,sizeof(wifi_password));
// if(e == ESP_OK) {
// printf("pswd = %s\r\n",wifi_password);
// }
// else {
// printf("error = %d\r\n",e);
// }
// sprintf(esp_wifi_info.ssid,"%s",wifi_name);
// sprintf(esp_wifi_info.password,"%s",wifi_password);
// esp_wifi_info.reboot = false;
// wifi_config_data.SubType = 0x40;
// wifi_config_data.addr = 0;
// Write_ESPFlash(wifi_config_data.SubType,wifi_config_data.addr,(uint8_t *)&wifi_config_data,sizeof(struct save_flash_t) + sizeof(wifi_info_t));
/* Log data received */
ESP_LOGI(TAG, "=========== RECEIVED DATA ==========");
ESP_LOGI(TAG, "%.*s", ret, buf);
ESP_LOGI(TAG, "====================================");
ESP_LOGI(TAG, "Prepare to restart system!");
wifi_write = true;
}
// End response
// 结束响应
if (wifi_write) {
ESP_LOGI(TAG, "Wi-Fi configuration successful.");
httpd_resp_send(req, "{\"status\":\"success\",\"message\":\"Wi-Fi配置成功!\"}", HTTPD_RESP_USE_STRLEN);
} else {
httpd_resp_send(req, "{\"status\":\"error\",\"message\":\"Wi-Fi配置失败!\"}", HTTPD_RESP_USE_STRLEN);
}
if(strcmp(wifi_name ,"\0")!=0 && strcmp(wifi_password,"\0")!=0)
{
//xSemaphoreGive(ap_sem);
// ESP_LOGI(TAG,"ssid:%s password:%s",esp_wifi_info.ssid,esp_wifi_info.password);
ESP_LOGI(TAG, "set wifi name and password successfully! goto station mode");
// if(wifi_write == true)esp_restart();
}
return ESP_OK;
}
static esp_err_t upgrade_post_handler(httpd_req_t *req)
{
int remaining = req->content_len;
char* buff;
uint16_t received = 0;
//ota_bin_data.buf = malloc(8192);
ESP_LOGI(TAG,"req->url:%s",req->uri);
ESP_LOGI(TAG,"req->content_len:%d",req->content_len);
// if(Search_Binfile(req->uri,strlen(req->uri)) == true)
// {
// //ESP_LOGI(TAG,"received:%d",received);
// buff = (char*)ota_tx_data.data;
// ota_tx_data.type = TYPE_MES_HEAD;
// ota_tx_data.size = req->content_len;
// xQueueSend(ota_data_queue, &ota_tx_data, portMAX_DELAY);
// xEventGroupSetBits(esp_event_group,OTA_EVENT);
// while (remaining > 0)
// {
// if ((received = httpd_req_recv(req, &buff[0], MIN(remaining, 4096))))
// {
// ota_tx_data.type = TYPE_WIFI_HTTP;
// // ota_bin_data.buf[received] = '\0';
// ota_tx_data.size = received;
// //ESP_LOGI(TAG,"received:%d %c",received,http_file_data.buf[0]);
// xQueueSend(ota_data_queue, &ota_tx_data, portMAX_DELAY);
// remaining -= received;
// }
// }
// }else{
// buff = http_file_data.buff;
// sprintf(http_file_data.file_name,req->uri);
// http_file_data.type = TYPE_START_OTA;
// http_file_data.file_size = req->content_len;
// xQueueSend(web_data_queue, &http_file_data, portMAX_DELAY);
// while (remaining > 0)
// {
// if ((received = httpd_req_recv(req, &buff[0], MIN(remaining, 8192))))
// {
// http_file_data.type = TYPE_START_OTA|TYPE_DATA;
// // ota_bin_data.buf[received] = '\0';
// http_file_data.buff_size = received;
// //ESP_LOGI(TAG,"received:%d %c",received,http_file_data.buf[0]);
// xQueueSend(web_data_queue, &http_file_data, portMAX_DELAY);
// remaining -= received;
// }
// }
// }
//free(ota_bin_data.buf);
ESP_LOGI(TAG,"upgrade ok %d",remaining);
/* Redirect onto root to see the updated file list */
httpd_resp_set_status(req, "303 See Other");
httpd_resp_set_hdr(req, "Location", "/ota");
httpd_resp_sendstr(req, "File uploaded successfully");
return ESP_OK;
}
/* Scratch buffer size */
#define SCRATCH_BUFSIZE 8192
struct file_server_data {
/* Base path of file storage */
char base_path[ESP_VFS_PATH_MAX + 1];
/* Scratch buffer for temporary storage during file transfer */
char scratch[SCRATCH_BUFSIZE];
};
esp_err_t start_web_server(void)
{
static struct file_server_data *server_data = NULL;
/* Allocate memory for server data */
server_data = calloc(1, sizeof(struct file_server_data));
if (!server_data) {
ESP_LOGE(TAG, "Failed to allocate memory for server data");
return ESP_ERR_NO_MEM;
}
strlcpy(server_data->base_path, "/data",
sizeof(server_data->base_path));
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
/* Use the URI wildcard matching function in order to
* allow the same handler to respond to multiple different
* target URIs which match the wildcard scheme */
config.uri_match_fn = httpd_uri_match_wildcard;
ESP_LOGI(TAG, "Starting HTTP Server");
if (httpd_start(&server, &config) != ESP_OK) {
ESP_LOGE(TAG, "Failed to start file server!");
return ESP_FAIL;
}
//start_file_server("/sdcard",server);
//return ESP_OK;
httpd_uri_t index_page = {
.uri = "/", //192.168.1.4
.method = HTTP_GET,
.handler = Home_get_handler,
/* Let's pass response string in user
* context to demonstrate it's usage */
.user_ctx = server_data
};
httpd_register_uri_handler(server, &index_page);
// httpd_uri_t upgrade_ota = {
// .uri = "/ota", //192.168.1.4
// .method = HTTP_GET,
// .handler = upgrade_ota_bin,
// /* Let's pass response string in user
// * context to demonstrate it's usage */
// .user_ctx = NULL,
// };
// httpd_register_uri_handler(server, &upgrade_ota);
httpd_uri_t home_page = {
.uri = "/config", //192.168.1.4
.method = HTTP_GET,
.handler = index_get_handler,
/* Let's pass response string in user
* context to demonstrate it's usage */
.user_ctx = NULL,
};
httpd_register_uri_handler(server, &home_page);
httpd_uri_t echo = {
.uri = "/config",
.method = HTTP_POST,
.handler = wifi_config_handler,
.user_ctx = server_data,
};
httpd_register_uri_handler(server, &echo);
// static const httpd_uri_t upgrade_post = {
// .uri = "/upload/*",
// .method = HTTP_POST,
// .handler = upgrade_post_handler,
// .user_ctx = NULL
// };
// httpd_register_uri_handler(server, &upgrade_post);
// httpd_uri_t favicon_page = {
// .uri = "/favicon.ico", //192.168.1.4
// .method = HTTP_GET,
// .handler = favicon_get_handler,
// /* Let's pass response string in user
// * context to demonstrate it's usage */
// .user_ctx = server_data
// };
// httpd_register_uri_handler(server, &favicon_page);
return ESP_OK;
}