|
@@ -1,154 +0,0 @@
|
|
|
-#include <string.h>
|
|
|
-#include <stdlib.h>
|
|
|
-#include "freertos/FreeRTOS.h"
|
|
|
-#include "freertos/task.h"
|
|
|
-#include "esp_log.h"
|
|
|
-#include "esp_system.h"
|
|
|
-#include "esp_event.h"
|
|
|
-
|
|
|
-#include "wifi.h"
|
|
|
-#include "config.h"
|
|
|
-#include "mqtt.h"
|
|
|
-
|
|
|
-static const char *TAG = "HTTP_CLIENT";
|
|
|
-
|
|
|
-#ifdef HTTP_ENABLED
|
|
|
-
|
|
|
-#include "nvs_flash.h"
|
|
|
-#include "esp_netif.h"
|
|
|
-#include "esp_tls.h"
|
|
|
-#include "esp_http_client.h"
|
|
|
-
|
|
|
-#define MAX_HTTP_RECV_BUFFER 512
|
|
|
-
|
|
|
-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);
|
|
|
- /*
|
|
|
- * Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
|
|
|
- * However, event handler can also be used in case chunked encoding is used.
|
|
|
- */
|
|
|
- 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) {
|
|
|
- // Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response
|
|
|
- //ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
|
|
|
- free(output_buffer);
|
|
|
- output_buffer = NULL;
|
|
|
- }
|
|
|
- output_len = 0;
|
|
|
- break;
|
|
|
- case HTTP_EVENT_DISCONNECTED:
|
|
|
- ESP_LOGD(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) {
|
|
|
- ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
|
|
|
- ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
|
|
|
- }
|
|
|
- if (output_buffer != NULL) {
|
|
|
- free(output_buffer);
|
|
|
- output_buffer = NULL;
|
|
|
- }
|
|
|
- output_len = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-
|
|
|
-void http_rest_with_url()
|
|
|
-{
|
|
|
- char local_response_buffer[MAX_HTTP_RECV_BUFFER] = {0};
|
|
|
-
|
|
|
- // The default state of VVB is always ON (kontaktor is normally closed, so control value is inverted)
|
|
|
- uint32_t kontaktor = 0; // Kontaktor 0 = VVB ON
|
|
|
-
|
|
|
- esp_http_client_config_t config = {
|
|
|
- .url = "http://192.168.1.110/electrical/system/isBoilerOn.php",
|
|
|
- .event_handler = _http_event_handler,
|
|
|
- .user_data = local_response_buffer, // Pass address of local buffer to get response
|
|
|
- };
|
|
|
- esp_http_client_handle_t client = esp_http_client_init(&config);
|
|
|
-
|
|
|
- esp_http_client_set_method(client, HTTP_METHOD_GET);
|
|
|
- esp_err_t err = esp_http_client_perform(client);
|
|
|
- if (err == ESP_OK) {
|
|
|
-
|
|
|
- const int status = esp_http_client_get_status_code(client);
|
|
|
- const int dLen = esp_http_client_get_content_length(client);
|
|
|
- ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",status,dLen );
|
|
|
- //ESP_LOG_BUFFER_HEX(TAG,local_response_buffer,dLen);
|
|
|
-
|
|
|
- if( status == 200 && dLen >= 6 ) {
|
|
|
- local_response_buffer[6] = '\0';
|
|
|
- if( strstr(local_response_buffer,"Ok") != NULL ) {
|
|
|
- ESP_LOGI(TAG, "HTTP Received: Ok" );
|
|
|
- if( strstr(local_response_buffer,"OFF") != NULL ) {
|
|
|
- ESP_LOGI(TAG, "HTTP Received: OFF" );
|
|
|
- kontaktor = 1;
|
|
|
- }
|
|
|
- else if( strstr(local_response_buffer,"ON") != NULL ) {
|
|
|
- ESP_LOGI(TAG, "HTTP Received: ON" );
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
- ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
|
|
|
- }
|
|
|
-
|
|
|
- esp_http_client_cleanup(client);
|
|
|
-
|
|
|
- ESP_LOGI(TAG, "Set kontaktor state: %u",kontaktor);
|
|
|
- gpio_set_level(VVB_RELAY_OUTPUT_IO, kontaktor);
|
|
|
-
|
|
|
- char txt[10];
|
|
|
- sprintf(txt,"%u",kontaktor);
|
|
|
- sendMQTTMessage("basement/boiler/contactor", txt);
|
|
|
-}
|
|
|
-
|
|
|
-#else
|
|
|
-
|
|
|
-void http_rest_with_url()
|
|
|
-{
|
|
|
- ESP_LOGI(TAG, "Set kontaktor state (FAKE): 0");
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-#endif
|