From 85cde3687fa7e17103a60e0a610986605fd5d117 Mon Sep 17 00:00:00 2001 From: Wang Beihong Date: Tue, 21 Apr 2026 01:04:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=94=B5=E5=99=A8=E8=A6=81=E9=A3=9E?= =?UTF-8?q?=E7=BA=BF=EF=BC=8C=E5=85=88=E5=88=A0=E9=99=A4=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.cpp | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 0dda797..f826c9c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -22,7 +22,8 @@ #define TAG "MAIN" -typedef struct { +typedef struct +{ char time_str[32]; float lux; float temp; @@ -35,8 +36,10 @@ static SemaphoreHandle_t s_env_data_lock = NULL; static bool wait_for_wifi_connected(TickType_t timeout_ticks) { const TickType_t start_ticks = xTaskGetTickCount(); - while ((xTaskGetTickCount() - start_ticks) < timeout_ticks) { - if (wifi_connect_get_status() == WIFI_CONNECT_STATUS_CONNECTED) return true; + while ((xTaskGetTickCount() - start_ticks) < timeout_ticks) + { + if (wifi_connect_get_status() == WIFI_CONNECT_STATUS_CONNECTED) + return true; vTaskDelay(pdMS_TO_TICKS(200)); } return false; @@ -44,7 +47,8 @@ static bool wait_for_wifi_connected(TickType_t timeout_ticks) static void env_data_update_system_info(void) { - if (s_env_data_lock == NULL) return; + if (s_env_data_lock == NULL) + return; time_t now; struct tm timeinfo; time(&now); @@ -60,7 +64,8 @@ static void env_data_update_system_info(void) static void ui_task(void *arg) { - for (;;) { + for (;;) + { env_data_update_system_info(); lvgl_port_lock(0); ui_tick(); @@ -86,32 +91,27 @@ extern "C" void app_main(void) ui_init(); lvgl_port_unlock(); - // 7. 创建 UI 任务 + // 7. 创建 UI 任务 xTaskCreate(ui_task, "ui_task", 8192, NULL, 5, NULL); - if (wait_for_wifi_connected(pdMS_TO_TICKS(15000))) { + if (wait_for_wifi_connected(pdMS_TO_TICKS(15000))) + { set_var_system_ip(wifi_connect_get_ip()); } - // 4. 初始化继电器 (避开 PSRAM 引脚) - static const relay_config_t relay_cfg[RELAY_CTRL_ID_MAX] = { - {.pin = GPIO_NUM_38, .active_high = true}, - {.pin = GPIO_NUM_39, .active_high = false}, - {.pin = GPIO_NUM_40, .active_high = false}, - }; - relay_ctrl_init(relay_cfg); // 5. 初始化 I2C 总线并注册传感器 (共享总线) ESP_ERROR_CHECK(bh1750_user_init()); i2c_master_bus_handle_t i2c_bus = bh1750_get_i2c_bus_handle(); - + // AHT30 挂载到同一条 I2C 总线上 aht30_handle_t aht30_dev = NULL; ESP_ERROR_CHECK(aht30_create(i2c_bus, AHT30_I2C_ADDRESS, &aht30_dev)); // 6. 创建传感器读取任务 - xTaskCreate([](void *arg) { + xTaskCreate([](void *arg) + { aht30_handle_t aht30 = (aht30_handle_t)arg; for (;;) { float lux = 0.0f, temp = 0.0f, hum = 0.0f; @@ -135,8 +135,5 @@ extern "C" void app_main(void) xSemaphoreGive(s_env_data_lock); } vTaskDelay(pdMS_TO_TICKS(1000)); - } - }, "sensor_task", 4096*2, (void*)aht30_dev, 6, NULL); - - + } }, "sensor_task", 4096 * 2, (void *)aht30_dev, 6, NULL); }