Files
BotanicalBuddy/main/main.c

137 lines
4.6 KiB
C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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"
#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";
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;
}
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));
}
}