1
0

http_client.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "esp_event.h"
  8. #include "wifi.h"
  9. #include "config.h"
  10. #ifdef HTTP_ENABLED
  11. #include "nvs_flash.h"
  12. #include "esp_netif.h"
  13. #include "esp_tls.h"
  14. #include "esp_http_client.h"
  15. #define MAX_HTTP_RECV_BUFFER 512
  16. static const char *TAG = "HTTP_CLIENT";
  17. esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  18. {
  19. static char *output_buffer; // Buffer to store response of http request from event handler
  20. static int output_len; // Stores number of bytes read
  21. switch(evt->event_id) {
  22. case HTTP_EVENT_ERROR:
  23. ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  24. break;
  25. case HTTP_EVENT_ON_CONNECTED:
  26. ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  27. break;
  28. case HTTP_EVENT_HEADER_SENT:
  29. ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  30. break;
  31. case HTTP_EVENT_ON_HEADER:
  32. ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
  33. break;
  34. case HTTP_EVENT_ON_DATA:
  35. ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
  36. /*
  37. * Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
  38. * However, event handler can also be used in case chunked encoding is used.
  39. */
  40. if (!esp_http_client_is_chunked_response(evt->client)) {
  41. // If user_data buffer is configured, copy the response into the buffer
  42. if (evt->user_data) {
  43. memcpy(evt->user_data + output_len, evt->data, evt->data_len);
  44. } else {
  45. if (output_buffer == NULL) {
  46. output_buffer = (char *) malloc(esp_http_client_get_content_length(evt->client));
  47. output_len = 0;
  48. if (output_buffer == NULL) {
  49. ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
  50. return ESP_FAIL;
  51. }
  52. }
  53. memcpy(output_buffer + output_len, evt->data, evt->data_len);
  54. }
  55. output_len += evt->data_len;
  56. }
  57. break;
  58. case HTTP_EVENT_ON_FINISH:
  59. ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  60. if (output_buffer != NULL) {
  61. // Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response
  62. //ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
  63. free(output_buffer);
  64. output_buffer = NULL;
  65. }
  66. output_len = 0;
  67. break;
  68. case HTTP_EVENT_DISCONNECTED:
  69. ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
  70. int mbedtls_err = 0;
  71. esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
  72. if (err != 0) {
  73. ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
  74. ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
  75. }
  76. if (output_buffer != NULL) {
  77. free(output_buffer);
  78. output_buffer = NULL;
  79. }
  80. output_len = 0;
  81. break;
  82. }
  83. return ESP_OK;
  84. }
  85. void http_rest_with_url()
  86. {
  87. char local_response_buffer[MAX_HTTP_RECV_BUFFER] = {0};
  88. uint32_t kontaktor = 1; // The default state is always ON
  89. esp_http_client_config_t config = {
  90. .url = "http://192.168.1.110/electrical/system/isBoilerOn.php",
  91. .event_handler = _http_event_handler,
  92. .user_data = local_response_buffer, // Pass address of local buffer to get response
  93. };
  94. esp_http_client_handle_t client = esp_http_client_init(&config);
  95. esp_http_client_set_method(client, HTTP_METHOD_GET);
  96. esp_err_t err = esp_http_client_perform(client);
  97. if (err == ESP_OK) {
  98. const int status = esp_http_client_get_status_code(client);
  99. const int dLen = esp_http_client_get_content_length(client);
  100. ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",status,dLen );
  101. //ESP_LOG_BUFFER_HEX(TAG,local_response_buffer,dLen);
  102. if( status == 200 && dLen >= 6 ) {
  103. local_response_buffer[6] = '\0';
  104. if( strstr(local_response_buffer,"Ok") != NULL ) {
  105. ESP_LOGI(TAG, "HTTP Received: Ok" );
  106. if( strstr(local_response_buffer,"OFF") != NULL ) {
  107. ESP_LOGI(TAG, "HTTP Received: OFF" );
  108. kontaktor = 0;
  109. }
  110. else if( strstr(local_response_buffer,"ON") != NULL ) {
  111. ESP_LOGI(TAG, "HTTP Received: ON" );
  112. }
  113. }
  114. }
  115. } else {
  116. ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
  117. }
  118. esp_http_client_cleanup(client);
  119. ESP_LOGI(TAG, "Change relay state: %u",kontaktor);
  120. gpio_set_level(VVB_RELAY_OUTPUT_IO, kontaktor);
  121. }
  122. #endif