feat: 添加独立状态网页服务,提供设备状态实时监控和API接口
This commit is contained in:
42
main/main.c
42
main/main.c
@@ -18,6 +18,7 @@
|
||||
#include "auto_ctrl_thresholds.h"
|
||||
#include "auto_alerts.h"
|
||||
#include "mqtt_control.h" // MQTT 控制接口
|
||||
#include "status_web.h"
|
||||
|
||||
// 配置宏定义:BH1750 光照传感器是否启用(默认禁用)
|
||||
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
|
||||
@@ -64,6 +65,7 @@
|
||||
#define BOTANY_MQTT_ALERT_TOPIC "topic/alert/esp32_iothome_001"
|
||||
// MQTT 遥测数据上报周期(毫秒)
|
||||
#define BOTANY_MQTT_TELEMETRY_PERIOD_MS 5000
|
||||
#define BOTANY_STATUS_WEB_PORT 8080
|
||||
|
||||
// 日志标签
|
||||
static const char *TAG = "main";
|
||||
@@ -82,6 +84,9 @@ static bool s_pump_on = false;
|
||||
static bool s_light_on = false;
|
||||
// 全局变量:自动控制模式使能(true=自动,false=手动)
|
||||
static bool s_auto_control_enabled = true;
|
||||
static bool s_i2c_ready = false;
|
||||
static bool s_soil_sensor_ready = false;
|
||||
static uint32_t s_main_loop_counter = 0;
|
||||
|
||||
/**
|
||||
* @brief 发布当前完整状态快照(含阈值)到传感器主题
|
||||
@@ -119,6 +124,34 @@ static esp_err_t publish_telemetry_snapshot(void)
|
||||
return mqtt_control_publish_sensor(telemetry_payload, 0, 0);
|
||||
}
|
||||
|
||||
static void update_status_web_snapshot(void)
|
||||
{
|
||||
status_web_snapshot_t snap = {0};
|
||||
snprintf(snap.temp, sizeof(snap.temp), "%s", s_air_temp[0] ? s_air_temp : "--");
|
||||
snprintf(snap.hum, sizeof(snap.hum), "%s", s_air_hum[0] ? s_air_hum : "--");
|
||||
snprintf(snap.soil, sizeof(snap.soil), "%s", s_soil[0] ? s_soil : "--");
|
||||
snprintf(snap.lux, sizeof(snap.lux), "%s", s_lux[0] ? s_lux : "--");
|
||||
snap.pump_on = s_pump_on;
|
||||
snap.light_on = s_light_on;
|
||||
snap.auto_mode = s_auto_control_enabled;
|
||||
|
||||
auto_ctrl_thresholds_t thresholds = {0};
|
||||
auto_ctrl_thresholds_get(&thresholds);
|
||||
snap.soil_on_threshold = thresholds.pump_on_soil_below_pct;
|
||||
snap.soil_off_threshold = thresholds.pump_off_soil_above_pct;
|
||||
snap.light_on_threshold = thresholds.light_on_lux_below;
|
||||
snap.light_off_threshold = thresholds.light_off_lux_above;
|
||||
snap.i2c_ready = s_i2c_ready;
|
||||
snap.soil_sensor_ready = s_soil_sensor_ready;
|
||||
snap.loop_counter = s_main_loop_counter;
|
||||
|
||||
esp_err_t ret = status_web_update(&snap);
|
||||
if (ret != ESP_OK && ret != ESP_ERR_INVALID_STATE)
|
||||
{
|
||||
ESP_LOGW(TAG, "status web update failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MQTT 控制命令处理函数
|
||||
*
|
||||
@@ -504,6 +537,7 @@ void app_main(void)
|
||||
else
|
||||
{
|
||||
i2c_ready = true;
|
||||
s_i2c_ready = true;
|
||||
}
|
||||
|
||||
// 初始化电容式土壤湿度传感器(GPIO0 / ADC1_CH0)。
|
||||
@@ -525,11 +559,15 @@ void app_main(void)
|
||||
else
|
||||
{
|
||||
soil_ready = true;
|
||||
s_soil_sensor_ready = true;
|
||||
}
|
||||
|
||||
// 按需求:仅在 Wi-Fi 确认连通后再初始化 MQTT和console。
|
||||
wait_for_wifi_connected();
|
||||
|
||||
// 独立状态网页(端口 8080),与配网页面(端口 80)互不干扰。
|
||||
ESP_ERROR_CHECK(status_web_start(BOTANY_STATUS_WEB_PORT));
|
||||
|
||||
ESP_ERROR_CHECK(mqtt_control_register_command_handler(mqtt_control_command_handler, NULL));
|
||||
ESP_ERROR_CHECK(mqtt_control_start()); // 启动 MQTT 客户端
|
||||
|
||||
@@ -554,6 +592,8 @@ void app_main(void)
|
||||
|
||||
for (;;)
|
||||
{
|
||||
s_main_loop_counter++;
|
||||
|
||||
// 预留给 MQTT 回调动态更新阈值:每个周期读取最新配置。
|
||||
auto_ctrl_thresholds_get(&thresholds);
|
||||
|
||||
@@ -612,6 +652,8 @@ void app_main(void)
|
||||
light_lux,
|
||||
&thresholds);
|
||||
|
||||
update_status_web_snapshot();
|
||||
|
||||
telemetry_elapsed_ms += 1000;
|
||||
if (telemetry_elapsed_ms >= BOTANY_MQTT_TELEMETRY_PERIOD_MS)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user