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=======================
- #include <string.h>
- #include <stdlib.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_log.h"
- #include "esp_system.h"
- #include "nvs_flash.h"
- #include "esp_event.h"
- #include "esp_netif.h"
- #include "protocol_examples_common.h"
- #include "esp_tls.h"
- #include "esp_http_client.h"
- #define MAX_HTTP_OUTPUT_BUFFER 2048
- static const char *TAG = "HTTP_CLIENT";
- esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
- static char *output_buffer; // Buffer to store response of http request from event handler
- static int output_len; // Stores number of bytes read
- switch(evt->event_id) {
- case HTTP_EVENT_ERROR:
- ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
- break;
- case HTTP_EVENT_ON_CONNECTED:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
- break;
- case HTTP_EVENT_HEADER_SENT:
- ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
- break;
- case HTTP_EVENT_ON_HEADER:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
- break;
- case HTTP_EVENT_ON_DATA:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
- if (!esp_http_client_is_chunked_response(evt->client)) {
- // If user_data buffer is configured, copy the response into the buffer
- if (evt->user_data) {
- memcpy(evt->user_data + output_len, evt->data, evt->data_len);
- } else {
- if (output_buffer == NULL) {
- output_buffer = (char *) malloc(esp_http_client_get_content_length(evt->client));
- output_len = 0;
- if (output_buffer == NULL) {
- ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
- return ESP_FAIL;
- }
- }
- memcpy(output_buffer + output_len, evt->data, evt->data_len);
- }
- output_len += evt->data_len;
- }
- break;
- case HTTP_EVENT_ON_FINISH:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
- if (output_buffer != NULL) {
- // ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
- free(output_buffer);
- output_buffer = NULL;
- output_len = 0;
- }
- break;
- case HTTP_EVENT_DISCONNECTED:
- ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
- int mbedtls_err = 0;
- esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
- if (err != 0) {
- if (output_buffer != NULL) {
- free(output_buffer);
- output_buffer = NULL;
- output_len = 0;
- }
- ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
- ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
- }
- break;
- }
- return ESP_OK;
- }
- static void http_test_task(void *pvParameters)
- {
- char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
- esp_http_client_config_t config = {
- .host = "httpbin.org",
- .path = "/get",
- .query = "esp",
- .event_handler = _http_event_handler,
- .user_data = local_response_buffer,
- .disable_auto_redirect = true,
- };
- while (1){
- printf("test HTTP %d \r\n", esp_get_free_heap_size());
- esp_http_client_handle_t client = esp_http_client_init(&config);
- // POST
- const char *post_data = "{\"field1\":\"value1\"}";
- esp_http_client_set_url(client, "http://httpbin.org/post");
- esp_http_client_set_method(client, HTTP_METHOD_POST);
- esp_http_client_set_header(client, "Content-Type", "application/json");
- esp_http_client_set_post_field(client, post_data, strlen(post_data));
- esp_err_t err = esp_http_client_perform(client);
- if (err == ESP_OK) {
- ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- } else {
- ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
- }
- esp_http_client_cleanup(client);
- vTaskDelay(5000 / portTICK_PERIOD_MS);
- }
- }
- void app_main(void)
- {
- esp_err_t ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK(ret);
- ESP_ERROR_CHECK(esp_netif_init());
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- ESP_ERROR_CHECK(example_connect());
- printf("idf version is %s\n", esp_get_idf_version());
- xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
- }
========================= 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