Can't send massive data from http server

nikolos
Posts: 2
Joined: Tue May 07, 2019 9:55 pm

Can't send massive data from http server

Postby nikolos » Tue May 07, 2019 10:04 pm

I want to send 5000 byte data as response for GET request. When I send small respond all goes right but once I increase sending data it crashes. How to fix it?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Can't send massive data from http server

Postby WiFive » Wed May 08, 2019 2:52 am

Sorry we are not psychic you have to post your code and crash/backtrace output.

nikolos
Posts: 2
Joined: Tue May 07, 2019 9:55 pm

Re: Can't send massive data from http server

Postby nikolos » Wed May 08, 2019 8:50 pm

The main task is to get 5000 kilobytes every 0.1 seconds from esp32. Is there better way to make it ?
  1. ***ERROR*** A stack overflow in task httpd has been detected.
  2. abort() was called at PC 0x40087e4c on core 0
  3. 0x40087e4c: vApplicationStackOverflowHook at /home/niko/esp/esp-idf/components/esp32/panic.c:716
  4.  
  5.  
  6. ELF file SHA256: 440ac70c88f935f625f5a612dda1df6f7698a6ba9d730597e52b392de6b9b17e
  7.  
  8. Backtrace: 0x40087be8:0x3ffc3ed0 0x40087e35:0x3ffc3ef0 0x40087e4c:0x3ffc3f10 0x4008c944:0x3ffc3f30 0x4008e3e8:0x3ffc3f50 0x4008e39e:0x4e45454b
  9. 0x40087be8: invoke_abort at /home/niko/esp/esp-idf/components/esp32/panic.c:716
  10.  
  11. 0x40087e35: abort at /home/niko/esp/esp-idf/components/esp32/panic.c:716
  12.  
  13. 0x40087e4c: vApplicationStackOverflowHook at /home/niko/esp/esp-idf/components/esp32/panic.c:716
  14.  
  15. 0x4008c944: vTaskSwitchContext at /home/niko/esp/esp-idf/components/freertos/tasks.c:3538
  16.  
  17. 0x4008e3e8: _frxt_dispatch at /home/niko/esp/esp-idf/components/freertos/portasm.S:406
  18.  
  19. 0x4008e39e: _frxt_int_exit at /home/niko/esp/esp-idf/components/freertos/portasm.S:206
  1. #include <esp_wifi.h>
  2. #include <esp_event_loop.h>
  3. #include <esp_log.h>
  4. #include <esp_system.h>
  5. #include <nvs_flash.h>
  6. #include <sys/param.h>
  7.  
  8.  
  9. #include <esp_http_server.h>
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "driver/gpio.h"
  14. #include "freertos/FreeRTOS.h"
  15.  
  16. #define LED1 12
  17.  
  18. static const char *TAG="APP";
  19. esp_err_t data_get_handler(httpd_req_t *req)
  20. {
  21.     // example of big data
  22.     char tmp_buff[10000] = "";
  23.     for (int i = 0; i < 9000; i++) {
  24.         tmp_buff[i] = 'a';
  25.     }
  26.  
  27.     httpd_resp_send(req, tmp_buff, strlen(tmp_buff));
  28.  
  29.     if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
  30.         ESP_LOGI(TAG, "Request headers lost");
  31.     }
  32.     return ESP_OK;
  33. }
  34.  
  35. httpd_uri_t data = {
  36.     .uri = "/data",
  37.     .method = HTTP_GET,
  38.     .handler = data_get_handler,
  39.     .user_ctx = "ESP32 data"
  40. };
  41.  
  42.  
  43. httpd_handle_t start_webserver(void)
  44. {
  45.     httpd_handle_t server = NULL;
  46.     httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  47.  
  48.     ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  49.     if (httpd_start(&server, &config) == ESP_OK) {
  50.         ESP_LOGI(TAG, "Registering URI handlers");
  51.    
  52.         httpd_register_uri_handler(server, &data);
  53.         return server;
  54.     }
  55.  
  56.     ESP_LOGI(TAG, "Error starting server!");
  57.     return NULL;
  58. }
  59.  
  60. void stop_webserver(httpd_handle_t server)
  61. {
  62.     httpd_stop(server);
  63. }
  64.  
  65. static esp_err_t event_handler(void *ctx, system_event_t *event)
  66. {
  67.     httpd_handle_t *server = (httpd_handle_t *) ctx;
  68.  
  69.     switch(event->event_id) {
  70.     case SYSTEM_EVENT_STA_START:
  71.         ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
  72.         ESP_ERROR_CHECK(esp_wifi_connect());
  73.         break;
  74.     case SYSTEM_EVENT_STA_GOT_IP:
  75.         ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
  76.         ESP_LOGI(TAG, "Got IP: '%s'", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  77.  
  78.         if (*server == NULL) {
  79.             *server = start_webserver();
  80.         }
  81.         break;
  82.     case SYSTEM_EVENT_STA_DISCONNECTED:
  83.         ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
  84.         ESP_ERROR_CHECK(esp_wifi_connect());
  85.  
  86.         if (*server) {
  87.             stop_webserver(*server);
  88.             *server = NULL;
  89.         }
  90.         break;
  91.     default:
  92.         break;
  93.     }
  94.  
  95.     return ESP_OK;
  96. }
  97.  
  98. static void initialize_wifi(void *arg)
  99. {
  100.     tcpip_adapter_init();
  101.     ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg));
  102.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  103.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  104.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  105.     wifi_config_t wifi_config = {
  106.         .sta = {
  107.             .ssid = "ZyXEL_KEENETIC_LITE_3EC257",
  108.             .password = "zyxel-009-tqt",
  109.         },
  110.     };
  111.     ESP_LOGI(TAG, "Setting WiFi configuration SSID %s PASSWORD %s", wifi_config.sta.ssid, wifi_config.sta.password);
  112.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  113.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  114.     ESP_ERROR_CHECK(esp_wifi_start());
  115. }
  116.  
  117.  
  118. static void initialize_gpio(){
  119.     gpio_pad_select_gpio(LED1);
  120.     gpio_set_direction(LED1, GPIO_MODE_OUTPUT);
  121. }
  122.  
  123. void app_main()
  124. {
  125.     static httpd_handle_t server = NULL;
  126.     ESP_ERROR_CHECK(nvs_flash_init());
  127.     initialize_gpio();
  128.     initialize_wifi(&server);
  129. }
  130.  

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: Can't send massive data from http server

Postby ESP_Sprite » Thu May 09, 2019 2:52 am

Line 22 allocates an 10K buffer on the stack. The default esp-http-server stack size is only 4K, so that leads to a stack overflow, as you've seen. The easy way would be to increase the webservers stack; you could e.g. add 'config.stack_size=20480' to line 47.

Who is online

Users browsing this forum: Baidu [Spider] and 172 guests