diff --git a/firmware/main/include/http_client.h b/firmware/main/include/http_client.h index 8e0b3d0..98a47dd 100644 --- a/firmware/main/include/http_client.h +++ b/firmware/main/include/http_client.h @@ -17,7 +17,7 @@ bool hc_check_updates(); struct hc_reg_res hc_reg_dev(); struct hc_dev_config hc_get_dev_conf(); -void hc_send_sensor_data(int humidity, int temperature); +void hc_send_sensor_data(float humidity, float temperature); #ifdef __cplusplus } diff --git a/firmware/main/include/lib.h b/firmware/main/include/lib.h new file mode 100644 index 0000000..6121763 --- /dev/null +++ b/firmware/main/include/lib.h @@ -0,0 +1,70 @@ +#ifndef LIB_H +#define LIB_H + + + +// Helper function to reverse a string +void reverse(char* str, int len) { + int i = 0, j = len - 1; + while (i < j) { + char temp = str[i]; + str[i] = str[j]; + str[j] = temp; + i++; + j--; + } +} + +// Helper function to convert an integer to a string +int intToStr(int x, char str[], int d) { + int i = 0; + while (x) { + str[i++] = (x % 10) + '0'; + x = x / 10; + } + + // If the number of digits required is more, then add 0s at the beginning + while (i < d) + str[i++] = '0'; + + reverse(str, i); + str[i] = '\0'; + return i; +} + +char* floatToString(float num) { + // Allocate memory for the string + char* str = (char*)malloc(50 * sizeof(char)); + if (str == NULL) { + // If memory allocation fails, return NULL + return NULL; + } + + // Handle integer part + int intPart = (int)num; + int i = intToStr(intPart, str, 0); + + // Handle fractional part + float fractionalPart = num - (float)intPart; + if (fractionalPart < 0) { + fractionalPart = -fractionalPart; + } + + if (fractionalPart != 0) { + str[i++] = '.'; // Add decimal point + + // Consider precision up to 6 digits after the decimal point + for (int j = 0; j < 6; j++) { + fractionalPart = fractionalPart * 10; + int fracIntPart = (int)fractionalPart; + str[i++] = fracIntPart + '0'; + fractionalPart = fractionalPart - fracIntPart; + } + } + + str[i] = '\0'; // Null-terminate the string + + return str; +} + +#endif diff --git a/firmware/main/src/http_client.c b/firmware/main/src/http_client.c index 8b3892d..66bf8c4 100644 --- a/firmware/main/src/http_client.c +++ b/firmware/main/src/http_client.c @@ -10,6 +10,7 @@ #include #include "http_client.h" +#include "lib.h" const char * TAG = "http_client.c"; @@ -100,7 +101,11 @@ void hc_test() xTaskCreate(&http_get_task, "http_get_task", 16384, NULL, 5, NULL); } -void hc_send_sensor_data(int humidity, int temperature) { - const char* msg = "{\"humidity\": 55,\"temperature\":25.6}"; - send_message(msg); +void hc_send_sensor_data(float humidity, float temperature) { + char tmp[50] = "{\"humidity\": "; + strcat(tmp, floatToString(humidity)); + strcat(tmp, ",\"temperature\": "); + strcat(tmp, floatToString(temperature)); + strcat(tmp, "}"); + send_message(tmp); } diff --git a/firmware/main/src/main.c b/firmware/main/src/main.c index f678f60..a3222be 100644 --- a/firmware/main/src/main.c +++ b/firmware/main/src/main.c @@ -8,8 +8,8 @@ #include "esp_http_client.h" #include -#include "dht.h" +#include "dht.h" #include "sensor.h" #include "http_client.h" @@ -31,7 +31,7 @@ void wifi_init(void) { ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_connect()); } - +/* void temperature_task(void *arg) { ESP_ERROR_CHECK(dht_init(DHT_GPIO, false)); @@ -44,7 +44,6 @@ void temperature_task(void *arg) // e.g. in dht22, 604 = 60.4%, 252 = 25.2 C // If you want to print float data, you should run `make menuconfig` // to enable full newlib and call dht_read_float_data() here instead - //printf("Humidity: %d Temperature: %d\n", humidity, temperature); printf("Humidity: %d.%d%% Temperature: %d.%d\n", humidity/10, humidity%10, temperature/10,temperature%10); } else { printf("Fail to get dht temperature data\n"); @@ -53,12 +52,24 @@ void temperature_task(void *arg) } vTaskDelete(NULL); } - +*/ void main_task(void* args) { + //ESP_ERROR_CHECK(dht_init(DHT_GPIO, false)); + //vTaskDelay(2000 / portTICK_PERIOD_MS); + float humidity = 0; + float temperature = 0; while (1) { - hc_send_sensor_data(50,25); - vTaskDelay(5000 / portTICK_PERIOD_MS); + 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); } }