- 从vars.c和vars.h中移除了空气温度整型变量及其相关的获取/设置函数 - 更新了FlowGlobalVariables枚举,移除了空气温度整型常量 - 修改了main.c中的UI任务,实现了每3秒切换屏幕的机制 - 清理了app_main函数,移除了空气温度整型的设置,仅保留字符串表示形式
226 lines
7.1 KiB
C
Executable File
226 lines
7.1 KiB
C
Executable File
#include <stdio.h>
|
||
#include <inttypes.h>
|
||
#include "sdkconfig.h"
|
||
#include "freertos/FreeRTOS.h"
|
||
#include "freertos/task.h"
|
||
#include "esp_check.h"
|
||
#include "esp_log.h"
|
||
#include "wifi-connect.h"
|
||
#include "lvgl_st7735s_use.h"
|
||
#include "i2c_master_messager.h"
|
||
#include "io_device_control.h"
|
||
#include "console_simple_init.h" // 提供 console_cmd_user_register 和 console_cmd_all_register
|
||
#include "console_user_cmds.h"
|
||
#include "capactive_soil_moisture_sensor_V2.0.h"
|
||
#include "ui.h" // 使用EEZStudio提供的ui组件,便于后续扩展
|
||
#include "esp_lvgl_port.h"
|
||
#include "vars.h" // 定义全局变量接口
|
||
|
||
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
|
||
#define CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE 0
|
||
#endif
|
||
|
||
#ifndef CONFIG_I2C_MASTER_MESSAGER_AHT30_ENABLE
|
||
#define CONFIG_I2C_MASTER_MESSAGER_AHT30_ENABLE 0
|
||
#endif
|
||
|
||
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS
|
||
#define CONFIG_I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS 500
|
||
#endif
|
||
|
||
#ifndef CONFIG_I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS
|
||
#define CONFIG_I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS 2000
|
||
#endif
|
||
|
||
#ifndef CONFIG_I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP
|
||
#define CONFIG_I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP 1
|
||
#endif
|
||
|
||
#define BOTANY_I2C_PORT I2C_NUM_0
|
||
#define BOTANY_I2C_SCL_GPIO GPIO_NUM_5
|
||
#define BOTANY_I2C_SDA_GPIO GPIO_NUM_4
|
||
#define BOTANY_BH1750_ENABLE CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
|
||
#define BOTANY_AHT30_ENABLE CONFIG_I2C_MASTER_MESSAGER_AHT30_ENABLE
|
||
#define BOTANY_BH1750_PERIOD_MS CONFIG_I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS
|
||
#define BOTANY_AHT30_PERIOD_MS CONFIG_I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS
|
||
#define BOTANY_I2C_INTERNAL_PULLUP CONFIG_I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP
|
||
|
||
static const char *TAG = "main";
|
||
|
||
static char s_air_temp[16];
|
||
static char s_air_hum[16];
|
||
static char s_soil[16];
|
||
static char s_lux[16];
|
||
|
||
static void ui_task(void *arg)
|
||
{
|
||
(void)arg;
|
||
|
||
uint32_t elapsed_ms = 0;
|
||
enum ScreensEnum current = SCREEN_ID_TEMPERATURE;
|
||
const uint32_t switch_period_ms = 3000; // 每3秒切一次
|
||
|
||
for (;;)
|
||
{
|
||
lvgl_port_lock(0);
|
||
ui_tick();
|
||
|
||
elapsed_ms += 20;
|
||
if (elapsed_ms >= switch_period_ms) {
|
||
elapsed_ms = 0;
|
||
|
||
// 下一个页面:1->2->3->4->1
|
||
if (current >= _SCREEN_ID_LAST) {
|
||
current = _SCREEN_ID_FIRST;
|
||
} else {
|
||
current = (enum ScreensEnum)(current + 1);
|
||
}
|
||
|
||
loadScreen(current);
|
||
}
|
||
|
||
lvgl_port_unlock();
|
||
vTaskDelay(pdMS_TO_TICKS(20));
|
||
}
|
||
}
|
||
|
||
static void wait_for_wifi_connected(void)
|
||
{
|
||
const uint32_t timeout_s = 120;
|
||
uint32_t elapsed_half_s = 0;
|
||
|
||
ESP_LOGI(TAG, "等待 Wi-Fi 连接成功后再初始化 console...");
|
||
while (wifi_connect_get_status() != WIFI_CONNECT_STATUS_CONNECTED)
|
||
{
|
||
if (elapsed_half_s >= (timeout_s * 2))
|
||
{
|
||
ESP_LOGW(TAG, "等待 Wi-Fi 超时(%" PRIu32 "s),继续初始化 console", timeout_s);
|
||
return;
|
||
}
|
||
|
||
vTaskDelay(pdMS_TO_TICKS(500));
|
||
elapsed_half_s++;
|
||
|
||
// 每 5 秒打印一次等待状态,避免日志刷屏。
|
||
if ((elapsed_half_s % 10) == 0)
|
||
{
|
||
ESP_LOGI(TAG, "仍在等待 Wi-Fi 连接(%" PRIu32 "s)", elapsed_half_s / 2);
|
||
}
|
||
}
|
||
|
||
ESP_LOGI(TAG, "Wi-Fi 已连接,开始初始化 console");
|
||
}
|
||
|
||
void app_main(void)
|
||
{
|
||
// 初始化 Wi-Fi 配网组件,支持长按按键进入配网
|
||
ESP_ERROR_CHECK(wifi_connect_init());
|
||
printf("设备启动完成:长按按键进入配网模式,手机连接 ESP32-* 后访问 http://192.168.4.1\n");
|
||
|
||
// 启动 LVGL 演示程序,显示简单的界面
|
||
ESP_ERROR_CHECK(start_lvgl_demo());
|
||
|
||
// 初始化 UI 组件(需在 LVGL 锁内进行对象创建)
|
||
lvgl_port_lock(0);
|
||
ui_init();
|
||
lvgl_port_unlock();
|
||
|
||
BaseType_t ui_task_ok = xTaskCreate(ui_task, "ui_task", 4096, NULL, 5, NULL);
|
||
ESP_ERROR_CHECK(ui_task_ok == pdPASS ? ESP_OK : ESP_FAIL);
|
||
|
||
// 初始化 IO 设备控制组件(GPIO1 水泵,GPIO10 光照,高电平有效)
|
||
ESP_ERROR_CHECK(io_device_control_init());
|
||
|
||
i2c_master_messager_config_t i2c_cfg = {
|
||
.i2c_port = BOTANY_I2C_PORT,
|
||
.scl_io_num = BOTANY_I2C_SCL_GPIO,
|
||
.sda_io_num = BOTANY_I2C_SDA_GPIO,
|
||
.i2c_enable_internal_pullup = BOTANY_I2C_INTERNAL_PULLUP,
|
||
.bh1750_enable = BOTANY_BH1750_ENABLE,
|
||
.aht30_enable = BOTANY_AHT30_ENABLE,
|
||
.bh1750_read_period_ms = BOTANY_BH1750_PERIOD_MS,
|
||
.aht30_read_period_ms = BOTANY_AHT30_PERIOD_MS,
|
||
.bh1750_mode = BH1750_CONTINUE_1LX_RES,
|
||
|
||
};
|
||
|
||
bool i2c_ready = false;
|
||
esp_err_t ret = i2c_master_messager_init(&i2c_cfg);
|
||
if (ret == ESP_OK)
|
||
{
|
||
ret = i2c_master_messager_start();
|
||
}
|
||
|
||
if (ret != ESP_OK)
|
||
{
|
||
ESP_LOGE(TAG, "I2C 传感器管理启动失败: %s", esp_err_to_name(ret));
|
||
ESP_LOGW(TAG, "请检查 I2C 引脚/上拉电阻/端口占用情况,系统将继续运行但不采集传感器");
|
||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("I2C init failed"));
|
||
}
|
||
else
|
||
{
|
||
i2c_ready = true;
|
||
}
|
||
|
||
// 初始化电容式土壤湿度传感器(GPIO0 / ADC1_CH0)。
|
||
bool soil_ready = false;
|
||
cap_soil_sensor_config_t soil_cfg = {
|
||
.unit = CAP_SOIL_SENSOR_DEFAULT_UNIT,
|
||
.channel = CAP_SOIL_SENSOR_DEFAULT_CHANNEL,
|
||
.atten = ADC_ATTEN_DB_12,
|
||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||
// 标定值来自当前实测:空气中约 3824,水中约 1463。
|
||
.air_raw = 3824,
|
||
.water_raw = 1463,
|
||
};
|
||
ret = cap_soil_sensor_init(&soil_cfg);
|
||
if (ret != ESP_OK)
|
||
{
|
||
ESP_LOGE(TAG, "土壤湿度传感器初始化失败: %s", esp_err_to_name(ret));
|
||
}
|
||
else
|
||
{
|
||
soil_ready = true;
|
||
}
|
||
|
||
// 按需求:仅在 Wi-Fi 确认连通后再初始化 console。
|
||
wait_for_wifi_connected();
|
||
|
||
ESP_ERROR_CHECK(console_cmd_init());
|
||
ESP_ERROR_CHECK(console_user_cmds_register());
|
||
ESP_ERROR_CHECK(console_cmd_all_register()); // 可选:自动注册插件命令
|
||
ESP_ERROR_CHECK(console_cmd_start());
|
||
|
||
for (;;)
|
||
{
|
||
cap_soil_sensor_data_t soil_data = {0};
|
||
if (soil_ready && cap_soil_sensor_read(&soil_data) == ESP_OK)
|
||
{
|
||
// 读取成功
|
||
snprintf(s_soil, sizeof(s_soil), "%.0f", soil_data.moisture_percent);
|
||
set_var_soil_moisture(s_soil);
|
||
}
|
||
|
||
i2c_master_messager_data_t sensor_data = {0};
|
||
if (i2c_ready && i2c_master_messager_get_data(&sensor_data) == ESP_OK)
|
||
{
|
||
// 读取成功
|
||
if (sensor_data.aht30.valid)
|
||
{
|
||
snprintf(s_air_temp, sizeof(s_air_temp), "%.1f", sensor_data.aht30.temperature_c);
|
||
set_var_air_temperature(s_air_temp);
|
||
|
||
snprintf(s_air_hum, sizeof(s_air_hum), "%.1f", sensor_data.aht30.humidity_rh);
|
||
set_var_air_humidity(s_air_hum);
|
||
}
|
||
if (sensor_data.bh1750.valid)
|
||
{
|
||
snprintf(s_lux, sizeof(s_lux), "%.0f", sensor_data.bh1750.lux);
|
||
set_var_light_intensity(s_lux);
|
||
}
|
||
}
|
||
|
||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||
}
|
||
}
|