173 lines
5.8 KiB
C
Executable File
173 lines
5.8 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"
|
||
|
||
#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 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());
|
||
|
||
// 初始化 IO 设备控制组件(GPIO1 水泵,GPIO0 光照,高电平有效)
|
||
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;
|
||
}
|
||
|
||
// 按需求:仅在 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 (;;)
|
||
{
|
||
i2c_master_messager_data_t sensor_data = {0};
|
||
if (i2c_ready && i2c_master_messager_get_data(&sensor_data) == ESP_OK)
|
||
{
|
||
char text[64] = {0};
|
||
|
||
if (BOTANY_BH1750_ENABLE && BOTANY_AHT30_ENABLE &&
|
||
sensor_data.bh1750.valid && sensor_data.aht30.valid)
|
||
{
|
||
snprintf(text,
|
||
sizeof(text),
|
||
"L:%.0f T:%.1fC\nH:%.1f%%",
|
||
sensor_data.bh1750.lux,
|
||
sensor_data.aht30.temperature_c,
|
||
sensor_data.aht30.humidity_rh);
|
||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
|
||
}
|
||
else if (BOTANY_BH1750_ENABLE && sensor_data.bh1750.valid)
|
||
{
|
||
snprintf(text, sizeof(text), "Light: %.1f lx", sensor_data.bh1750.lux);
|
||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
|
||
}
|
||
else if (BOTANY_AHT30_ENABLE && sensor_data.aht30.valid)
|
||
{
|
||
snprintf(text,
|
||
sizeof(text),
|
||
"T:%.1fC\nH:%.1f%%",
|
||
sensor_data.aht30.temperature_c,
|
||
sensor_data.aht30.humidity_rh);
|
||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
|
||
}
|
||
else
|
||
{
|
||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("Sensor waiting..."));
|
||
}
|
||
}
|
||
else if (i2c_ready)
|
||
{
|
||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("Sensor read fail"));
|
||
}
|
||
else
|
||
{
|
||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||
}
|
||
|
||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||
}
|
||
}
|