1

我正在使用 ESP-IDF 和 PlatformIO 为 ESP32 编写一个文件服务 http 服务器,但我无法将数据上传到 SPIFFS 工作。我正在尝试将 html 和 favicon 发送到 flash,以便可以在 http 上提供。

服务器的代码取自示例https://github.com/espressif/esp-idf/tree/master/examples/protocols/http_server/file_serving。明显的区别是示例项目仅使用 ESP-IDF 工具(没有 platformio),并且在示例中,数据文件与源文件位于同一目录中,在我的项目中,我为 /src 和 /data 分开了目录。SPIFFS 是使用自定义分区表配置的。

我遵循 PlatformIO 文档(https://docs.platformio.org/en/latest/platforms/espressif32.html?utm_source=platformio&utm_medium=piohome)以及 ESP(https://docs.espressif.com )的说明/projects/esp-idf/en/latest/esp32/api-guides/build-system.html#embedding-binary-data)。

我有自定义 partitions.csv 文件(与示例相同)+ 更改 menuconfig 以使用它。

在 platformio.ini 中,我添加了:

board_build.partitions = partitions.csv
board_build.embed_txtfiles =
    data/favicon.ico
    data/upload_script.html

我还更改了项目 CMakeLists 文件以嵌入如下数据:

cmake_minimum_required(VERSION 3.16.0)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(HTTP-server)
target_add_binary_data(HTTP-server.elf "data/favicon.ico" TEXT)
target_add_binary_data(HTTP-server.elf "data/upload_script.html" TEXT)

/src/CMakeLists 保持不变:

FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
idf_component_register(SRCS ${app_sources})

但即使有所有这些配置,当我尝试在 file_server.c 中使用这些数据时,如下所示:

extern const unsigned char favicon_ico_start[] asm("_binary_favicon_ico_start");
extern const unsigned char favicon_ico_end[] asm("_binary_favicon_ico_end");
extern const unsigned char upload_script_start[] asm("_binary_upload_script_html_start");
extern const unsigned char upload_script_end[] asm("_binary_upload_script_html_end"); 

我收到编译错误

Linking .pio/build/nodemcu-32s/firmware.elf
/home/artur/.platformio/packages/toolchain-xtensa32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: .pio/build/nodemcu-32s/src/file_server.o:(.literal.http_resp_dir_html+0x14): undefined reference to `_binary_upload_script_html_end'
/home/artur/.platformio/packages/toolchain-xtensa32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: .pio/build/nodemcu-32s/src/file_server.o:(.literal.http_resp_dir_html+0x18): undefined reference to `_binary_upload_script_html_start'
/home/artur/.platformio/packages/toolchain-xtensa32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: .pio/build/nodemcu-32s/src/file_server.o:(.literal.favicon_get_handler+0x0): undefined reference to `_binary_favicon_ico_end'
/home/artur/.platformio/packages/toolchain-xtensa32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: .pio/build/nodemcu-32s/src/file_server.o:(.literal.favicon_get_handler+0x4): undefined reference to `_binary_favicon_ico_start'
collect2: error: ld returned 1 exit status
*** [.pio/build/nodemcu-32s/firmware.elf] Error 1
================================================================================= [FAILED] Took 65.20 seconds =================================================================================

我尝试将外部定义更改为:

extern const unsigned char favicon_ico_start[] asm("_binary_data_favicon_ico_start");

但这并没有改变什么。

此外,在运行“构建文件系统映像”任务时,我收到此错误:

*** [.pio/build/nodemcu-32s/spiffs.bin] Implicit dependency `data/favicon' not found, needed by target `.pio/build/nodemcu-32s/spiffs.bin'.
================================================================================= [FAILED] Took 5.70 seconds =================================================================================
The terminal process "platformio 'run', '--target', 'buildfs', '--environment', 'nodemcu-32s'" terminated with exit code: 1.

任何帮助将不胜感激,因为我觉得我做了文档所述的一切。

4

2 回答 2

0

我怀疑您需要将它们声明为 BINARY 而不是 TEXT。TEXT 创建一个以 null 结尾的字符串,它可能不会生成_binary_..._end别名。

target_add_binary_data(HTTP-server.elf "data/favicon.ico" BINARY)
target_add_binary_data(HTTP-server.elf "data/upload_script.html" BINARY)

您的 SPIFFS 图像生成也有问题。您知道 CMake 宏target_add_binary_data()并将idf_component_register(... EMBED_TXTFILES...)这些内容仅嵌入到应用程序二进制文件中,对吗?您不能使用它们将内容添加到预先生成的 SPIFFS 分区。为此,您需要使用spiffsgen-py脚本。

于 2021-08-25T16:14:25.060 回答
0

问题是我对将文件嵌入应用程序和将内容发送到 SPIFFS 分区之间的区别有点困惑。

解决方案是将 .html 和 .ico 文件从 /data 移动到 /src。我认为这背后的原因是这段代码:asm("_binary_favicon_ico_start")无法引用其他目录中的文件,但我不确定。我还将项目 CMakeFile 反转为默认值,并将这一行添加到 /src/CMakeFile:idf_component_register(SRCS ${app_sources} EMBED_FILES "favicon.ico" "upload_script.html")

我也不需要创建任何文件系统映像,因为 SPIFFS 分区只需要网络服务器本身。

于 2021-08-26T08:32:52.913 回答