完成对光照和温湿度的采集,显示到LCD上,并存放在变量
This commit is contained in:
@@ -17,14 +17,14 @@
|
||||
#include "esp_system.h"
|
||||
#include "esp_efuse.h"
|
||||
#include "esp_efuse_table.h"
|
||||
#include "bh1750_use.h"
|
||||
#include "aht30.h"
|
||||
|
||||
#define TAG "MAIN"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
char time_str[32];
|
||||
char mac_str[20];
|
||||
char uid_str[20];
|
||||
float lux;
|
||||
float temp;
|
||||
float humidity;
|
||||
} env_data_t;
|
||||
@@ -35,22 +35,16 @@ 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 wifi_connect_get_status() == WIFI_CONNECT_STATUS_CONNECTED;
|
||||
return false;
|
||||
}
|
||||
|
||||
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,17 +54,13 @@ static void env_data_update_system_info(void)
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static void ui_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
for (;;)
|
||||
{
|
||||
for (;;) {
|
||||
env_data_update_system_info();
|
||||
lvgl_port_lock(0);
|
||||
ui_tick();
|
||||
@@ -83,42 +73,70 @@ extern "C" void app_main(void)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
ESP_LOGI(TAG, "--- APP STARTING ---");
|
||||
|
||||
s_env_data_lock = xSemaphoreCreateMutex();
|
||||
|
||||
// 2. 初始化 Wi-Fi
|
||||
// 1. 初始化 Wi-Fi
|
||||
ESP_ERROR_CHECK(wifi_connect_init());
|
||||
|
||||
// 3. 初始化显示屏和 LVGL
|
||||
// 2. 初始化显示屏和 LVGL
|
||||
start_lvgl_demo();
|
||||
|
||||
// 4. 初始化 UI
|
||||
// 3. 初始化 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("有人");
|
||||
set_var_hot_status("开");
|
||||
set_var_cool_status("关");
|
||||
set_var_fan_status("开");
|
||||
set_var_light_status("开");
|
||||
|
||||
// 7. 创建 UI 任务
|
||||
xTaskCreate(ui_task, "ui_task", 8192, NULL, 5, NULL);
|
||||
|
||||
if (wait_for_wifi_connected(pdMS_TO_TICKS(15000)))
|
||||
{
|
||||
ESP_LOGI(TAG, "IP: %s", wifi_connect_get_ip());
|
||||
if (wait_for_wifi_connected(pdMS_TO_TICKS(15000))) {
|
||||
set_var_system_ip(wifi_connect_get_ip());
|
||||
}
|
||||
|
||||
// 1. 初始化继电器 (避开 Octal PSRAM/Flash 所占用的引脚)
|
||||
// ESP32-S3 N16R8 使用 8线 PSRAM,占用 GPIO 33-37
|
||||
// 4. 初始化继电器 (避开 PSRAM 引脚)
|
||||
static const relay_config_t relay_cfg[RELAY_CTRL_ID_MAX] = {
|
||||
{.pin = GPIO_NUM_38, .active_high = true}, // 示例改为常用 GPIO
|
||||
{.pin = GPIO_NUM_38, .active_high = true},
|
||||
{.pin = GPIO_NUM_39, .active_high = false},
|
||||
{.pin = GPIO_NUM_40, .active_high = false},
|
||||
|
||||
};
|
||||
ESP_ERROR_CHECK(relay_ctrl_init(relay_cfg));
|
||||
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) {
|
||||
aht30_handle_t aht30 = (aht30_handle_t)arg;
|
||||
for (;;) {
|
||||
float lux = 0.0f, temp = 0.0f, hum = 0.0f;
|
||||
// 读取 BH1750
|
||||
if (bh1750_user_read(&lux) == ESP_OK) {
|
||||
set_var_light_val(lux);
|
||||
}
|
||||
// 读取 AHT30
|
||||
if (aht30_get_temperature_humidity_value(aht30, &temp, &hum) == ESP_OK) {
|
||||
set_var_temp(temp);
|
||||
set_var_humity_val(hum);
|
||||
}
|
||||
|
||||
|
||||
// 数据存入共享结构体
|
||||
if (s_env_data_lock) {
|
||||
xSemaphoreTake(s_env_data_lock, portMAX_DELAY);
|
||||
s_env_data.lux = lux;
|
||||
s_env_data.temp = temp;
|
||||
s_env_data.humidity = hum;
|
||||
xSemaphoreGive(s_env_data_lock);
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}, "sensor_task", 4096*2, (void*)aht30_dev, 6, NULL);
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user