#include #include #include #include //#include #include #include #include #include #include #include "controller.h" #include "functions.h" #define MAX_HTTP_OUTPUT_BUFFER 2048 static const char * TAG = "controller.c"; esp_err_t controller_init() { return ESP_OK; } static esp_err_t http_get_event_handler(esp_http_client_event_t *evt) { switch (evt->event_id) { case HTTP_EVENT_ERROR: ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ERROR"); break; case HTTP_EVENT_ON_CONNECTED: ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ON_CONNECTED"); break; case HTTP_EVENT_HEADER_SENT: ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_HEADER_SENT"); break; case HTTP_EVENT_ON_HEADER: ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value); break; case HTTP_EVENT_ON_DATA: ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); if (!esp_http_client_is_chunked_response(evt->client)) { // Write out data printf("%.*s", evt->data_len, (char*)evt->data); } break; case HTTP_EVENT_ON_FINISH: ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ON_FINISH"); break; case HTTP_EVENT_DISCONNECTED: ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_DISCONNECTED"); break; } return ESP_OK; } static bool register_device(unsigned *id) { static esp_http_client_config_t register_config = { .url = "http://tempcontroller.luethi.co/api/config", .event_handler = NULL, .method = HTTP_METHOD_POST, }; return 1; //1 as test ID } static bool send_sensor_data(unsigned id, float humidity, float temperature) { esp_err_t err; int content_length = 0; char output_buffer[MAX_HTTP_OUTPUT_BUFFER]; static esp_http_client_config_t config = { .url = "http://tempcontroller.luethi.co/api/report", .event_handler = NULL, .method = HTTP_METHOD_POST, }; // Create cJSON object cJSON *json_payload = cJSON_CreateObject(); if (json_payload == NULL) { ESP_LOGE(TAG, "Failed to create JSON object"); return ESP_FAIL; } cJSON_AddNumberToObject(json_payload, "temperature", temperature); cJSON_AddNumberToObject(json_payload, "humidity", humidity); cJSON_AddNumberToObject(json_payload, "id", id); /* char tmp[100] = "{\"humidity\": "; strcat(tmp, floatToString(humidity)); strcat(tmp, ",\"temperature\": "); strcat(tmp, floatToString(temperature)); strcat(tmp, ",\"id\": "); strcat(tmp, intToString(id)); strcat(tmp, "}"); */ // Print JSON object char *json_string = cJSON_PrintUnformatted(json_payload); if (json_string == NULL) { ESP_LOGE(TAG, "Failed to print JSON string"); cJSON_Delete(json_payload); return ESP_FAIL; } ESP_LOGI(TAG, "Set up htttp client..."); esp_http_client_handle_t client = esp_http_client_init(&config); // Set HTTP headers esp_http_client_set_header(client, "Content-Type", "application/json"); // Set POST data esp_http_client_set_post_field(client, json_string, strlen(json_string)); // Perform the HTTP POST request 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)); content_length = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); if (content_length >= 0) { output_buffer[content_length] = 0; // Null-terminate the response ESP_LOGI(TAG, "Response: %s", output_buffer); } else { ESP_LOGE(TAG, "Failed to read response"); } } else { ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err)); return 1; } // Cleanup esp_http_client_cleanup(client); cJSON_Delete(json_payload); free(json_string); return 0; }