endpoints for pressure and humidity

This commit is contained in:
2026-09-11 13:34:55 +02:00
parent 1261ad8f2d
commit 98fb81702b
4 changed files with 53 additions and 6 deletions
+4
View File
@@ -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);
+4
View File
@@ -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);
+38 -6
View File
@@ -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
};
+7
View File
@@ -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