feat: 添加健康监控系统并重构代码结构

- 新增health_monitor模块,包含温度、电压、WiFi RSSI监控功能
- 移除旧的app_main.h/app_main.c和debug模块,将调试日志移动到monitor目录
- 在main.cpp中替换system_monitor_task为health_monitor_task
- 更新CMakeLists.txt依赖项,添加esp_driver_tsens和esp_driver_uart
- 修改WiFi配置参数,更新SSID和密码设置方式
- 在Python工具中修复OTA客户端重新连接逻辑
- 调整任务优先级分配,统一使用核心0以适配ESP32-C3单核架构
This commit is contained in:
小龙虾
2026-04-27 02:34:47 +08:00
parent bcfe130451
commit 7bb28cee1e
32 changed files with 177 additions and 191 deletions
-97
View File
@@ -1,97 +0,0 @@
/*
* app_main.c - 应用程序初始化与健康监控
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_timer.h"
#include "esp_chip_info.h"
#include "esp_system.h"
/* ESP-IDF 6.0: temperature_sensor.h 已移除,使用替代方案 */
#include "ota_relay.h"
#include "health_monitor.h"
#include "debug_log.h"
extern health_state_t g_health_state;
/* ESP-IDF 6.0: temperature_sensor 驱动已移除,使用系统负载估算温度 */
static int32_t s_temp_estimate_celsius = 0;
// 芯片温度监控(1秒间隔)- 使用系统负载估算
static void temp_monitor_callback(void *arg)
{
/* ESP-IDF 6.0 替代方案:使用空闲任务时间估算温度
* 实际项目中建议使用外部温度传感器(如 I2C TMP107)
*/
(void)esp_timer_get_time(); // 获取时间戳用于负载估算(暂未使用)
DEBUG_DBG("TEMP", "芯片温度估算: %ld°C (基于系统负载)", s_temp_estimate_celsius);
if (s_temp_estimate_celsius > 100) {
g_health_state = SYS_SAFE_SHUTDOWN;
DEBUG_ERR("TEMP", "危险: 温度 %ld°C > 100°C,触发安全关机", s_temp_estimate_celsius);
} else if (s_temp_estimate_celsius > 80) {
if (g_health_state < SYS_WARNING) {
g_health_state = SYS_WARNING;
DEBUG_WARN("TEMP", "警告: 温度 %ld°C > 80°C", s_temp_estimate_celsius);
}
}
}
// 电压监控(启动时检查一次)
static void voltage_monitor_callback(void *arg)
{
// ESP-IDF 6.0: esp_efuse_get_voltage 已移除
// 使用回退方案:假设 3.3V
float voltage = 3.30f;
DEBUG_DBG("VOLT", "VDD 电压: %.2fV (估算)", voltage);
if (voltage < 2.3f) {
g_health_state = SYS_SAFE_SHUTDOWN;
DEBUG_ERR("VOLT", "危险: 电压 %.2fV < 2.3V,触发安全关机", voltage);
} else if (voltage < 2.5f) {
if (g_health_state < SYS_WARNING) {
g_health_state = SYS_WARNING;
DEBUG_WARN("VOLT", "警告: 电压 %.2fV < 2.5V", voltage);
}
}
}
// 系统监控任务
void system_monitor_task(void *pvParameters)
{
DEBUG_INFO("MON", "系统监控任务启动");
/* ESP-IDF 6.0: temperature_sensor 驱动已移除
* 使用默认温度估计值(实际项目建议使用外部传感器)
*/
s_temp_estimate_celsius = 25; // 默认 25°C
// 创建定时器进行周期性监控
esp_timer_handle_t temp_timer;
esp_timer_create_args_t timer_args = {};
timer_args.callback = &temp_monitor_callback;
timer_args.arg = nullptr;
timer_args.name = "temp_monitor";
timer_args.dispatch_method = ESP_TIMER_TASK;
timer_args.skip_unhandled_events = false;
esp_timer_create(&timer_args, &temp_timer);
esp_timer_start_periodic(temp_timer, 1 * 1000000); // 1秒
// 启动时检查一次电压
voltage_monitor_callback(NULL);
while (1) {
// 每30秒记录一次健康状态
DEBUG_INFO("MON", "健康状态: %d, 空闲堆: %lu",
g_health_state, esp_get_free_heap_size());
vTaskDelay(pdMS_TO_TICKS(30000));
}
}
// 当 eFuse 读取不可用时的电压回退方案
uint32_t read_voltage_mv(void)
{
return 3300; // 默认 3.3V 假设
}
-15
View File
@@ -1,15 +0,0 @@
/*
* app_main.h - 应用程序初始化与健康监控
*/
#ifndef APP_MAIN_H
#define APP_MAIN_H
#include <stdint.h>
/* 系统监控任务 */
void system_monitor_task(void *pvParameters);
/* 电压读取(回退方案) */
uint32_t read_voltage_mv(void);
#endif /* APP_MAIN_H */
+20 -25
View File
@@ -8,17 +8,15 @@
#include "esp_log.h"
#include "esp_chip_info.h"
#include "core/app_main.h"
#include "ota_relay.h"
#include "spi_master.h"
#include "wifi_ota.h"
#include "ble_ota.h"
#include "protocol.h"
#include "debug_log.h"
#include "monitor/health_monitor.h"
#include "relay/ota_relay.h"
#include "transport/spi_master.h"
#include "server/wifi_ota.h"
#include "server/ble_ota.h"
#include "transport/protocol.h"
#include "monitor/debug_log.h"
extern "C" void app_main(void);
void app_main(void)
extern "C" void app_main(void)
{
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
@@ -31,6 +29,17 @@ void app_main(void)
DEBUG_INFO("MAIN", "Free heap: %lu bytes", esp_get_free_heap_size());
DEBUG_INFO("MAIN", "========================================");
// 健康监控任务(温度 + 电压 + WiFi RSSI)
xTaskCreatePinnedToCore(
health_monitor_task,
"health_mon",
4096,
NULL,
configMAX_PRIORITIES - 4,
NULL,
0
);
// 初始化协议层(CRC、AES)
protocol_init();
DEBUG_INFO("MAIN", "协议层初始化完成");
@@ -46,20 +55,6 @@ void app_main(void)
// 创建 FreeRTOS 任务
BaseType_t xRet;
// 系统监控任务(最高优先级)
xRet = xTaskCreatePinnedToCore(
system_monitor_task,
"sys_monitor",
4096,
NULL,
configMAX_PRIORITIES - 1,
NULL,
1 // 核1
);
if (xRet != pdPASS) {
DEBUG_ERR("MAIN", "创建 system_monitor_task 失败");
}
// OTA 中继任务(中高优先级)
xRet = xTaskCreatePinnedToCore(
ota_relay_task,
@@ -96,7 +91,7 @@ void app_main(void)
NULL,
configMAX_PRIORITIES - 3,
NULL,
1
0 // ESP32-C3 单核,固定核0
);
if (xRet != pdPASS) {
DEBUG_ERR("MAIN", "创建 ble_ota_task 失败");