modified: main/src/http_client.c
modified: main/src/main.c
This commit is contained in:
1 parent
e950fcec8f
commit
f5bf4fc361
2 files changed
+84
-40
No files matched your search
@@ -6,15 +6,20 @@
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_http_client.h"
|
||||
#include <cJSON.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "http_client.h"
|
||||
#include "lib.h"
|
||||
|
||||
#define MAX_HTTP_OUTPUT_BUFFER 2048
|
||||
|
||||
const char * TAG = "http_client.c";
|
||||
|
||||
static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||
|
||||
|
||||
static esp_err_t http_get_event_handler(esp_http_client_event_t *evt)
|
||||
{
|
||||
switch (evt->event_id) {
|
||||
case HTTP_EVENT_ERROR:
|
||||
@@ -46,38 +51,6 @@ static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_http_client_config_t report_config = {
|
||||
.url = "http://tempcontroller.luethi.co/api/report",
|
||||
.event_handler = _http_event_handler,
|
||||
};
|
||||
static esp_http_client_config_t register_config = {
|
||||
.url = "http://tempcontroller.luethi.co/api/config",
|
||||
.event_handler = _http_event_handler,
|
||||
};
|
||||
|
||||
static void send_message(esp_http_client_config_t *config, const char* payload)
|
||||
{
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
// Set the request headers and payload
|
||||
esp_http_client_set_method(client, HTTP_METHOD_POST);
|
||||
esp_http_client_set_header(client, "Content-Type", "application/json");
|
||||
esp_http_client_set_post_field(client, payload, strlen(payload));
|
||||
|
||||
// Perform the request
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI("HTTP_EVENT", "HTTP POST Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE("HTTP_EVENT", "HTTP POST request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
/*
|
||||
void hc_test()
|
||||
{
|
||||
@@ -85,10 +58,37 @@ void hc_test()
|
||||
}
|
||||
*/
|
||||
bool hc_register(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
|
||||
}
|
||||
|
||||
bool hc_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\": ");
|
||||
@@ -96,6 +96,48 @@ bool hc_send_sensor_data(unsigned id, float humidity, float temperature) {
|
||||
strcat(tmp, ",\"id\": ");
|
||||
strcat(tmp, intToString(id));
|
||||
strcat(tmp, "}");
|
||||
send_message(&report_config, 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;
|
||||
}
|
||||
@@ -64,12 +64,16 @@ void main_task(void* args) {
|
||||
hc_register(&id);
|
||||
while (1)
|
||||
{
|
||||
hc_send_sensor_data(id,0,0); //test
|
||||
/*
|
||||
if (dht_read_float_data(DHT_TYPE_DHT22, DHT_GPIO, &humidity, &temperature) == ESP_OK) {
|
||||
} else {
|
||||
hc_send_sensor_data(id,humidity,temperature);
|
||||
}
|
||||
else {
|
||||
printf("Fail to get dht temperature data\n");
|
||||
}
|
||||
hc_send_sensor_data(id,humidity,temperature);
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,9 +81,7 @@ void app_main()
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
|
||||
xTaskCreate(main_task, "main task", 2048, NULL, tskIDLE_PRIORITY, NULL);
|
||||
|
||||
// WIFI
|
||||
wifi_init();
|
||||
//hc_test();
|
||||
|
||||
xTaskCreate(main_task, "main task", 2048, NULL, tskIDLE_PRIORITY, NULL);
|
||||
}
|
||||
Reference in new issue
Block a user