#include #include #include "esp_check.h" #include "console_simple_init.h" #include "console_user_cmds.h" #include "i2c_master_messager.h" #include "io_device_control.h" #include "wifi-connect.h" static const char *wifi_status_to_str(wifi_connect_status_t status) { switch (status) { case WIFI_CONNECT_STATUS_IDLE: return "idle"; case WIFI_CONNECT_STATUS_PROVISIONING: return "provisioning"; case WIFI_CONNECT_STATUS_CONNECTING: return "connecting"; case WIFI_CONNECT_STATUS_CONNECTED: return "connected"; case WIFI_CONNECT_STATUS_FAILED: return "failed"; case WIFI_CONNECT_STATUS_TIMEOUT: return "timeout"; default: return "unknown"; } } // hello: 最小可用命令,用于验证 console 链路是否正常。 static int cmd_hello(int argc, char **argv) { (void)argc; (void)argv; printf("hello from BotanicalBuddy\n"); return 0; } // sensor: 读取一次传感器缓存数据并打印,便于快速排查现场状态。 static int cmd_sensor(int argc, char **argv) { (void)argc; (void)argv; i2c_master_messager_data_t data = {0}; esp_err_t ret = i2c_master_messager_get_data(&data); if (ret != ESP_OK) { printf("sensor read failed: %s\n", esp_err_to_name(ret)); return 1; } if (data.bh1750.valid) { printf("BH1750: lux=%.1f, ts=%lld ms\n", data.bh1750.lux, (long long)data.bh1750.last_update_ms); } else { printf("BH1750: invalid, err=%s\n", esp_err_to_name(data.bh1750.last_error)); } if (data.aht30.valid) { printf("AHT30: temp=%.1f C, hum=%.1f %%, ts=%lld ms\n", data.aht30.temperature_c, data.aht30.humidity_rh, (long long)data.aht30.last_update_ms); } else { printf("AHT30: invalid, err=%s\n", esp_err_to_name(data.aht30.last_error)); } return 0; } static bool parse_on_off_arg(const char *arg, bool *on) { if (strcmp(arg, "on") == 0 || strcmp(arg, "1") == 0) { *on = true; return true; } if (strcmp(arg, "off") == 0 || strcmp(arg, "0") == 0) { *on = false; return true; } return false; } // pump: 控制水泵开关,参数支持 on/off 或 1/0。 static int cmd_pump(int argc, char **argv) { if (argc < 2) { printf("usage: pump \n"); return 1; } bool on = false; if (!parse_on_off_arg(argv[1], &on)) { printf("invalid arg: %s\n", argv[1]); printf("usage: pump \n"); return 1; } esp_err_t ret = io_device_control_set_pump(on); if (ret != ESP_OK) { printf("set pump failed: %s\n", esp_err_to_name(ret)); return 1; } printf("pump: %s\n", on ? "on" : "off"); return 0; } // light: 控制补光灯开关,参数支持 on/off 或 1/0。 static int cmd_light(int argc, char **argv) { if (argc < 2) { printf("usage: light \n"); return 1; } bool on = false; if (!parse_on_off_arg(argv[1], &on)) { printf("invalid arg: %s\n", argv[1]); printf("usage: light \n"); return 1; } esp_err_t ret = io_device_control_set_light(on); if (ret != ESP_OK) { printf("set light failed: %s\n", esp_err_to_name(ret)); return 1; } printf("light: %s\n", on ? "on" : "off"); return 0; } // wifi: 查询或控制配网状态,支持 status/start/stop/clear 子命令。 static int cmd_wifi(int argc, char **argv) { if (argc < 2 || strcmp(argv[1], "status") == 0) { wifi_connect_config_t cfg = {0}; esp_err_t cfg_ret = wifi_connect_get_config(&cfg); printf("wifi status: %s\n", wifi_status_to_str(wifi_connect_get_status())); if (cfg_ret == ESP_OK && cfg.has_config) { printf("saved ssid: %s\n", cfg.ssid); } else { printf("saved config: none\n"); } return 0; } if (strcmp(argv[1], "start") == 0) { esp_err_t ret = wifi_connect_start(); if (ret != ESP_OK) { printf("wifi start failed: %s\n", esp_err_to_name(ret)); return 1; } printf("wifi start requested\n"); return 0; } if (strcmp(argv[1], "stop") == 0) { esp_err_t ret = wifi_connect_stop(); if (ret != ESP_OK) { printf("wifi stop failed: %s\n", esp_err_to_name(ret)); return 1; } printf("wifi stop requested\n"); return 0; } if (strcmp(argv[1], "clear") == 0) { esp_err_t ret = wifi_connect_clear_config(); if (ret != ESP_OK) { printf("wifi clear failed: %s\n", esp_err_to_name(ret)); return 1; } printf("wifi config cleared\n"); return 0; } printf("usage: wifi \n"); return 1; } esp_err_t console_user_cmds_register(void) { const esp_console_cmd_t hello_cmd = { .command = "hello", .help = "打印欢迎信息。用法: hello", .func = cmd_hello, }; ESP_RETURN_ON_ERROR(esp_console_cmd_register(&hello_cmd), "console_user_cmds", "register hello failed"); const esp_console_cmd_t sensor_cmd = { .command = "sensor", .help = "打印当前传感器缓存数据。用法: sensor", .func = cmd_sensor, }; ESP_RETURN_ON_ERROR(esp_console_cmd_register(&sensor_cmd), "console_user_cmds", "register sensor failed"); const esp_console_cmd_t pump_cmd = { .command = "pump", .help = "控制水泵。用法: pump ", .hint = "", .func = cmd_pump, }; ESP_RETURN_ON_ERROR(esp_console_cmd_register(&pump_cmd), "console_user_cmds", "register pump failed"); const esp_console_cmd_t light_cmd = { .command = "light", .help = "控制补光灯。用法: light ", .hint = "", .func = cmd_light, }; ESP_RETURN_ON_ERROR(esp_console_cmd_register(&light_cmd), "console_user_cmds", "register light failed"); const esp_console_cmd_t wifi_cmd = { .command = "wifi", .help = "Wi-Fi 状态与控制。用法: wifi ", .hint = "", .func = cmd_wifi, }; ESP_RETURN_ON_ERROR(esp_console_cmd_register(&wifi_cmd), "console_user_cmds", "register wifi failed"); return ESP_OK; }