#include #include #include #include #include #include #include #include #include #include #include #include #include #include const char *TAG = "example"; #include "wifi_connect.h" #define MAX_HTTP_RECV_BUFFER 512 #define MAX_HTTP_OUTPUT_BUFFER 2048 #define EXAMPLE_HTTP_ENDPOINT "192.168.1.80" esp_err_t _http_event_handler(esp_http_client_event_t *evt) { 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); // Clean the buffer in case of a new request if (output_len == 0 && evt->user_data) { // we are just starting to copy the output data into the use memset(evt->user_data, 0, MAX_HTTP_OUTPUT_BUFFER); } // If user_data buffer is configured, copy the response into the buffer int copy_len = 0; assert(evt->user_data); // The last byte in evt->user_data is kept for the NULL character in case of out-of-bound access. copy_len = MIN(evt->data_len, (MAX_HTTP_OUTPUT_BUFFER - output_len)); if (copy_len) { memcpy(evt->user_data + output_len, evt->data, copy_len); } output_len += copy_len; break; case HTTP_EVENT_ON_FINISH: ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH"); 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((esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL); if (err != 0) { ESP_LOGI(TAG, "Last esp error code: 0x%x", err); ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); } output_len = 0; break; case HTTP_EVENT_REDIRECT: ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT"); break; } return ESP_OK; } static void https_with_url(void) { char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER + 1] = {0}; esp_http_client_config_t config = { .url = "https://www.howsmyssl.com", .event_handler = _http_event_handler, .user_data = local_response_buffer, // Pass address of local buffer to get response .crt_bundle_attach = esp_crt_bundle_attach, }; esp_http_client_handle_t client = esp_http_client_init(&config); esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err)); } ESP_LOGI(TAG, "Body: %s", local_response_buffer); esp_http_client_cleanup(client); } static void http_test_task(void *pvParameters) { https_with_url(); vTaskDelete(NULL); } 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()); wifi_connect(); xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL); }