diff --git a/Partitions_4M.csv b/Partitions_4M.csv new file mode 100644 index 0000000..05cc121 --- /dev/null +++ b/Partitions_4M.csv @@ -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, \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index cecdbf6..f8cb5cc 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -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/") diff --git a/main/file_system/dr_sdmmc.c b/main/file_system/dr_sdmmc.c new file mode 100644 index 0000000..4c0811e --- /dev/null +++ b/main/file_system/dr_sdmmc.c @@ -0,0 +1,56 @@ +#include +#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; +} \ No newline at end of file diff --git a/main/file_system/spiffs.c b/main/file_system/spiffs.c new file mode 100644 index 0000000..30eb2e0 --- /dev/null +++ b/main/file_system/spiffs.c @@ -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); + +// } + diff --git a/main/file_system/spiffs.h b/main/file_system/spiffs.h new file mode 100644 index 0000000..53b8964 --- /dev/null +++ b/main/file_system/spiffs.h @@ -0,0 +1,31 @@ +#ifndef _SPIFFS_H +#define _SPIFFS_H + +#include +#include +#include +#include +#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 \ No newline at end of file diff --git a/main/main.c b/main/main.c index 158d0ef..b974aba 100755 --- a/main/main.c +++ b/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 = { diff --git a/main/wifi/file/favicon.ico b/main/wifi/file/favicon.ico new file mode 100644 index 0000000..83643f2 Binary files /dev/null and b/main/wifi/file/favicon.ico differ diff --git a/main/wifi/wifi_html.c b/main/wifi/wifi_html.c index af39c69..68754c3 100644 --- a/main/wifi/wifi_html.c +++ b/main/wifi/wifi_html.c @@ -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

ESP32 WIFI 配网

-
+
@@ -82,3 +82,65 @@ + + \ No newline at end of file diff --git a/tools/esptools.py b/tools/esptools.py new file mode 100644 index 0000000..c90f11a --- /dev/null +++ b/tools/esptools.py @@ -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" +]) \ No newline at end of file