增加网页配网界面,实现数据下发到ESP32
This commit is contained in:
parent
2c21a0b322
commit
29d8f6aaec
11
Partitions_4M.csv
Normal file
11
Partitions_4M.csv
Normal file
@ -0,0 +1,11 @@
|
||||
# ESP-IDF Partition Table
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
nvs, data, nvs, 0x9000, 0x4000,
|
||||
otadata, data, ota, 0xd000, 0x2000,
|
||||
phy_init, data, phy, 0xf000, 0x1000,
|
||||
ota_0, app, ota_0, 0x10000, 896K,
|
||||
ota_1, app, ota_1, 0xf0000, 896K,
|
||||
Config, data, 0x40, 0x1d0000,4K,
|
||||
storage, data, spiffs, 0x1d1000,1M,
|
||||
html, data, 0x41, 0x2d1000,64K,
|
||||
user, data, 0x45, 0x2e1000,1148K,
|
||||
|
@ -1,10 +1,12 @@
|
||||
|
||||
file(GLOB PORT_SRCS "wifi/*.c" )
|
||||
|
||||
file(GLOB HTML_SRCS "wifi/www/*.html" )
|
||||
file(GLOB HTML_SRCS "wifi/www/*.html" "wifi/file/favicon.ico")
|
||||
|
||||
file(GLOB FILE_SYS_SRCS "file_system/spiffs.c" )
|
||||
|
||||
idf_component_register(SRCS "main.c" ${PORT_SRCS}
|
||||
idf_component_register(SRCS "main.c" ${PORT_SRCS} ${FILE_SYS_SRCS}
|
||||
EMBED_FILES ${HTML_SRCS}
|
||||
INCLUDE_DIRS "."
|
||||
INCLUDE_DIRS "file_system/"
|
||||
INCLUDE_DIRS "wifi/")
|
||||
|
||||
56
main/file_system/dr_sdmmc.c
Normal file
56
main/file_system/dr_sdmmc.c
Normal file
@ -0,0 +1,56 @@
|
||||
#include <sys/stat.h>
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "sdmmc_cmd.h"
|
||||
#include "driver/sdmmc_host.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "dr_sdmmc";
|
||||
#define MOUNT_POINT "/sdcard"
|
||||
|
||||
esp_err_t sdmmc_init(void)
|
||||
{
|
||||
esp_err_t ret;
|
||||
|
||||
// Options for mounting the filesystem.
|
||||
// If format_if_mount_failed is set to true, SD card will be partitioned and
|
||||
// formatted in case when mounting fails.
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = false,
|
||||
.max_files = 5,
|
||||
.allocation_unit_size = 1024 * 1024 *2
|
||||
};
|
||||
sdmmc_card_t *card;
|
||||
const char mount_point[] = MOUNT_POINT;
|
||||
ESP_LOGI(TAG, "Initializing SD card");
|
||||
|
||||
ESP_LOGI(TAG, "Using SDMMC peripheral");
|
||||
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
||||
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
||||
|
||||
slot_config.width = 4;
|
||||
slot_config.clk = 7;
|
||||
slot_config.cmd = 2;
|
||||
slot_config.d0 = 6;
|
||||
slot_config.d1 = 5;
|
||||
slot_config.d2 = 39;
|
||||
slot_config.d3 = 38;
|
||||
slot_config.gpio_cd = 33;
|
||||
slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
|
||||
|
||||
ESP_LOGI(TAG, "Mounting filesystem");
|
||||
ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
|
||||
|
||||
if (ret != ESP_OK) {
|
||||
if (ret == ESP_FAIL) {
|
||||
ESP_LOGE(TAG, "Failed to mount filesystem. "
|
||||
"If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to initialize the card (%s). "
|
||||
"Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGI(TAG, "Filesystem mounted");
|
||||
sdmmc_card_print_info(stdout, card);
|
||||
return ESP_OK;
|
||||
}
|
||||
53
main/file_system/spiffs.c
Normal file
53
main/file_system/spiffs.c
Normal file
@ -0,0 +1,53 @@
|
||||
#include "spiffs.h"
|
||||
|
||||
static const char *TAG = "file System";
|
||||
|
||||
esp_err_t init_spiffs(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing SPIFFS");
|
||||
|
||||
esp_vfs_spiffs_conf_t conf = {
|
||||
.base_path = "/spiffs",
|
||||
.partition_label = NULL,
|
||||
.max_files = 10, // This decides the maximum number of files that can be created on the storage
|
||||
.format_if_mount_failed = true
|
||||
};
|
||||
|
||||
esp_err_t ret = esp_vfs_spiffs_register(&conf);
|
||||
if (ret != ESP_OK) {
|
||||
if (ret == ESP_FAIL) {
|
||||
ESP_LOGE(TAG, "Failed to mount or format filesystem");
|
||||
} else if (ret == ESP_ERR_NOT_FOUND) {
|
||||
ESP_LOGE(TAG, "Failed to find SPIFFS partition");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
size_t total = 0, used = 0;
|
||||
ret = esp_spiffs_info(NULL, &total, &used);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// void Read_file(char* filename, Read_data data,uint16_t size)
|
||||
// {
|
||||
// FILE* f ;
|
||||
|
||||
// f = fopen(filename, "rb");
|
||||
// if (f == NULL) {
|
||||
// ESP_LOGE(FILE_TAG, "Failed to open file for writing");
|
||||
// return;
|
||||
// }
|
||||
// data.size = fread(data.buf,1,size,f);
|
||||
// data.type = SPIFFS;
|
||||
// fclose(f);
|
||||
|
||||
// }
|
||||
|
||||
31
main/file_system/spiffs.h
Normal file
31
main/file_system/spiffs.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef _SPIFFS_H
|
||||
#define _SPIFFS_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_spiffs.h"
|
||||
|
||||
|
||||
#define FILE_TAG "spiffs"
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t type;
|
||||
uint32_t file_size;
|
||||
uint16_t buff_size;
|
||||
char file_name[20];
|
||||
uint8_t buff[256];
|
||||
}spiffs_data_t;
|
||||
|
||||
extern esp_err_t init_spiffs(void);
|
||||
extern esp_err_t sdmmc_init(void);
|
||||
// void Read_file(char* filename, Read_data data,uint16_t size);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
10
main/main.c
10
main/main.c
@ -32,6 +32,7 @@
|
||||
#include "driver/gpio.h"
|
||||
|
||||
#include "wifi_task.h"
|
||||
#include "spiffs.h"
|
||||
|
||||
|
||||
#define EX_UART_NUM UART_NUM_0
|
||||
@ -171,9 +172,16 @@ static void uart_event_task(void *pvParameters)
|
||||
void app_main(void)
|
||||
{
|
||||
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase()); // 擦除 NVS 分区
|
||||
err = nvs_flash_init(); // 重新初始化
|
||||
}
|
||||
ESP_ERROR_CHECK(err);
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
init_spiffs();
|
||||
/* Configure parameters of an UART driver,
|
||||
* communication pins and install the driver */
|
||||
uart_config_t uart_config = {
|
||||
|
||||
BIN
main/wifi/file/favicon.ico
Normal file
BIN
main/wifi/file/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 104 KiB |
@ -8,8 +8,8 @@ static const char *TAG = "wifi_html";
|
||||
|
||||
static esp_err_t index_get_handler(httpd_req_t *req)
|
||||
{
|
||||
extern const unsigned char wifi_config_start[] asm("_binary_wifi_html_start");
|
||||
extern const unsigned char wifi_config_end[] asm("_binary_wifi_html_end");
|
||||
extern const unsigned char wifi_config_start[] asm("_binary_wifi_config_html_start");
|
||||
extern const unsigned char wifi_config_end[] asm("_binary_wifi_config_html_end");
|
||||
const size_t wifi_config_size = (wifi_config_end - wifi_config_start);
|
||||
|
||||
/* Add file upload form and script which on execution sends a POST request to /upload */
|
||||
@ -56,9 +56,22 @@ static esp_err_t Save_file_handler(httpd_req_t *req)
|
||||
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)
|
||||
{
|
||||
extern const unsigned char favicon_ico_start[] asm("_binary_favicon_ico_start");
|
||||
extern const unsigned char favicon_ico_end[] asm("_binary_favicon_ico_end");
|
||||
const size_t favicon_ico_size = (favicon_ico_end - favicon_ico_start);
|
||||
httpd_resp_set_type(req, "image/x-icon");
|
||||
httpd_resp_send(req, (const char *)favicon_ico_start, favicon_ico_size);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
char wifi_name[20] = {0};
|
||||
char wifi_password[12] = {0};
|
||||
static esp_err_t echo_post_handler(httpd_req_t *req)
|
||||
static esp_err_t wifi_config_handler(httpd_req_t *req)
|
||||
{
|
||||
char buf[100];
|
||||
// char ssid[10];
|
||||
@ -77,8 +90,8 @@ static esp_err_t echo_post_handler(httpd_req_t *req)
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/* Send back the same data */
|
||||
httpd_resp_send_chunk(req, buf, ret);
|
||||
// /* Send back the same data */
|
||||
// httpd_resp_send_chunk(req, buf, ret);
|
||||
remaining -= ret;
|
||||
|
||||
cJSON *root = cJSON_Parse(buf);
|
||||
@ -105,7 +118,7 @@ static esp_err_t echo_post_handler(httpd_req_t *req)
|
||||
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);
|
||||
@ -137,13 +150,19 @@ static esp_err_t echo_post_handler(httpd_req_t *req)
|
||||
}
|
||||
|
||||
// End response
|
||||
httpd_resp_send_chunk(req, NULL, 0);
|
||||
// 结束响应
|
||||
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();
|
||||
// if(wifi_write == true)esp_restart();
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
@ -279,7 +298,7 @@ esp_err_t start_web_server(void)
|
||||
httpd_uri_t echo = {
|
||||
.uri = "/config",
|
||||
.method = HTTP_POST,
|
||||
.handler = echo_post_handler,
|
||||
.handler = wifi_config_handler,
|
||||
.user_ctx = server_data,
|
||||
};
|
||||
httpd_register_uri_handler(server, &echo);
|
||||
@ -292,5 +311,14 @@ esp_err_t start_web_server(void)
|
||||
// };
|
||||
// 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;
|
||||
}
|
||||
@ -40,7 +40,7 @@
|
||||
<body>
|
||||
<div class="dowebok">
|
||||
<h2>ESP32 WIFI 配网</h2>
|
||||
<form style='text-align: center;padding-top: 20px' name='input' action='/' method='POST'>
|
||||
<form style='text-align: center;padding-top: 20px' name='input' action='/' method='POST' onsubmit="send_wifi(event)">
|
||||
<div class="form-item">
|
||||
<input id="username" type="text" name='ssid' autocomplete="off" placeholder="WiFi名称">
|
||||
</div>
|
||||
@ -82,3 +82,65 @@
|
||||
</style>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
<script>
|
||||
function send_wifi(event) {
|
||||
// 通过 event.submitter 获取触发提交的按钮
|
||||
let buttonId = event.submitter.id;
|
||||
console.log("触发提交的按钮 ID:", buttonId);
|
||||
|
||||
if (buttonId === "set_button") {//只有设置按钮才能阻塞
|
||||
event.preventDefault(); // 阻止表单提交
|
||||
console.log("send_wifi() 函数被调用了");
|
||||
|
||||
// 获取 WiFi 名称和密码
|
||||
let wifi_name = document.getElementById("username").value;
|
||||
let wifi_password = document.getElementById("password").value;
|
||||
let retry_count = document.getElementById("retry").value;
|
||||
|
||||
|
||||
|
||||
console.log("WiFi 名称:", wifi_name);
|
||||
console.log("WiFi 密码:", wifi_password);
|
||||
console.log("重连次数:", retry_count);
|
||||
|
||||
// 你可以在这里添加 AJAX 代码将数据发送到服务器
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.open("POST", "/config", true);
|
||||
xhttp.setRequestHeader("Content-Type", "application/json"); // 确保以 JSON 格式发送
|
||||
xhttp.onreadystatechange = function() {
|
||||
if (xhttp.readyState == 4) {
|
||||
if (xhttp.status == 200) {
|
||||
// 解析 JSON 响应
|
||||
let response = JSON.parse(xhttp.responseText);
|
||||
console.log("服务器响应:", response);
|
||||
if (response.status === "success") {
|
||||
alert("Wi-Fi 配置成功!");
|
||||
} else {
|
||||
alert("Wi-Fi 配置失败!");
|
||||
}
|
||||
}
|
||||
else if (xhttp.status == 0) {
|
||||
alert("服务器连接被关闭,重新加载页面!");
|
||||
location.reload()
|
||||
} else {
|
||||
alert(xhttp.status + " 错误!\n" + xhttp.responseText);
|
||||
location.reload()
|
||||
}
|
||||
}
|
||||
};
|
||||
// 发送 JSON 数据
|
||||
var data = {
|
||||
"wifi_name": wifi_name,
|
||||
"wifi_code": wifi_password,
|
||||
"retry_count": retry_count
|
||||
};
|
||||
xhttp.send(JSON.stringify(data));
|
||||
}
|
||||
|
||||
}
|
||||
// function setpath() {
|
||||
// var default_path = document.getElementById("newfile").files[0].name;
|
||||
// document.getElementById("filepath").value = default_path;
|
||||
// }
|
||||
</script>
|
||||
14
tools/esptools.py
Normal file
14
tools/esptools.py
Normal file
@ -0,0 +1,14 @@
|
||||
import esptool
|
||||
|
||||
|
||||
#/Users/yu/.espressif/python_env/idf5.3_py3.9_env/bin/python /Users/yu/esp/v5.3.2/esp-idf/components/esptool_py/esptool/esptool.py -p /dev/tty.wchusbserial54CE0416791 -b 460800 --before default_reset --after hard_reset --chip esp32s3 write_flash --flash_mode dio --flash_freq 80m --flash_size 4MB 0x0 bootloader/bootloader.bin 0x10000 usb_serial_jtag_echo.bin 0x8000 partition_table/partition-table.bin 0xd000 ota_data_initial.bin
|
||||
|
||||
|
||||
esptool.main([
|
||||
"--chip", "esp32c3",
|
||||
"--port", "/dev/tty.usbmodem11401",
|
||||
"--baud", "460800",
|
||||
"write_flash",
|
||||
"-z",
|
||||
"0x2d1000", "/Users/yu/esp/esp32c3-usb_serial/main/wifi/www/wifi_config.html"
|
||||
])
|
||||
Loading…
Reference in New Issue
Block a user