新增:添加 Wi-Fi 和 MQTT 连接状态变量,并更新 UI
- 引入了新的全局变量,用于跟踪 Wi-Fi 和 MQTT 连接状态。 - 实现了 Wi-Fi 和 MQTT 连接状态的 getter 和 setter 函数。 - 增加了管理网络信息和时间同步的功能。 - 更新了主应用程序逻辑,以反映 UI 中的当前连接状态。 - 改进了 Wi-Fi 连接处理,以定期发布网络信息。 -土壤湿度和空气温度的综合阈值变量被整合到 UI 中。
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
idf_component_register(SRCS "main.c" "auto_ctrl_thresholds.c" "auto_alerts.c" "status_web.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES wifi-connect mqtt_control esp_lvgl_port lvgl_st7789_use i2c_master_messager io_device_control console_simple_init console console_user_cmds capactive_soil_moisture_sensor_V2.0 ui esp_app_format
|
||||
REQUIRES wifi-connect mqtt_control esp_lvgl_port lvgl_st7789_use i2c_master_messager io_device_control console_simple_init console console_user_cmds capactive_soil_moisture_sensor_V2.0 ui esp_app_format sntp_timp
|
||||
)
|
||||
|
||||
57
main/main.c
57
main/main.c
@@ -19,6 +19,7 @@
|
||||
#include "auto_alerts.h"
|
||||
#include "mqtt_control.h" // MQTT 控制接口
|
||||
#include "status_web.h"
|
||||
#include "sntp_timp.h"
|
||||
|
||||
// 配置宏定义:BH1750 光照传感器是否启用(默认禁用)
|
||||
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
|
||||
@@ -152,6 +153,31 @@ static void update_status_web_snapshot(void)
|
||||
}
|
||||
}
|
||||
|
||||
static void update_ui_threshold_vars(const auto_ctrl_thresholds_t *thresholds)
|
||||
{
|
||||
if (thresholds == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
char soil_threshold_text[16] = {0};
|
||||
char air_threshold_text[16] = {0};
|
||||
|
||||
snprintf(soil_threshold_text,
|
||||
sizeof(soil_threshold_text),
|
||||
"%.0f~%.0f",
|
||||
thresholds->pump_on_soil_below_pct,
|
||||
thresholds->pump_off_soil_above_pct);
|
||||
snprintf(air_threshold_text,
|
||||
sizeof(air_threshold_text),
|
||||
"%.0f~%.0f",
|
||||
thresholds->light_on_lux_below,
|
||||
thresholds->light_off_lux_above);
|
||||
|
||||
set_var_soil_mois_num(soil_threshold_text);
|
||||
set_var_air_temp_num(air_threshold_text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MQTT 控制命令处理函数
|
||||
*
|
||||
@@ -440,6 +466,9 @@ static void ui_task(void *arg)
|
||||
*/
|
||||
static void wait_for_wifi_connected(void)
|
||||
{
|
||||
set_var_wifi_disconnected(true);
|
||||
set_var_wifi_connected(false);
|
||||
|
||||
const uint32_t timeout_s = 120;
|
||||
uint32_t elapsed_half_s = 0;
|
||||
|
||||
@@ -462,9 +491,22 @@ static void wait_for_wifi_connected(void)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
set_var_wifi_disconnected(false);
|
||||
set_var_wifi_connected(true);
|
||||
ESP_LOGI(TAG, "Wi-Fi 已连接,开始初始化 console");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 同步 MQTT 连接状态到 UI 变量
|
||||
*/
|
||||
static void update_mqtt_ui_state(void)
|
||||
{
|
||||
bool mqtt_connected = mqtt_control_is_connected();
|
||||
set_var_mqtt_connected(mqtt_connected);
|
||||
set_var_mqtt_disconnected(!mqtt_connected);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 主函数
|
||||
*
|
||||
@@ -547,9 +589,20 @@ void app_main(void)
|
||||
// 按需求:仅在 Wi-Fi 确认连通后再初始化 MQTT和console。
|
||||
wait_for_wifi_connected();
|
||||
|
||||
// Wi-Fi 连通后做一次 SNTP 对时,失败不阻断后续业务。
|
||||
esp_err_t sntp_ret = sntp_timp_sync_time(12000);
|
||||
if (sntp_ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "SNTP 对时未完成: %s", esp_err_to_name(sntp_ret));
|
||||
}
|
||||
|
||||
// 独立状态网页(端口 8080),与配网页面(端口 80)互不干扰。
|
||||
ESP_ERROR_CHECK(status_web_start(BOTANY_STATUS_WEB_PORT));
|
||||
|
||||
// MQTT 启动前,先给 UI 一个明确的初始状态。
|
||||
set_var_mqtt_connected(false);
|
||||
set_var_mqtt_disconnected(true);
|
||||
|
||||
ESP_ERROR_CHECK(mqtt_control_register_command_handler(mqtt_control_command_handler, NULL));
|
||||
ESP_ERROR_CHECK(mqtt_control_start()); // 启动 MQTT 客户端
|
||||
|
||||
@@ -563,6 +616,7 @@ void app_main(void)
|
||||
ESP_ERROR_CHECK(auto_alerts_register_callback(auto_alert_mqtt_callback, NULL));
|
||||
auto_ctrl_thresholds_t thresholds = {0};
|
||||
auto_ctrl_thresholds_get(&thresholds);
|
||||
update_ui_threshold_vars(&thresholds);
|
||||
|
||||
uint32_t telemetry_elapsed_ms = 0;
|
||||
ESP_LOGI(TAG,
|
||||
@@ -576,8 +630,11 @@ void app_main(void)
|
||||
{
|
||||
s_main_loop_counter++;
|
||||
|
||||
update_mqtt_ui_state();
|
||||
|
||||
// 预留给 MQTT 回调动态更新阈值:每个周期读取最新配置。
|
||||
auto_ctrl_thresholds_get(&thresholds);
|
||||
update_ui_threshold_vars(&thresholds);
|
||||
|
||||
bool soil_valid = false;
|
||||
float soil_moisture_pct = 0.0f;
|
||||
|
||||
Reference in New Issue
Block a user