modified: firmware/main/include/http_client.h
modified: firmware/main/include/lib.h modified: firmware/main/src/http_client.c modified: firmware/main/src/main.c
This commit is contained in:
1 parent
13e6cf1032
commit
e950fcec8f
4 files changed
+68
-41
No files matched your search
@@ -5,19 +5,15 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct hc_reg_res {
|
||||
unsigned device_id;
|
||||
};
|
||||
|
||||
struct hc_dev_config {
|
||||
};
|
||||
|
||||
void hc_test(void);
|
||||
bool hc_check_updates();
|
||||
struct hc_reg_res hc_reg_dev();
|
||||
bool hc_register();
|
||||
struct hc_dev_config hc_get_dev_conf();
|
||||
|
||||
void hc_send_sensor_data(float humidity, float temperature);
|
||||
bool hc_send_sensor_data(unsigned id, float humidity, float temperature);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -67,4 +67,45 @@ char* floatToString(float num) {
|
||||
return str;
|
||||
}
|
||||
|
||||
// Function to convert an integer to a string
|
||||
char* intToString(int num)
|
||||
{
|
||||
// Allocate memory for the string
|
||||
char* str = (char*)malloc(50 * sizeof(char));
|
||||
|
||||
int i = 0;
|
||||
// Save the sign of the number
|
||||
int sign = num;
|
||||
|
||||
// If the number is negative, make it positive
|
||||
if (num < 0) {
|
||||
num = -num;
|
||||
}
|
||||
|
||||
// Extract digits from the number and add them to the
|
||||
// string
|
||||
do {
|
||||
// Convert integer digit to character
|
||||
str[i++] = num % 10 + '0';
|
||||
} while ((num /= 10) > 0);
|
||||
|
||||
// If the number was negative, add a minus sign to the
|
||||
// string
|
||||
if (sign < 0) {
|
||||
str[i++] = '-';
|
||||
}
|
||||
|
||||
// Null-terminate the string
|
||||
str[i] = '\0';
|
||||
|
||||
// Reverse the string to get the correct order
|
||||
for (int j = 0, k = i - 1; j < k; j++, k--) {
|
||||
char temp = str[j];
|
||||
str[j] = str[k];
|
||||
str[k] = temp;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -46,33 +46,16 @@ static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_http_client_config_t config = {
|
||||
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 http_get_task(void *pvParameters) {
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
while (1) {
|
||||
// Perform the HTTP GET request
|
||||
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));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
// Wait for 1 second
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
}
|
||||
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void send_message(const char* payload)
|
||||
static void send_message(esp_http_client_config_t *config, const char* payload)
|
||||
{
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
@@ -95,17 +78,24 @@ static void send_message(const char* payload)
|
||||
// Cleanup
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
/*
|
||||
void hc_test()
|
||||
{
|
||||
xTaskCreate(&http_get_task, "http_get_task", 16384, NULL, 5, NULL);
|
||||
}
|
||||
*/
|
||||
bool hc_register(unsigned *id) {
|
||||
return 1; //1 as test ID
|
||||
}
|
||||
|
||||
void hc_send_sensor_data(float humidity, float temperature) {
|
||||
char tmp[50] = "{\"humidity\": ";
|
||||
bool hc_send_sensor_data(unsigned id, float humidity, float temperature) {
|
||||
char tmp[100] = "{\"humidity\": ";
|
||||
strcat(tmp, floatToString(humidity));
|
||||
strcat(tmp, ",\"temperature\": ");
|
||||
strcat(tmp, floatToString(temperature));
|
||||
strcat(tmp, ",\"id\": ");
|
||||
strcat(tmp, intToString(id));
|
||||
strcat(tmp, "}");
|
||||
send_message(tmp);
|
||||
send_message(&report_config, tmp);
|
||||
return 0;
|
||||
}
|
||||
@@ -54,22 +54,22 @@ void temperature_task(void *arg)
|
||||
}
|
||||
*/
|
||||
void main_task(void* args) {
|
||||
//ESP_ERROR_CHECK(dht_init(DHT_GPIO, false));
|
||||
//vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
float humidity = 0;
|
||||
float temperature = 0;
|
||||
|
||||
ESP_ERROR_CHECK(dht_init(DHT_GPIO, false));
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
//Register device with Server
|
||||
unsigned id;
|
||||
hc_register(&id);
|
||||
while (1)
|
||||
{
|
||||
ESP_ERROR_CHECK(dht_init(DHT_GPIO, false));
|
||||
vTaskDelay(4000 / portTICK_PERIOD_MS);
|
||||
|
||||
if (dht_read_float_data(DHT_TYPE_DHT22, DHT_GPIO, &humidity, &temperature) == ESP_OK) {
|
||||
} else {
|
||||
printf("Fail to get dht temperature data\n");
|
||||
}
|
||||
|
||||
hc_send_sensor_data(humidity,temperature);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
hc_send_sensor_data(id,humidity,temperature);
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user