將ESP32——WEB服務(wù)程序測(cè)試項(xiàng)目移植到一個(gè)現(xiàn)有項(xiàng)目中,現(xiàn)有項(xiàng)目包括基于固定IP的WIFI連接、OTA升級(jí)、Websocket等功能。
因模塊采用ESP32-WROVER-E(4MB),故分區(qū)表內(nèi)容修改如下(后發(fā)現(xiàn)問(wèn)題,下面會(huì)提到)。
# 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, 1M ota_1, app, ota_1, , 1M www, data, spiffs, , 1M
并修改配置如下圖項(xiàng)。
將restful_server項(xiàng)目front目錄及其下所有內(nèi)容復(fù)制到現(xiàn)有項(xiàng)目根目錄下。
2.3 復(fù)制rest_server.c文件到現(xiàn)有項(xiàng)目main目錄下并在main目錄下CMakeLists.txt文件中添加 "rest_server.c"
idf_component_register(SRCS "ota.c" "wifi.c" "websocket.c" "rest_server.c" "main.c"
INCLUDE_DIRS "."
# Embed the server root certificate into the final binary
EMBED_TXTFILES ${project_dir}/server_certs/ca_cert.pem)
2.4 復(fù)制esp_rest_main.c文件中如下內(nèi)容到現(xiàn)有項(xiàng)目main.c文件中//此處略去了必要的頭文件和變量定義
esp_err_t start_rest_server(const char *base_path);
#if CONFIG_EXAMPLE_WEB_DEPLOY_SF
esp_err_t init_fs(void)
{
esp_vfs_spiffs_conf_t conf = {
.base_path = CONFIG_EXAMPLE_WEB_MOUNT_POINT,
.partition_label = NULL,
.max_files = 5,
.format_if_mount_failed = false
};
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));
} else {
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
}
return ESP_OK;
}
#endif
void app_main(void)
{
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());
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
IO_Init();
ip_set();
wifi_init_sta();
xTaskCreate(&advanced_ota_example_task, "advanced_ota_example_task", 1024 * 8, service_ip, 5, NULL);
websocket_app_start(service_ip);
ESP_ERROR_CHECK(init_fs());
ESP_ERROR_CHECK(start_rest_server(CONFIG_EXAMPLE_WEB_MOUNT_POINT));
// tasklist_show();
while (1)
{
LED_Run();
Key_Read();
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
2.5 復(fù)制main目錄下Kconfig.projbuild中如下內(nèi)容到現(xiàn)有項(xiàng)目對(duì)應(yīng)文件中注:項(xiàng)目Kconfig.projbuild中原來(lái)的內(nèi)容因?qū)嶋H未用到,故刪除了。
menu "Example Configuration"
config EXAMPLE_MDNS_HOST_NAME
string "mDNS Host Name"
default "esp-home"
help
Specify the domain name used in the mDNS service.
Note that webpage also take it as a part of URL where it will send GET/POST requests to.
choice EXAMPLE_WEB_DEPLOY_MODE
prompt "Website deploy mode"
default EXAMPLE_WEB_DEPLOY_SEMIHOST
help
Select website deploy mode.
You can deploy website to host, and ESP32 will retrieve them in a semihost way (JTAG is needed).
You can deploy website to SD card or SPI flash, and ESP32 will retrieve them via SDIO/SPI interface.
Detailed operation steps are listed in the example README file.
config EXAMPLE_WEB_DEPLOY_SEMIHOST
bool "Deploy website to host (JTAG is needed)"
help
Deploy website to host.
It is recommended to choose this mode during developing.
config EXAMPLE_WEB_DEPLOY_SD
depends on IDF_TARGET_ESP32
bool "Deploy website to SD card"
help
Deploy website to SD card.
Choose this production mode if the size of website is too large (bigger than 2MB).
config EXAMPLE_WEB_DEPLOY_SF
bool "Deploy website to SPI Nor Flash"
help
Deploy website to SPI Nor Flash.
Choose this production mode if the size of website is small (less than 2MB).
endchoice
if EXAMPLE_WEB_DEPLOY_SEMIHOST
config EXAMPLE_HOST_PATH_TO_MOUNT
string "Host path to mount (e.g. absolute path to web dist directory)"
default "PATH-TO-WEB-DIST_DIR"
help
When using semihost in ESP32, you should specify the host path which will be mounted to VFS.
Note that only absolute path is acceptable.
endif
config EXAMPLE_WEB_MOUNT_POINT
string "Website mount point in VFS"
default "/www"
help
Specify the mount point in VFS.
endmenu
保存后,重新打開(kāi)配置出現(xiàn)如下選項(xiàng),并做如下設(shè)置
if(CONFIG_EXAMPLE_WEB_DEPLOY_SF)
set(WEB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../front/web-demo")
if(EXISTS ${WEB_SRC_DIR}/dist)
spiffs_create_partition_image(www ${WEB_SRC_DIR}/dist FLASH_IN_PROJECT)
else()
message(FATAL_ERROR "${WEB_SRC_DIR}/dist doesn't exit. Please run 'npm run build' in ${WEB_SRC_DIR}")
endif()
endif()
2.7 復(fù)制根目錄下Makefilet中如下內(nèi)容到現(xiàn)有項(xiàng)目對(duì)應(yīng)文件中ifdef CONFIG_EXAMPLE_WEB_DEPLOY_SF
WEB_SRC_DIR = $(shell pwd)/front/web-demo
ifneq ($(wildcard $(WEB_SRC_DIR)/dist/.*),)
SPIFFS_IMAGE_FLASH_IN_PROJECT := 1
$(eval $(call spiffs_create_partition_image,www,$(WEB_SRC_DIR)/dist))
else
$(error $(WEB_SRC_DIR)/dist doesn't exist. Please run 'npm run build' in $(WEB_SRC_DIR))
endif
endif
2.8 修改配置如下部分: ?
此處文件名長(zhǎng)度如設(shè)置太小,編譯項(xiàng)目時(shí)會(huì)報(bào)錯(cuò)(前端部分生成的文件名比較長(zhǎng))。
三、調(diào)試項(xiàng)目修改完成后,編譯出現(xiàn)下圖錯(cuò)誤
后來(lái)發(fā)現(xiàn)是因?yàn)榍岸松傻膁ist目錄內(nèi)容占用存儲(chǔ)空間超過(guò)1M,調(diào)整分區(qū)大小后正常。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