From 98fb81702b5ca0d6387169d0f31cee86724dad50 Mon Sep 17 00:00:00 2001 From: Mateusz Date: Fri, 11 Sep 2026 13:34:55 +0200 Subject: [PATCH] endpoints for pressure and humidity --- thermo_sensor/main/thermo_sensor.c | 4 +++ thermo_sensor/main/ygg_bmp280.c | 4 +++ thermo_sensor/main/ygg_http_routes.c | 44 ++++++++++++++++++++++++---- thermo_sensor/main/ygg_http_routes.h | 7 +++++ 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/thermo_sensor/main/thermo_sensor.c b/thermo_sensor/main/thermo_sensor.c index a3839c5..697038c 100644 --- a/thermo_sensor/main/thermo_sensor.c +++ b/thermo_sensor/main/thermo_sensor.c @@ -15,6 +15,8 @@ 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) { @@ -36,6 +38,8 @@ void app_main() httpd_handle_t server = ygg_start_webserver(); httpd_register_uri_handler(server, &ygg_temp_uri); + httpd_register_uri_handler(server, &ygg_press_uri); + httpd_register_uri_handler(server, &ygg_hum_uri); ESP_ERROR_CHECK(i2cdev_init()); ygg_bmp280_init(&dev_bmp_280); diff --git a/thermo_sensor/main/ygg_bmp280.c b/thermo_sensor/main/ygg_bmp280.c index d9933ef..99e6eec 100644 --- a/thermo_sensor/main/ygg_bmp280.c +++ b/thermo_sensor/main/ygg_bmp280.c @@ -4,6 +4,8 @@ #include "ygg_bmp280.h" extern float last_temperature; +extern float last_pressure; +extern float last_humidity; void ygg_bmp280_init(bmp280_t *dev) { bmp280_params_t params; @@ -25,6 +27,8 @@ void ygg_bmp280_read_sensor(bmp280_t *dev) { printf("Temperature/pressure reading failed\n"); last_temperature = temperature; + last_pressure = pressure; + last_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_routes.c b/thermo_sensor/main/ygg_http_routes.c index 244af7f..fb9bc1f 100644 --- a/thermo_sensor/main/ygg_http_routes.c +++ b/thermo_sensor/main/ygg_http_routes.c @@ -2,23 +2,55 @@ #include "ygg_http_routes.h" extern float last_temperature; +extern float last_pressure; +extern float last_humidity; -esp_err_t ygg_get_temp_handler(httpd_req_t *req) +esp_err_t ygg_get_handler(httpd_req_t *req) { - float current_temp = last_temperature; char response_json[64]; - snprintf(response_json, sizeof(response_json), "{\"temperature\": %.1f}", current_temp); + ygg_http_user_ctx *user_ctx = req->user_ctx; + float current_val = *user_ctx->param_val; + snprintf(response_json, sizeof(response_json), + "{\"%s\": %.1f}", user_ctx->param_name, current_val); httpd_resp_set_type(req, "application/json"); - httpd_resp_send(req, response_json, HTTPD_RESP_USE_STRLEN); return ESP_OK; } +static ygg_http_user_ctx temp_ctx = { + .param_name = "temperature", + .param_val = &last_temperature +}; + const httpd_uri_t ygg_temp_uri = { .uri = "/temp", .method = HTTP_GET, - .handler = ygg_get_temp_handler, - .user_ctx = NULL + .handler = ygg_get_handler, + .user_ctx = &temp_ctx +}; + +static ygg_http_user_ctx press_ctx = { + .param_name = "pressure", + .param_val = &last_pressure +}; + +const httpd_uri_t ygg_press_uri = { + .uri = "/press", + .method = HTTP_GET, + .handler = ygg_get_handler, + .user_ctx = &press_ctx +}; + +static ygg_http_user_ctx hum_ctx = { + .param_name = "humidity", + .param_val = &last_humidity +}; + +const httpd_uri_t ygg_hum_uri = { + .uri = "/hum", + .method = HTTP_GET, + .handler = ygg_get_handler, + .user_ctx = &hum_ctx }; diff --git a/thermo_sensor/main/ygg_http_routes.h b/thermo_sensor/main/ygg_http_routes.h index 9ca6f8e..272dd60 100644 --- a/thermo_sensor/main/ygg_http_routes.h +++ b/thermo_sensor/main/ygg_http_routes.h @@ -4,5 +4,12 @@ esp_err_t ygg_get_temp_handler(httpd_req_t *req); extern const httpd_uri_t ygg_temp_uri; +extern const httpd_uri_t ygg_press_uri; +extern const httpd_uri_t ygg_hum_uri; + +typedef struct { + const char *param_name; + float *param_val; +} ygg_http_user_ctx; #endif