Files

57 lines
1.3 KiB
C

#include "esp_http_server.h"
#include "ygg_http_routes.h"
extern float last_temperature;
extern float last_pressure;
extern float last_humidity;
esp_err_t ygg_get_handler(httpd_req_t *req)
{
char response_json[64];
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_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
};