#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;icontent_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; }