From a1dae20cbf4ec99986aea0924e6cedb21a0f1500 Mon Sep 17 00:00:00 2001 From: Mateusz Date: Tue, 15 Sep 2026 20:51:07 +0200 Subject: [PATCH] state information --- thermo_sensor/main/CMakeLists.txt | 2 +- thermo_sensor/main/thermo_sensor.c | 9 +++------ thermo_sensor/main/ygg_bmp280.c | 11 ++++------- thermo_sensor/main/ygg_http_req.c | 11 ++++++----- thermo_sensor/main/ygg_http_req.h | 5 ++++- thermo_sensor/main/ygg_http_routes.c | 11 ++++------- thermo_sensor/main/ygg_state.c | 3 +++ thermo_sensor/main/ygg_state.h | 13 +++++++++++++ 8 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 thermo_sensor/main/ygg_state.c create mode 100644 thermo_sensor/main/ygg_state.h diff --git a/thermo_sensor/main/CMakeLists.txt b/thermo_sensor/main/CMakeLists.txt index a5d9113..869108c 100644 --- a/thermo_sensor/main/CMakeLists.txt +++ b/thermo_sensor/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "thermo_sensor.c" "ygg_http_routes.c" "ygg_bmp280.c" "ygg_http_req.c" "../../common/esp32/ygg_wifi.c" "../../common/esp32/ygg_http_server.c" +idf_component_register(SRCS "thermo_sensor.c" "ygg_http_routes.c" "ygg_bmp280.c" "ygg_http_req.c" "ygg_state.c" "../../common/esp32/ygg_wifi.c" "../../common/esp32/ygg_http_server.c" INCLUDE_DIRS "." "../../common/esp32") diff --git a/thermo_sensor/main/thermo_sensor.c b/thermo_sensor/main/thermo_sensor.c index c1e0d89..d84b5ee 100644 --- a/thermo_sensor/main/thermo_sensor.c +++ b/thermo_sensor/main/thermo_sensor.c @@ -11,21 +11,18 @@ #include "ygg_http_server.h" #include "ygg_bmp280.h" #include "ygg_http_req.h" +#include "ygg_state.h" static const char *TAG = "thermo_sensor"; -float last_temperature = 0.0f; -float last_pressure = 0.0f; -float last_humidity = 0.0f; bmp280_t dev_bmp_280; void main_loop(void *pvParameters) { while (1) { vTaskDelay(pdMS_TO_TICKS(10000)); ygg_bmp280_read_sensor(&dev_bmp_280); - ygg_send_sensor_redouts(last_temperature, - last_pressure, - last_humidity); + ygg_send_sensor_readouts(SENSOR_NAME, + CURR_READOUT); } } diff --git a/thermo_sensor/main/ygg_bmp280.c b/thermo_sensor/main/ygg_bmp280.c index 99e6eec..b1353f9 100644 --- a/thermo_sensor/main/ygg_bmp280.c +++ b/thermo_sensor/main/ygg_bmp280.c @@ -2,10 +2,7 @@ #include #include #include "ygg_bmp280.h" - -extern float last_temperature; -extern float last_pressure; -extern float last_humidity; +#include "ygg_state.h" void ygg_bmp280_init(bmp280_t *dev) { bmp280_params_t params; @@ -26,9 +23,9 @@ void ygg_bmp280_read_sensor(bmp280_t *dev) { if (bmp280_read_float(dev, &temperature, &pressure, &humidity) != ESP_OK) printf("Temperature/pressure reading failed\n"); - last_temperature = temperature; - last_pressure = pressure; - last_humidity = humidity; + CURR_READOUT.temperature = temperature; + CURR_READOUT.pressure = pressure; + CURR_READOUT.humidity = humidity; printf("Pressure: %.2f Pa, Temperature: %.2f C", pressure, temperature); if (bme280p) printf(", Humidity: %.2f\n", humidity); diff --git a/thermo_sensor/main/ygg_http_req.c b/thermo_sensor/main/ygg_http_req.c index ebe1b8a..65a5ef9 100644 --- a/thermo_sensor/main/ygg_http_req.c +++ b/thermo_sensor/main/ygg_http_req.c @@ -106,23 +106,24 @@ char* ygg_send_post(const char *url, char *post_data) { int ygg_get_sensor_id(const char *sensor_name, const char *url); -void ygg_send_sensor_redouts(float temp, float press, float hum) { +void ygg_send_sensor_readouts(const char *sensor_name, + ygg_sensor_readout readout) { char post_data[64]; - int sensor_id = ygg_get_sensor_id("super_hiper_bmp280", "http://192.168.0.236:5000"); + int sensor_id = ygg_get_sensor_id(sensor_name, "http://192.168.0.236:5000"); char url[128]; snprintf(url, sizeof(url), "http://192.168.0.236:5000/api/sensors/%d/readouts", sensor_id); snprintf(post_data, sizeof(post_data), - "{\"type\":\"temperature\", \"value\":%.2f}", temp); + "{\"type\":\"temperature\", \"value\":%.2f}", readout.temperature); ygg_send_post(url, post_data); snprintf(post_data, sizeof(post_data), - "{\"type\":\"pressure\", \"value\":%.2f}", press); + "{\"type\":\"pressure\", \"value\":%.2f}", readout.pressure); ygg_send_post(url, post_data); snprintf(post_data, sizeof(post_data), - "{\"type\":\"humidity\", \"value\":%.2f}", hum); + "{\"type\":\"humidity\", \"value\":%.2f}", readout.humidity); ygg_send_post(url, post_data); } diff --git a/thermo_sensor/main/ygg_http_req.h b/thermo_sensor/main/ygg_http_req.h index 03d876c..6462765 100644 --- a/thermo_sensor/main/ygg_http_req.h +++ b/thermo_sensor/main/ygg_http_req.h @@ -1,6 +1,9 @@ #ifndef YGG_HTTP_REQ_H #define YGG_HTTP_REQ_H -void ygg_send_sensor_redouts(float temp, float press, float hum); +#include "ygg_state.h" + +void ygg_send_sensor_readouts(const char *sensor_name, + ygg_sensor_readout readout); #endif diff --git a/thermo_sensor/main/ygg_http_routes.c b/thermo_sensor/main/ygg_http_routes.c index fb9bc1f..01dfcf2 100644 --- a/thermo_sensor/main/ygg_http_routes.c +++ b/thermo_sensor/main/ygg_http_routes.c @@ -1,9 +1,6 @@ #include "esp_http_server.h" #include "ygg_http_routes.h" - -extern float last_temperature; -extern float last_pressure; -extern float last_humidity; +#include "ygg_state.h" esp_err_t ygg_get_handler(httpd_req_t *req) { @@ -21,7 +18,7 @@ esp_err_t ygg_get_handler(httpd_req_t *req) static ygg_http_user_ctx temp_ctx = { .param_name = "temperature", - .param_val = &last_temperature + .param_val = &CURR_READOUT.temperature }; const httpd_uri_t ygg_temp_uri = { @@ -33,7 +30,7 @@ const httpd_uri_t ygg_temp_uri = { static ygg_http_user_ctx press_ctx = { .param_name = "pressure", - .param_val = &last_pressure + .param_val = &CURR_READOUT.pressure }; const httpd_uri_t ygg_press_uri = { @@ -45,7 +42,7 @@ const httpd_uri_t ygg_press_uri = { static ygg_http_user_ctx hum_ctx = { .param_name = "humidity", - .param_val = &last_humidity + .param_val = &CURR_READOUT.humidity }; const httpd_uri_t ygg_hum_uri = { diff --git a/thermo_sensor/main/ygg_state.c b/thermo_sensor/main/ygg_state.c new file mode 100644 index 0000000..82093a6 --- /dev/null +++ b/thermo_sensor/main/ygg_state.c @@ -0,0 +1,3 @@ +#include "ygg_state.h" + +ygg_sensor_readout CURR_READOUT; diff --git a/thermo_sensor/main/ygg_state.h b/thermo_sensor/main/ygg_state.h new file mode 100644 index 0000000..2ba4def --- /dev/null +++ b/thermo_sensor/main/ygg_state.h @@ -0,0 +1,13 @@ +#ifndef YGG_STATE_H +#define YGG_STATE_H + +typedef struct { + float temperature; + float pressure; + float humidity; +} ygg_sensor_readout; + +extern ygg_sensor_readout CURR_READOUT; +static const char *SENSOR_NAME = "BMP_280_SENSOR_001"; + +#endif