feat: 添加电容式土壤湿度传感器支持,更新相关命令和初始化逻辑
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_check.h"
|
||||
#include "console_simple_init.h"
|
||||
#include "console_user_cmds.h"
|
||||
#include "capactive_soil_moisture_sensor_V2.0.h"
|
||||
#include "i2c_master_messager.h"
|
||||
#include "io_device_control.h"
|
||||
#include "wifi-connect.h"
|
||||
@@ -28,6 +30,20 @@ static const char *wifi_status_to_str(wifi_connect_status_t status)
|
||||
}
|
||||
}
|
||||
|
||||
static const char *soil_level_to_str(cap_soil_level_t level)
|
||||
{
|
||||
switch (level) {
|
||||
case CAP_SOIL_LEVEL_DRY:
|
||||
return "dry";
|
||||
case CAP_SOIL_LEVEL_MOIST:
|
||||
return "moist";
|
||||
case CAP_SOIL_LEVEL_WET:
|
||||
return "wet";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
// hello: 最小可用命令,用于验证 console 链路是否正常。
|
||||
static int cmd_hello(int argc, char **argv)
|
||||
{
|
||||
@@ -182,6 +198,48 @@ static int cmd_wifi(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
// soil: 查询土壤湿度,或动态更新空气/水中标定值。
|
||||
static int cmd_soil(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2 || strcmp(argv[1], "read") == 0) {
|
||||
cap_soil_sensor_data_t data = {0};
|
||||
esp_err_t ret = cap_soil_sensor_read(&data);
|
||||
if (ret != ESP_OK) {
|
||||
printf("soil read failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("soil raw=%d, mv=%d, moisture=%.1f%%, level=%s\n",
|
||||
data.raw,
|
||||
data.voltage_mv,
|
||||
data.moisture_percent,
|
||||
soil_level_to_str(data.level));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "cal") == 0) {
|
||||
if (argc < 4) {
|
||||
printf("usage: soil cal <air_raw> <water_raw>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int air_raw = (int)strtol(argv[2], NULL, 10);
|
||||
int water_raw = (int)strtol(argv[3], NULL, 10);
|
||||
esp_err_t ret = cap_soil_sensor_set_calibration(air_raw, water_raw);
|
||||
if (ret != ESP_OK) {
|
||||
printf("soil cal failed: %s\n", esp_err_to_name(ret));
|
||||
printf("hint: air_raw should be greater than water_raw\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("soil calibration updated: air=%d, water=%d\n", air_raw, water_raw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("usage: soil <read|cal <air_raw> <water_raw>>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_err_t console_user_cmds_register(void)
|
||||
{
|
||||
const esp_console_cmd_t hello_cmd = {
|
||||
@@ -222,5 +280,13 @@ esp_err_t console_user_cmds_register(void)
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&wifi_cmd), "console_user_cmds", "register wifi failed");
|
||||
|
||||
const esp_console_cmd_t soil_cmd = {
|
||||
.command = "soil",
|
||||
.help = "土壤湿度读取与标定。用法: soil <read|cal <air_raw> <water_raw>>",
|
||||
.hint = "<read|cal>",
|
||||
.func = cmd_soil,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&soil_cmd), "console_user_cmds", "register soil failed");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user