C ESP32“;没有这样的文件或目录;对于本机ESP-IDF组件
我正在尝试将文件_服务示例移植为使用HTTPS 我试图将spiff文件服务器功能移动到esp idf中现有的https_服务器示例中,但出现错误:httpd_server_init:创建ctrl套接字时出错(112) 我意识到这可能不是最简单的方法,相反,我应该重新编写原始文件的示例代码来使用https。启动服务器的函数位于文件\u server.c中:C ESP32“;没有这样的文件或目录;对于本机ESP-IDF组件,c,https,cmake,compiler-errors,esp32,C,Https,Cmake,Compiler Errors,Esp32,我正在尝试将文件_服务示例移植为使用HTTPS 我试图将spiff文件服务器功能移动到esp idf中现有的https_服务器示例中,但出现错误:httpd_server_init:创建ctrl套接字时出错(112) 我意识到这可能不是最简单的方法,相反,我应该重新编写原始文件的示例代码来使用https。启动服务器的函数位于文件\u server.c中: /* Function to start the file server */ esp_err_t start_file_server(con
/* Function to start the file server */
esp_err_t start_file_server(const char *base_path)
{
static struct file_server_data *server_data = NULL;
/* Validate file storage base path */
if (!base_path || strcmp(base_path, "/spiffs") != 0) {
ESP_LOGE(TAG, "File server presently supports only '/spiffs' as base path");
return ESP_ERR_INVALID_ARG;
}
if (server_data) {
ESP_LOGE(TAG, "File server already started");
return ESP_ERR_INVALID_STATE;
}
/* Allocate memory for server data */
server_data = calloc(1, sizeof(struct file_server_data));
if (!server_data) {
ESP_LOGE(TAG, "Failed to allocate memory for server data");
return ESP_ERR_NO_MEM;
}
strlcpy(server_data->base_path, base_path,
sizeof(server_data->base_path));
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
/* Use the URI wildcard matching function in order to
* allow the same handler to respond to multiple different
* target URIs which match the wildcard scheme */
config.uri_match_fn = httpd_uri_match_wildcard;
ESP_LOGI(TAG, "Starting HTTP Server");
if (httpd_start(&server, &config) != ESP_OK) {
ESP_LOGE(TAG, "Failed to start file server!");
return ESP_FAIL;
}
/* URI handler for getting uploaded files */
httpd_uri_t file_download = {
.uri = "/*", // Match all URIs of type /path/to/file
.method = HTTP_GET,
.handler = download_get_handler,
.user_ctx = server_data // Pass server data as context
};
httpd_register_uri_handler(server, &file_download);
/* URI handler for uploading files to server */
httpd_uri_t file_upload = {
.uri = "/upload/*", // Match all URIs of type /upload/path/to/file
.method = HTTP_POST,
.handler = upload_post_handler,
.user_ctx = server_data // Pass server data as context
};
httpd_register_uri_handler(server, &file_upload);
/* URI handler for deleting files from server */
httpd_uri_t file_delete = {
.uri = "/delete/*", // Match all URIs of type /delete/path/to/file
.method = HTTP_POST,
.handler = delete_post_handler,
.user_ctx = server_data // Pass server data as context
};
httpd_register_uri_handler(server, &file_delete);
return ESP_OK;
}
主函数引用启动文件服务器,如下所示:
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
/* Initialize file storage */
ESP_ERROR_CHECK(init_spiffs());
/* Start the file server */
ESP_ERROR_CHECK(start_file_server("/spiffs"));
}
我运行了https_服务器示例,我的https_mbedtls客户端连接到该示例,但未验证我的自签名证书的有效性。该https_mbedtls客户端将用于连接文件服务器(https)
原始文件服务器示例包括esp\u http\u server.h。我假设我需要包含esp_https_server.h
以下是我对其所做的更改:
/* Function to start the file server */
esp_err_t start_file_server(const char *base_path)
{
static struct file_server_data *server_data = NULL;
/* Validate file storage base path */
if (!base_path || strcmp(base_path, "/spiffs") != 0) {
ESP_LOGE(TAG, "File server presently supports only '/spiffs' as base path");
return ESP_ERR_INVALID_ARG;
}
if (server_data) {
ESP_LOGE(TAG, "File server already started");
return ESP_ERR_INVALID_STATE;
}
/* Allocate memory for server data */
server_data = calloc(1, sizeof(struct file_server_data));
if (!server_data) {
ESP_LOGE(TAG, "Failed to allocate memory for server data");
return ESP_ERR_NO_MEM;
}
strlcpy(server_data->base_path, base_path,
sizeof(server_data->base_path));
//***********
httpd_handle_t server = NULL;
httpd_ssl_config_t config = HTTPD_SSL_CONFIG_DEFAULT();
//***********
extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
conf.cacert_pem = cacert_pem_start;
conf.cacert_len = cacert_pem_end - cacert_pem_start;
extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
conf.prvtkey_pem = prvtkey_pem_start;
conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
//***********
/* Use the URI wildcard matching function in order to
* allow the same handler to respond to multiple different
* target URIs which match the wildcard scheme */
config.uri_match_fn = httpd_uri_match_wildcard;
ESP_LOGI(TAG, "Starting HTTPS Server");
//***********
if (httpd_ssl_start(&server, &config) != ESP_OK) {
ESP_LOGE(TAG, "Failed to start file server!");
return ESP_FAIL;
}
/* URI handler for getting uploaded files */
httpd_uri_t file_download = {
.uri = "/*", // Match all URIs of type /path/to/file
.method = HTTP_GET,
.handler = download_get_handler,
.user_ctx = server_data // Pass server data as context
};
httpd_register_uri_handler(server, &file_download);
/* URI handler for uploading files to server */
httpd_uri_t file_upload = {
.uri = "/upload/*", // Match all URIs of type /upload/path/to/file
.method = HTTP_POST,
.handler = upload_post_handler,
.user_ctx = server_data // Pass server data as context
};
httpd_register_uri_handler(server, &file_upload);
/* URI handler for deleting files from server */
httpd_uri_t file_delete = {
.uri = "/delete/*", // Match all URIs of type /delete/path/to/file
.method = HTTP_POST,
.handler = delete_post_handler,
.user_ctx = server_data // Pass server data as context
};
httpd_register_uri_handler(server, &file_delete);
return ESP_OK;
}
当我通过ESP-IDF命令行执行flash时,我得到以下错误:
../main/file_server.c:22:10:致命错误:esp_https_server.h:没有这样的文件或目录
#包括“esp_https_server.h”
^~~~~~~~~~~~~~~~~~~~
编译终止
为了解决这个问题,我尝试将“REQUIRES esp_https_server”添加到主目录中的CMakeLists.txt中。这将以前的错误替换为
../main/main.c:16:10:致命错误:esp_spiffs.h:没有这样的文件或目录
#包括“esp_spiffs.h”
^~~~~~~~~~~~~~
编译终止
在我的CMakeLists.txt如下所示之前,如果找不到其他要求,也会发生同样的情况:
idf_component_register(SRCS "main.c" "file_server.c"
INCLUDE_DIRS "."
EMBED_FILES "favicon.ico" "upload_script.html"
EMBED_TXTFILES "certs/cacert.pem"
"certs/prvtkey.pem"
REQUIRES esp_https_server spiffs nvs_flash protocol_examples_common)
我在这一点上得到的错误是再次无法找到esp_https_server.h
../main/file_server.c:22:10:致命错误:esp_https_server.h:没有这样的文件或目录
#包括“esp_https_server.h”
^~~~~~~~~~~~~~~~~~~~
编译终止
我希望您能理解我所处的令人沮丧的困境,我将非常感谢您对如何修复“没有这样的文件或目录”错误的任何意见,以及如何将此示例移植到https的任何提示。谢谢。我的指导教授研究了这个问题并找到了解决办法。以下是需要进行的更改: 在sdkconfig文件中包括以下行: 配置\u ESP\u HTTPS\u服务器\u启用=y 文件_server.c中的“config”而不是“conf”,http服务器的配置是https配置的一个子组件,需要在“config”后面加一个“httpd.”:
extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
config.cacert_pem = cacert_pem_start;
config.cacert_len = cacert_pem_end - cacert_pem_start;
extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
config.prvtkey_pem = prvtkey_pem_start;
config.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
//***********
/* Use the URI wildcard matching function in order to
* allow the same handler to respond to multiple different
* target URIs which match the wildcard scheme */
config.httpd.uri_match_fn = httpd_uri_match_wildcard;