您可以直接在CMakeLists.txt
. 例如,要添加文件favicon.jpg
图像,在 myCMakeLists.txt
的同一目录中main.c
:
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "."
EMBED_FILES "favicon.jpg")
在 main.c 的某个地方:
/* The favicon */
static esp_err_t favicon_handler(httpd_req_t *req)
{
extern const char favicon_start[] asm("_binary_favicon_jpg_start");
extern const char favicon_end[] asm("_binary_favicon_jpg_end");
size_t favicon_len = favicon_end - favicon_start;
httpd_resp_set_type(req, "image/jpeg");
httpd_resp_send(req, favicon_start, favicon_len);
return ESP_OK;
}
static const httpd_uri_t favicon_uri = {
.uri = "/favicon.ico",
.method = HTTP_GET,
.handler = favicon_handler,
.user_ctx = NULL
};
您可以通过这种方式添加所需的任意数量的文件,文本、html、json 等...(当然要考虑设备内存)。