Compare commits
2 Commits
8bb5cc8836
...
132118c786
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
132118c786 | ||
|
|
61a250048d |
@@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_check.h"
|
||||
#include "console_simple_init.h"
|
||||
#include "console_user_cmds.h"
|
||||
#include "i2c_master_messager.h"
|
||||
@@ -183,32 +184,43 @@ static int cmd_wifi(int argc, char **argv)
|
||||
|
||||
esp_err_t console_user_cmds_register(void)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
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");
|
||||
|
||||
ret = console_cmd_user_register("hello", cmd_hello);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
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");
|
||||
|
||||
ret = console_cmd_user_register("sensor", cmd_sensor);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
const esp_console_cmd_t pump_cmd = {
|
||||
.command = "pump",
|
||||
.help = "控制水泵。用法: pump <on|off>",
|
||||
.hint = "<on|off>",
|
||||
.func = cmd_pump,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&pump_cmd), "console_user_cmds", "register pump failed");
|
||||
|
||||
ret = console_cmd_user_register("pump", cmd_pump);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
const esp_console_cmd_t light_cmd = {
|
||||
.command = "light",
|
||||
.help = "控制补光灯。用法: light <on|off>",
|
||||
.hint = "<on|off>",
|
||||
.func = cmd_light,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&light_cmd), "console_user_cmds", "register light failed");
|
||||
|
||||
ret = console_cmd_user_register("light", cmd_light);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = console_cmd_user_register("wifi", cmd_wifi);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
const esp_console_cmd_t wifi_cmd = {
|
||||
.command = "wifi",
|
||||
.help = "Wi-Fi 状态与控制。用法: wifi <status|start|stop|clear>",
|
||||
.hint = "<status|start|stop|clear>",
|
||||
.func = cmd_wifi,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(esp_console_cmd_register(&wifi_cmd), "console_user_cmds", "register wifi failed");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
30
main/main.c
30
main/main.c
@@ -43,6 +43,33 @@
|
||||
|
||||
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 配网组件,支持长按按键进入配网
|
||||
@@ -86,6 +113,9 @@ void app_main(void)
|
||||
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()); // 可选:自动注册插件命令
|
||||
|
||||
Reference in New Issue
Block a user