diff --git a/main/app_state.c b/main/app_state.c index af45725..322e176 100644 --- a/main/app_state.c +++ b/main/app_state.c @@ -14,7 +14,7 @@ uint8_t led_brightness_value = 0; bool led_backlight_on = false; device_message_t g_device_message; -float g_temperature_threshold = 28.0f; +float g_temperature_threshold = 39.0f; bool g_cooling_mode_enabled = false; bool g_high_temp_alerted = false; diff --git a/main/main.cpp b/main/main.cpp index 8513846..13c420a 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -130,10 +130,11 @@ static void safe_mqtt_start_task(void *pvParameters) ESP_LOGI(TAG, "WiFi连接成功 (IP获取可能需要更多时间),准备启动MQTT"); break; } - + // 增加对 FAILED 的处理,避免死等 - if (wifi_status == WIFI_CONNECT_STATUS_FAILED) { - ESP_LOGW(TAG, "WiFi连接明确失败,等待重连..."); + if (wifi_status == WIFI_CONNECT_STATUS_FAILED) + { + ESP_LOGW(TAG, "WiFi连接明确失败,等待重连..."); } vTaskDelay(pdMS_TO_TICKS(1000)); @@ -170,11 +171,11 @@ static void mqtt_app_start(void) // 这个任务只是等待,不需要大栈空间 BaseType_t task_ok = xTaskCreate(safe_mqtt_start_task, "safe_mqtt_start", - 2048, // 修改这里:从 3072 降为 2048 + 2048, // 修改这里:从 3072 降为 2048 NULL, 5, NULL); - + if (task_ok != pdPASS) { ESP_LOGE(TAG, "创建安全启动任务失败(内存不足),直接启动MQTT"); @@ -441,8 +442,6 @@ extern "C" // MCU间的串口通信初始化 serial_mcu_init(); - - mqtt_app_start(); // 启动 MQTT 客户端 ESP_ERROR_CHECK(time_alarm_start()); // 创建降温模式任务 @@ -545,6 +544,15 @@ static void cooling_mode_task(void *pvParameters) ESP_LOGW(TAG, "High temperature alert: %.1f°C (>%.1f°C)", current_temp, g_temperature_threshold); + // 延时3秒后自动关闭蜂鸣器 + vTaskDelay(pdMS_TO_TICKS(3000)); + if (xControlFlagMutex != NULL && xSemaphoreTake(xControlFlagMutex, portMAX_DELAY) == pdTRUE) + { + buzzer_control_flag = false; + xSemaphoreGive(xControlFlagMutex); + } + ESP_LOGI(TAG, "蜂鸣器已自动关闭(3秒)"); + // 发送MQTT提醒消息 if (g_mqtt_client != NULL) { @@ -606,18 +614,7 @@ static void cooling_mode_task(void *pvParameters) } } -/** - * @brief 自动通风控制模式任务 - * 监测空气质量,当空气质量大于50时自动开启风扇并发送提醒 - */ -/* ventilation_mode_task 已移除,保留 README 文档说明,不再在代码中保留未使用的静态函数 */ -// MQ135 task removed; provide a short stub to avoid undefined references -void mq135_task(void *pvParameters) -{ - ESP_LOGI("mq135_task", "mq135 task removed"); - vTaskDelete(NULL); -} // Remaining MQ135 implementation removed @@ -643,7 +640,7 @@ extern "C" ESP_LOGE(TAG, "Failed to save cooling enabled: %s", esp_err_to_name(err)); } - err = nvs_set_u32(nvs_handle, "temperature_threshold", (uint32_t)(g_temperature_threshold * 10)); + err = nvs_set_u32(nvs_handle, "temp_th", (uint32_t)(g_temperature_threshold * 10)); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to save temperature threshold: %s", esp_err_to_name(err)); @@ -661,7 +658,7 @@ extern "C" { ESP_LOGI(TAG, "No cooling config found in NVS, using defaults"); g_cooling_mode_enabled = false; - g_temperature_threshold = 28.0f; + g_temperature_threshold = 39.0f; return; } @@ -676,15 +673,15 @@ extern "C" g_cooling_mode_enabled = false; } - uint32_t temp_threshold = 280; // 28.0°C * 10 - err = nvs_get_u32(nvs_handle, "temperature_threshold", &temp_threshold); + uint32_t temp_threshold = 390; // 39.0°C * 10 + err = nvs_get_u32(nvs_handle, "temp_th", &temp_threshold); if (err == ESP_OK) { g_temperature_threshold = temp_threshold / 10.0f; } else { - g_temperature_threshold = 28.0f; + g_temperature_threshold = 39.0f; } nvs_close(nvs_handle); diff --git a/main/mqtt_manager.c b/main/mqtt_manager.c index 5ff7033..323c035 100644 --- a/main/mqtt_manager.c +++ b/main/mqtt_manager.c @@ -571,7 +571,6 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ { bool auto_mode = cJSON_IsTrue(auto_mode_obj); ESP_LOGI(TAG, "Setting auto_mode to: %s", auto_mode ? "true (Auto)" : "false (Manual)"); - if (xMqttMessageMutex != NULL && xSemaphoreTake(xMqttMessageMutex, portMAX_DELAY) == pdTRUE) { g_device_message.state.auto_mode = auto_mode; @@ -584,7 +583,22 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ } xSemaphoreGive(xMqttMessageMutex); } - + + // 自动/手动模式联动自动功能 + if (auto_mode) { + g_cooling_mode_enabled = true; + g_use_time_period = true; + cooling_mode_save_to_nvs(); + time_period_save_to_nvs(); + ESP_LOGI(TAG, "自动模式: 已开启降温、时间段等自动功能"); + } else { + g_cooling_mode_enabled = false; + g_use_time_period = false; + cooling_mode_save_to_nvs(); + time_period_save_to_nvs(); + ESP_LOGI(TAG, "手动模式: 已关闭降温、时间段等自动功能"); + } + // 发布反馈消息 mqtt_manager_publish_feedback(); } diff --git a/main/sensors.c b/main/sensors.c index 05c7ca3..6144b18 100644 --- a/main/sensors.c +++ b/main/sensors.c @@ -278,12 +278,12 @@ void get_sensor_data(sensor_data_t *data) esp_err_t sensors_init(void) { - esp_err_t err = i2c_master_init(); - if (err != ESP_OK) - { - ESP_LOGW(TAG, "i2c_master_init failed: %s", esp_err_to_name(err)); - return err; - } + // esp_err_t err = i2c_master_init(); + // if (err != ESP_OK) + // { + // ESP_LOGW(TAG, "i2c_master_init failed: %s", esp_err_to_name(err)); + // return err; + // } ESP_LOGI(TAG, "创建传感器任务前堆: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT)); /* i2c0_ahtxx_task 需要较大栈,防止栈溢出(观察到任务崩溃),恢复为 4096 */ diff --git a/main/time_alarm.c b/main/time_alarm.c index dfd9a1c..285b60a 100644 --- a/main/time_alarm.c +++ b/main/time_alarm.c @@ -221,7 +221,7 @@ static void alarm_trigger_action(int idx) // 调整栈为2048以节省内存(保守测试)并记录堆情况 ESP_LOGI(TAG, "创建 alarm_auto_stop_task 前堆: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT)); // 降低自动停止任务栈到 1536,释放堆空间(如遇溢出可再调大) - BaseType_t result = xTaskCreate(alarm_auto_stop_task, "alarm_stop_task", 1536, (void *)idx, 3, &stop_task_handle); + BaseType_t result = xTaskCreate(alarm_auto_stop_task, "alarm_stop_task", 2048, (void *)idx, 3, &stop_task_handle); ESP_LOGI(TAG, "创建 alarm_auto_stop_task 后堆: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT)); if (result == pdPASS) {