feat: 添加电容式土壤湿度传感器支持,更新相关命令和初始化逻辑
This commit is contained in:
123
main/main.c
123
main/main.c
@@ -11,6 +11,7 @@
|
||||
#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"
|
||||
|
||||
#ifndef CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE
|
||||
#define CONFIG_I2C_MASTER_MESSAGER_BH1750_ENABLE 0
|
||||
@@ -113,6 +114,27 @@ void app_main(void)
|
||||
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();
|
||||
|
||||
@@ -123,6 +145,13 @@ void app_main(void)
|
||||
|
||||
for (;;)
|
||||
{
|
||||
cap_soil_sensor_data_t soil_data = {0};
|
||||
bool soil_ok = false;
|
||||
if (soil_ready && cap_soil_sensor_read(&soil_data) == ESP_OK)
|
||||
{
|
||||
soil_ok = true;
|
||||
}
|
||||
|
||||
i2c_master_messager_data_t sensor_data = {0};
|
||||
if (i2c_ready && i2c_master_messager_get_data(&sensor_data) == ESP_OK)
|
||||
{
|
||||
@@ -131,26 +160,71 @@ void app_main(void)
|
||||
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);
|
||||
if (soil_ok)
|
||||
{
|
||||
snprintf(text,
|
||||
sizeof(text),
|
||||
"L:%.0f S:%.0f%%\nT:%.1f H:%.1f%%",
|
||||
sensor_data.bh1750.lux,
|
||||
soil_data.moisture_percent,
|
||||
sensor_data.aht30.temperature_c,
|
||||
sensor_data.aht30.humidity_rh);
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
if (soil_ok)
|
||||
{
|
||||
snprintf(text,
|
||||
sizeof(text),
|
||||
"Light:%.0f\nSoil:%.0f%%",
|
||||
sensor_data.bh1750.lux,
|
||||
soil_data.moisture_percent);
|
||||
}
|
||||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (soil_ok)
|
||||
{
|
||||
snprintf(text,
|
||||
sizeof(text),
|
||||
"T:%.1f H:%.1f%%\nSoil:%.0f%%",
|
||||
sensor_data.aht30.temperature_c,
|
||||
sensor_data.aht30.humidity_rh,
|
||||
soil_data.moisture_percent);
|
||||
}
|
||||
else
|
||||
{
|
||||
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 if (soil_ok)
|
||||
{
|
||||
snprintf(text,
|
||||
sizeof(text),
|
||||
"T:%.1fC\nH:%.1f%%",
|
||||
sensor_data.aht30.temperature_c,
|
||||
sensor_data.aht30.humidity_rh);
|
||||
"Soil:%.0f%%\nADC:%d",
|
||||
soil_data.moisture_percent,
|
||||
soil_data.raw);
|
||||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
|
||||
}
|
||||
else
|
||||
@@ -160,11 +234,36 @@ void app_main(void)
|
||||
}
|
||||
else if (i2c_ready)
|
||||
{
|
||||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("Sensor read fail"));
|
||||
if (soil_ok)
|
||||
{
|
||||
char text[64] = {0};
|
||||
snprintf(text,
|
||||
sizeof(text),
|
||||
"I2C read fail\nSoil:%.0f%%",
|
||||
soil_data.moisture_percent);
|
||||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("Sensor read fail"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
if (soil_ok)
|
||||
{
|
||||
char text[64] = {0};
|
||||
snprintf(text,
|
||||
sizeof(text),
|
||||
"Soil:%.0f%%\nADC:%d",
|
||||
soil_data.moisture_percent,
|
||||
soil_data.raw);
|
||||
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
|
||||
}
|
||||
else
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
|
||||
Reference in New Issue
Block a user