|
@@ -19,14 +19,10 @@
|
|
|
#define MAX_HTTP_RECV_BUFFER 512
|
|
|
static const char *TAG = "HTTP_CLIENT";
|
|
|
|
|
|
-typedef struct {
|
|
|
- double energyData_kWh;
|
|
|
- uint32_t bigCnt;
|
|
|
- int32_t cnt;
|
|
|
-} sensor_data;
|
|
|
-
|
|
|
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");
|
|
@@ -42,64 +38,103 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
|
|
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)) {
|
|
|
- // Write out data
|
|
|
- // printf("%.*s", evt->data_len, (char*)evt->data);
|
|
|
+ // 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_LOGI(TAG, "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;
|
|
|
}
|
|
|
|
|
|
-static void http_rest_with_url(void *pvParameters)
|
|
|
+void http_rest_with_url()
|
|
|
{
|
|
|
+ char local_response_buffer[MAX_HTTP_RECV_BUFFER] = {0};
|
|
|
+
|
|
|
+ uint32_t kontaktor = 1; // The default state is always 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);
|
|
|
|
|
|
- //char post_data[10];
|
|
|
- //sprintf(post_data,"",);
|
|
|
-
|
|
|
esp_http_client_set_method(client, HTTP_METHOD_GET);
|
|
|
- //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 GET Status = %d, content_length = %d",
|
|
|
- esp_http_client_get_status_code(client),
|
|
|
- esp_http_client_get_content_length(client));
|
|
|
+
|
|
|
+ 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 = 0;
|
|
|
+ }
|
|
|
+ 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);
|
|
|
-}
|
|
|
|
|
|
-static void http_get_data_task(void *pvParameters)
|
|
|
-{
|
|
|
-
|
|
|
- http_rest_with_url(pvParameters);
|
|
|
-
|
|
|
- vTaskDelete(NULL);
|
|
|
+ ESP_LOGI(TAG, "Change relay state: %u",kontaktor);
|
|
|
+ gpio_set_level(VVB_RELAY_OUTPUT_IO, kontaktor);
|
|
|
}
|
|
|
|
|
|
-void sendHTTPMessage() {
|
|
|
|
|
|
- if( commIsUpAndRunning == 1 ) xTaskCreate(&http_get_data_task, "get_data_task", 8192, NULL, 5, NULL);
|
|
|
-}
|
|
|
|
|
|
#endif
|