"After 5 HTTP requests the microcontroller restarts"

jakarman12
Posts: 8
Joined: Fri Oct 30, 2020 6:46 pm

"After 5 HTTP requests the microcontroller restarts"

Postby jakarman12 » Fri Jan 22, 2021 11:36 pm

Hi guys, I'm stuck with an issue that I ran into while testing the esp_http_client component. can you help me please? :(

HW: ESP32-DevKitC
IDF Version: v4.2-238-g8cd16b60f
Problem: After 5 HTTP requests the microcontroller restarts.
Description: I require a task that every 5 seconds send a POST request to a Rest API, I am using the example of esp_http_client and I deleted everything that I do not need, the program after sending 5 requests restarts and marks the error shown below .


========================= CODE=======================
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "esp_log.h"
  6. #include "esp_system.h"
  7. #include "nvs_flash.h"
  8. #include "esp_event.h"
  9. #include "esp_netif.h"
  10. #include "protocol_examples_common.h"
  11. #include "esp_tls.h"
  12.  
  13. #include "esp_http_client.h"
  14.  
  15. #define MAX_HTTP_OUTPUT_BUFFER 2048
  16. static const char *TAG = "HTTP_CLIENT";
  17.  
  18.  
  19. esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
  20.     static char *output_buffer;  // Buffer to store response of http request from event handler
  21.     static int output_len;       // Stores number of bytes read
  22.     switch(evt->event_id) {
  23.         case HTTP_EVENT_ERROR:
  24.             ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  25.             break;
  26.         case HTTP_EVENT_ON_CONNECTED:
  27.             ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  28.             break;
  29.         case HTTP_EVENT_HEADER_SENT:
  30.             ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  31.             break;
  32.         case HTTP_EVENT_ON_HEADER:
  33.             ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
  34.             break;
  35.         case HTTP_EVENT_ON_DATA:
  36.             ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
  37.  
  38.             if (!esp_http_client_is_chunked_response(evt->client)) {
  39.                 // If user_data buffer is configured, copy the response into the buffer
  40.                 if (evt->user_data) {
  41.                     memcpy(evt->user_data + output_len, evt->data, evt->data_len);
  42.                 } else {
  43.                     if (output_buffer == NULL) {
  44.                         output_buffer = (char *) malloc(esp_http_client_get_content_length(evt->client));
  45.                         output_len = 0;
  46.                         if (output_buffer == NULL) {
  47.                             ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
  48.                             return ESP_FAIL;
  49.                         }
  50.                     }
  51.                     memcpy(output_buffer + output_len, evt->data, evt->data_len);
  52.                 }
  53.                 output_len += evt->data_len;
  54.             }
  55.  
  56.             break;
  57.         case HTTP_EVENT_ON_FINISH:
  58.             ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  59.             if (output_buffer != NULL) {
  60.                 // ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
  61.                 free(output_buffer);
  62.                 output_buffer = NULL;
  63.                 output_len = 0;
  64.             }
  65.             break;
  66.         case HTTP_EVENT_DISCONNECTED:
  67.             ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
  68.             int mbedtls_err = 0;
  69.             esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
  70.             if (err != 0) {
  71.                 if (output_buffer != NULL) {
  72.                     free(output_buffer);
  73.                     output_buffer = NULL;
  74.                     output_len = 0;
  75.                 }
  76.                 ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
  77.                 ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
  78.             }
  79.             break;
  80.     }
  81.     return ESP_OK;
  82. }
  83.  
  84.  
  85. static void http_test_task(void *pvParameters)    
  86. {
  87.      char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};    
  88.  
  89.     esp_http_client_config_t config = {            
  90.         .host = "httpbin.org",                      
  91.         .path = "/get",                            
  92.         .query = "esp",                            
  93.         .event_handler = _http_event_handler,      
  94.         .user_data = local_response_buffer,        
  95.         .disable_auto_redirect = true,          
  96.     };
  97.        
  98.     while (1){
  99.         printf("test HTTP %d \r\n", esp_get_free_heap_size());
  100.         esp_http_client_handle_t client = esp_http_client_init(&config);  
  101.  
  102.         // POST
  103.     const char *post_data = "{\"field1\":\"value1\"}";
  104.     esp_http_client_set_url(client, "http://httpbin.org/post");
  105.     esp_http_client_set_method(client, HTTP_METHOD_POST);
  106.     esp_http_client_set_header(client, "Content-Type", "application/json");
  107.     esp_http_client_set_post_field(client, post_data, strlen(post_data));
  108.     esp_err_t err = esp_http_client_perform(client);
  109.  
  110.     if (err == ESP_OK) {
  111.         ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
  112.                 esp_http_client_get_status_code(client),
  113.                 esp_http_client_get_content_length(client));
  114.     } else {
  115.         ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
  116.     }
  117.  
  118.         esp_http_client_cleanup(client);
  119.         vTaskDelay(5000 / portTICK_PERIOD_MS);
  120.  
  121.     }
  122. }
  123.  
  124. void app_main(void)
  125. {
  126.     esp_err_t ret = nvs_flash_init();                                                
  127.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  128.       ESP_ERROR_CHECK(nvs_flash_erase());                                            
  129.       ret = nvs_flash_init();                                                        
  130.     }
  131.     ESP_ERROR_CHECK(ret);                            
  132.     ESP_ERROR_CHECK(esp_netif_init());                
  133.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  134.  
  135.     ESP_ERROR_CHECK(example_connect());
  136.     printf("idf version is %s\n", esp_get_idf_version());
  137.  
  138.     xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
  139. }
