httpd_start and spiffs

Przemek
Posts: 2
Joined: Fri Jan 11, 2019 9:27 pm

httpd_start and spiffs

Postby Przemek » Fri Jan 11, 2019 10:15 pm

I am trying to add spiffs functionality to http_server/simple example. For some reason adding spiffs initialization into server start routine breaks the code and it no longer responds to requests from browser, so if I type xx.xx.xx.xx/hello into browser bar I get timeout. The code is shown below. It is literally http_server example code with few lines from spiffs example inserted (plus relevant #include, not shown here)
If I added the spiffs initialization earlier, at the beginning of the function, the example runs correctly. Just this particular order is problematic. Program displays the message "Registering URI handlers" and the returned value is not-NULL so it is not early return from the function. The partition table is set up, spiffs partition is uploaded and files can be read.

I probably shall place spiff initialization even earlier, before initializing wifi in app_main, but since I noticed there is a problem here, can someone explain to me what are hidden interactions between spiffs and httpd and what can go wrong ?
Thanks in advance.
  1. httpd_handle_t start_webserver(void)
  2. {
  3.     httpd_handle_t server = NULL;
  4.     httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  5.  
  6.     // Start the httpd server
  7.     ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  8.     if (httpd_start(&server, &config) == ESP_OK) {
  9.  
  10.  
  11.  
  12.         ESP_LOGI(TAG, "Initializing SPIFFS");
  13.             esp_vfs_spiffs_conf_t conf = {
  14.               .base_path = "/spiffs",
  15.               .partition_label = NULL,
  16.               .max_files = 5,
  17.               .format_if_mount_failed = false
  18.             };
  19.  
  20.             esp_err_t ret = esp_vfs_spiffs_register(&conf);
  21.  
  22.             if (ret != ESP_OK) {
  23.                 if (ret == ESP_FAIL) {
  24.                     ESP_LOGE(TAG, "Failed to mount or format filesystem");
  25.                 } else if (ret == ESP_ERR_NOT_FOUND) {
  26.                     ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  27.                 } else {
  28.                     ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  29.                 }
  30.                 return NULL;
  31.             }
  32.  
  33.  
  34.  
  35.  
  36.         // Set URI handlers
  37.         ESP_LOGI(TAG, "Registering URI handlers");
  38.         httpd_register_uri_handler(server, &hello);
  39.         httpd_register_uri_handler(server, &echo);
  40.         httpd_register_uri_handler(server, &ctrl);
  41.         return server;
  42.     }
  43.  
  44.     ESP_LOGI(TAG, "Error starting server!");
  45.     return NULL;
  46. }
  1. Partition table binary generated. Contents:
  2. *******************************************************************************
  3. # Espressif ESP32 Partition Table
  4. # Name, Type, SubType, Offset, Size, Flags
  5. nvs,data,nvs,0x9000,24K,
  6. phy_init,data,phy,0xf000,4K,
  7. factory,app,factory,0x10000,1M,
  8. www,data,spiffs,0x110000,64K,
  9. *******************************************************************************
  1. ../../../tools/mkspiffs/mkspiffs --version
  2. mkspiffs ver. 0.2.3-5-g9f4acb5
  3. Build configuration name: generic
  4. SPIFFS ver. 0.3.7-5-gf5e26c4
  5. Extra build flags: (none)
  6. SPIFFS configuration:
  7.   SPIFFS_OBJ_NAME_LEN: 32
  8.   SPIFFS_OBJ_META_LEN: 0
  9.   SPIFFS_USE_MAGIC: 1
  10.   SPIFFS_USE_MAGIC_LENGTH: 1
  11.   SPIFFS_ALIGNED_OBJECT_INDEX_TABLES: 0
  1. ##
  2. # SPIFFS Configuration
  3. #
  4. CONFIG_SPIFFS_MAX_PARTITIONS=3
  5.  
  6. #
  7. # SPIFFS Cache Configuration
  8. #
  9. CONFIG_SPIFFS_CACHE=y
  10. CONFIG_SPIFFS_CACHE_WR=y
  11. CONFIG_SPIFFS_CACHE_STATS=
  12. CONFIG_SPIFFS_PAGE_CHECK=y
  13. CONFIG_SPIFFS_GC_MAX_RUNS=10
  14. CONFIG_SPIFFS_GC_STATS=
  15. CONFIG_SPIFFS_PAGE_SIZE=256
  16. CONFIG_SPIFFS_OBJ_NAME_LEN=32
  17. CONFIG_SPIFFS_USE_MAGIC=y
  18. CONFIG_SPIFFS_USE_MAGIC_LENGTH=y
  19. CONFIG_SPIFFS_META_LENGTH=0
  20.  
  21. #
  22. # Debug Configuration
  23. #
  24. CONFIG_SPIFFS_DBG=
  25. CONFIG_SPIFFS_API_DBG=
  26. CONFIG_SPIFFS_GC_DBG=
  27. CONFIG_SPIFFS_CACHE_DBG=
  28. CONFIG_SPIFFS_CHECK_DBG=
  29. CONFIG_SPIFFS_TEST_VISUALISATION=

Przemek
Posts: 2
Joined: Fri Jan 11, 2019 9:27 pm

Re: httpd_start and spiffs

Postby Przemek » Sat Jan 12, 2019 10:14 am

More info:

Setting log level to DEBUG or VERBOSE, globally or locally on httpd_main.c removes the issue.
It looks like a potential bug report.

That's how log looks like on DEBUG level - that's CORRECT operation:
  1. I (8258) event: sta ip: 192.168.89.101, mask: 255.255.255.0, gw: 192.168.89.1
  2. I (8258) APP: SYSTEM_EVENT_STA_GOT_IP
  3. I (8258) APP: Got IP: '192.168.89.101'
  4. I (8258) APP: Starting server on port: '80'
  5. I (8268) APP: Initializing SPIFFS
  6. D (8268) httpd: httpd_thread: web server started
  7. D (8278) httpd: httpd_server: doing select maxfd+1 = 56
  8. I (8278) APP: Registering URI handlers
  9. D (8288) httpd_uri: httpd_register_uri_handler: [0] installed /hello
  10. D (8288) httpd_uri: httpd_find_uri_handler: [0] = /hello
  11. D (8298) httpd_uri: httpd_register_uri_handler: [0] exists /hello
  12. D (8308) httpd_uri: httpd_register_uri_handler: [1] installed /echo
  13. D (8308) httpd_uri: httpd_find_uri_handler: [0] = /hello
  14. D (8318) httpd_uri: httpd_find_uri_handler: [1] = /echo
  15. D (8318) httpd_uri: httpd_register_uri_handler: [0] exists /hello
  16. D (8328) httpd_uri: httpd_register_uri_handler: [1] exists /echo
  17. D (8328) httpd_uri: httpd_register_uri_handler: [2] installed /ctrl
  18. I (8338) APP: URI handlers registered
  19. D (12068) httpd: httpd_server: processing listen socket 54
  20. D (12078) httpd: httpd_accept_conn: newfd = 57
  21. D (12078) httpd: httpd_accept_conn: complete
  22. D (12078) httpd: httpd_server: doing select maxfd+1 = 58
  23. D (12078) httpd: httpd_server: processing socket 57
  24. D (12088) httpd_uri: httpd_uri: request for /hello with type 1
  25. D (12088) httpd_uri: httpd_find_uri_handler2: [0] = /hello
  26. I (12098) APP: Found header => Host: 192.168.89.101
  27. I (12118) APP: Request headers lost
  28. D (12118) httpd: httpd_server: doing select maxfd+1 = 58

devill
Posts: 1
Joined: Tue Apr 30, 2019 1:30 pm

Re: httpd_start and spiffs

Postby devill » Tue Apr 30, 2019 1:33 pm

Initializing the storage before the http server also make it works for me

Who is online

Users browsing this forum: No registered users and 117 guests