完成继电器三个通道的设置,还有1通道引脚占用错误要修改
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wifi-connect.h"
|
||||
#include "esp_lvgl_port.h"
|
||||
#include "lvgl_st7789_use.h"
|
||||
#include "ui.h"
|
||||
#include "vars.h"
|
||||
#include "relay_ctrl.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
@@ -20,21 +20,18 @@
|
||||
|
||||
#define TAG "MAIN"
|
||||
|
||||
/**
|
||||
* @brief 系统环境数据结构体
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
char time_str[32];
|
||||
char mac_str[20];
|
||||
char uid_str[20];
|
||||
float temp;
|
||||
float humidity;
|
||||
} env_data_t;
|
||||
|
||||
// 定义全局变量和锁
|
||||
static env_data_t s_env_data;
|
||||
static SemaphoreHandle_t s_env_data_lock = NULL;
|
||||
|
||||
// 等待 Wi-Fi 连接成功
|
||||
static bool wait_for_wifi_connected(TickType_t timeout_ticks)
|
||||
{
|
||||
const TickType_t start_ticks = xTaskGetTickCount();
|
||||
@@ -49,15 +46,11 @@ static bool wait_for_wifi_connected(TickType_t timeout_ticks)
|
||||
return wifi_connect_get_status() == WIFI_CONNECT_STATUS_CONNECTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 更新系统状态到结构体,并同步到 UI
|
||||
*/
|
||||
static void env_data_update_system_info(void)
|
||||
{
|
||||
if (s_env_data_lock == NULL)
|
||||
return;
|
||||
|
||||
// 获取当前时间
|
||||
time_t now;
|
||||
struct tm timeinfo;
|
||||
time(&now);
|
||||
@@ -65,11 +58,11 @@ static void env_data_update_system_info(void)
|
||||
char time_buf[32];
|
||||
strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", &timeinfo);
|
||||
|
||||
// 更新结构体 (线程安全)
|
||||
xSemaphoreTake(s_env_data_lock, portMAX_DELAY);
|
||||
strncpy(s_env_data.time_str, time_buf, sizeof(s_env_data.time_str));
|
||||
|
||||
xSemaphoreGive(s_env_data_lock);
|
||||
|
||||
set_var_local_time(s_env_data.time_str);
|
||||
}
|
||||
|
||||
@@ -79,33 +72,31 @@ static void ui_task(void *arg)
|
||||
for (;;)
|
||||
{
|
||||
env_data_update_system_info();
|
||||
|
||||
lvgl_port_lock(0);
|
||||
ui_tick();
|
||||
lvgl_port_unlock();
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
vTaskDelay(pdMS_TO_TICKS(30));
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
printf("\n\n--- APP START ---\n\n");
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
ESP_LOGI(TAG, "--- APP STARTING ---");
|
||||
|
||||
// 创建结构体互斥锁
|
||||
s_env_data_lock = xSemaphoreCreateMutex();
|
||||
|
||||
// 初始化 Wi-Fi
|
||||
// 2. 初始化 Wi-Fi
|
||||
ESP_ERROR_CHECK(wifi_connect_init());
|
||||
|
||||
// 初始化屏幕和 LVGL
|
||||
// 3. 初始化显示屏和 LVGL
|
||||
start_lvgl_demo();
|
||||
|
||||
// 初始化 UI
|
||||
lvgl_port_lock(0);
|
||||
// 4. 初始化 UI
|
||||
lvgl_port_lock(100 / portTICK_PERIOD_MS);
|
||||
ui_init();
|
||||
lvgl_port_unlock();
|
||||
|
||||
// 设置初始状态变量 (常量)
|
||||
set_var_door_status("关闭");
|
||||
set_var_food_status("良好");
|
||||
set_var_hum_status("有人");
|
||||
@@ -114,18 +105,20 @@ extern "C" void app_main(void)
|
||||
set_var_fan_status("开");
|
||||
set_var_light_status("开");
|
||||
|
||||
// 创建 UI 刷新任务
|
||||
xTaskCreate(ui_task, "ui_task", 4096, NULL, 10, NULL);
|
||||
xTaskCreate(ui_task, "ui_task", 8192, NULL, 5, NULL);
|
||||
|
||||
// 等待网络并对时
|
||||
if (wait_for_wifi_connected(pdMS_TO_TICKS(60000)))
|
||||
if (wait_for_wifi_connected(pdMS_TO_TICKS(15000)))
|
||||
{
|
||||
printf("Wi-Fi connected, IP address: %s\n", wifi_connect_get_ip());
|
||||
ESP_LOGI(TAG, "IP: %s", wifi_connect_get_ip());
|
||||
set_var_system_ip(wifi_connect_get_ip());
|
||||
esp_err_t sntp_ret = sntp_timp_sync_time(12000);
|
||||
if (sntp_ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "SNTP sync failed");
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 初始化继电器 (避开 Octal PSRAM/Flash 所占用的引脚)
|
||||
// ESP32-S3 N16R8 使用 8线 PSRAM,占用 GPIO 33-37
|
||||
static const relay_config_t relay_cfg[RELAY_CTRL_ID_MAX] = {
|
||||
{.pin = GPIO_NUM_38, .active_high = true}, // 示例改为常用 GPIO
|
||||
{.pin = GPIO_NUM_39, .active_high = false},
|
||||
{.pin = GPIO_NUM_40, .active_high = false},
|
||||
};
|
||||
ESP_ERROR_CHECK(relay_ctrl_init(relay_cfg));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user