=====================================================

========================= LOG =======================
idf version is v4.2-238-g8cd16b60f
test HTTP 230080
I (4993) HTTP_CLIENT: HTTP POST Status = 200, content_length = 424
I (5003) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
test HTTP 233920
I (10343) HTTP_CLIENT: HTTP POST Status = 200, content_length = 424
I (10343) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
test HTTP 233708
I (15983) HTTP_CLIENT: HTTP POST Status = 200, content_length = 424
I (15983) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
test HTTP 233496
I (21303) HTTP_CLIENT: HTTP POST Status = 200, content_length = 424
I (21303) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
test HTTP 233280
I (26733) HTTP_CLIENT: HTTP POST Status = 200, content_length = 424
I (26733) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
test HTTP 233064
Guru Meditation Error: Core 0 panic'ed (IllegalInstruction). Exception was unhandled.

Core 0 register dump:
PC : 0x74736f70 PS : 0x00060f30 A0 : 0x800f8a65 A1 : 0x3ffc6a80
A2 : 0x3ffb9814 A3 : 0x3ffc6b30 A4 : 0x00000000 A5 : 0x00000000
A6 : 0x00000067 A7 : 0x00000072 A8 : 0x8015a0ee A9 : 0x3ffc6a60
A10 : 0x3ffb98a8 A11 : 0x3ffb8fb0 A12 : 0x3ffb8fba A13 : 0x3ffb8f98
A14 : 0x00000000 A15 : 0x00000000 SAR : 0x00000018 EXCCAUSE: 0x00000000
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffc

Backtrace:0x74736f6d:0x3ffc6a80 |<-CORRUPTED


ELF file SHA256: c92e42dd878a56fe

Rebooting...
ets Jun 8 2016 00:22:57

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

Re: "After 5 HTTP requests the microcontroller restarts"

Postby ESP_Sprite » Sun Jan 24, 2021 3:18 am

Your output_len variable is defined static and does not reset to 0 when you start a new http request.

jakarman12
Posts: 8
Joined: Fri Oct 30, 2020 6:46 pm

Re: "After 5 HTTP requests the microcontroller restarts"

Postby jakarman12 » Mon Jan 25, 2021 1:52 am

ESP_Sprite wrote:
Sun Jan 24, 2021 3:18 am
Your output_len variable is defined static and does not reset to 0 when you start a new http request.
hi @ESP_Sprite

I have already reviewed what you tell me, but the static variable is necessary since the handler could be entered more than once for each request ... what I did notice is that the output_len variable never became 0 (something that you also mentioned to me ). I already searched and apparently it is a bug that has already been fixed in new versions, I add the commit where the correction is made.

https://github.com/espressif/esp-idf/co ... 3204d781d6

thanks

Who is online

Users browsing this forum: No registered users and 11 guests