2026-08-08 21:13:56 +08:00
|
|
|
|
/* user_cmd.c - 蓝牙串口调试命令处理
|
|
|
|
|
|
*
|
|
|
|
|
|
* 通过 USART1 接收简单文本命令,用于现场调试:
|
|
|
|
|
|
* RESET/OTA - 复位整机
|
|
|
|
|
|
* RECOVERY - WiFi模块 AT+RESTORE 忘掉已存热点(测试SmartConfig) + 复位
|
|
|
|
|
|
* INFO - 打印 MQTT 配置信息
|
|
|
|
|
|
* STATUS - 打印继电器状态
|
|
|
|
|
|
* R1ON ~ R4OFF - 单路继电器开关
|
|
|
|
|
|
* ALLON / ALLOFF - 全部继电器开关
|
|
|
|
|
|
* HELP - 显示支持命令
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "user_cmd.h"
|
|
|
|
|
|
#include "app_common.h"
|
|
|
|
|
|
#include "relay.h"
|
|
|
|
|
|
#include "24c02.h"
|
|
|
|
|
|
#include "uart.h"
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
|
#include "network.h"
|
2026-08-08 21:50:39 +08:00
|
|
|
|
#include "net_wifi.h"
|
2026-08-08 21:13:56 +08:00
|
|
|
|
#include "esp8266.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* 本地命令字符串不区分大小写比较 */
|
|
|
|
|
|
static int cmd_is(const char *a, const char *b)
|
|
|
|
|
|
{
|
|
|
|
|
|
while (*a && *b) {
|
|
|
|
|
|
char ca = *a, cb = *b;
|
|
|
|
|
|
if (ca >= 'A' && ca <= 'Z') ca += 'a' - 'A';
|
|
|
|
|
|
if (cb >= 'A' && cb <= 'Z') cb += 'a' - 'A';
|
|
|
|
|
|
if (ca != cb) return 0;
|
|
|
|
|
|
a++; b++;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (*a == 0 && *b == 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 通过蓝牙串口直接控制单个继电器,并保存/上报状态 */
|
|
|
|
|
|
static void relay_set(uint8_t ch, uint8_t on)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ch < 1 || ch > 4) return;
|
|
|
|
|
|
|
|
|
|
|
|
relaySet(ch, on);
|
|
|
|
|
|
log_info("> BT: SW%d %s", ch, on ? "ON" : "OFF");
|
|
|
|
|
|
eepromWriteData(102, &MqttInfoStr.Relay_State[0], 7);
|
|
|
|
|
|
relayRequestReport();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void userCmdInit(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* 无需额外初始化 */
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 处理 USART1 蓝牙调试命令 */
|
|
|
|
|
|
void process_usart1_command(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
const char *cmd = USART1_GetCommand();
|
|
|
|
|
|
if (!cmd) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (cmd_is(cmd, "RESET")) {
|
|
|
|
|
|
log_warn("> USART1 RESET command received, rebooting...");
|
2026-08-08 22:51:10 +08:00
|
|
|
|
HAL_Delay(100);
|
|
|
|
|
|
NVIC_SystemReset();
|
2026-08-08 21:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (cmd_is(cmd, "INFO")) {
|
|
|
|
|
|
log_info("> BT INFO: ver=%u, server=%s:%d, client=%s",
|
|
|
|
|
|
MqttInfoStr.Ver, MqttInfoStr.ServerIP,
|
|
|
|
|
|
MqttInfoStr.ServerPort, MqttInfoStr.ClientID);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (cmd_is(cmd, "R1ON")) { relay_set(1, 1); }
|
|
|
|
|
|
else if (cmd_is(cmd, "R1OFF")) { relay_set(1, 0); }
|
|
|
|
|
|
else if (cmd_is(cmd, "R2ON")) { relay_set(2, 1); }
|
|
|
|
|
|
else if (cmd_is(cmd, "R2OFF")) { relay_set(2, 0); }
|
|
|
|
|
|
else if (cmd_is(cmd, "R3ON")) { relay_set(3, 1); }
|
|
|
|
|
|
else if (cmd_is(cmd, "R3OFF")) { relay_set(3, 0); }
|
|
|
|
|
|
else if (cmd_is(cmd, "R4ON")) { relay_set(4, 1); }
|
|
|
|
|
|
else if (cmd_is(cmd, "R4OFF")) { relay_set(4, 0); }
|
|
|
|
|
|
else if (cmd_is(cmd, "ALLON")) {
|
|
|
|
|
|
relay_set(1, 1); relay_set(2, 1); relay_set(3, 1); relay_set(4, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (cmd_is(cmd, "ALLOFF")) {
|
|
|
|
|
|
relay_set(1, 0); relay_set(2, 0); relay_set(3, 0); relay_set(4, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (cmd_is(cmd, "STATUS")) {
|
|
|
|
|
|
log_info("> BT STATUS: SW1=%s, SW2=%s, SW3=%s, SW4=%s",
|
|
|
|
|
|
MqttInfoStr.Relay_State[1] ? "ON" : "OFF",
|
|
|
|
|
|
MqttInfoStr.Relay_State[2] ? "ON" : "OFF",
|
|
|
|
|
|
MqttInfoStr.Relay_State[3] ? "ON" : "OFF",
|
|
|
|
|
|
MqttInfoStr.Relay_State[4] ? "ON" : "OFF");
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (cmd_is(cmd, "OTA")) {
|
|
|
|
|
|
log_info("> BT OTA: reboot to bootloader for OTA check");
|
2026-08-08 22:51:10 +08:00
|
|
|
|
HAL_Delay(100);
|
|
|
|
|
|
NVIC_SystemReset();
|
2026-08-08 21:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (cmd_is(cmd, "RECOVERY")) {
|
|
|
|
|
|
/* WiFi 模块 AT+RESTORE 恢复出厂(忘掉已保存的热点),用于测试 SmartConfig;
|
|
|
|
|
|
* 仅 WiFi 通道激活时才操作模块(UART4 与 4G 共享接收缓冲,
|
|
|
|
|
|
* 4G 模式下动 UART4 会干扰 4G 数据),随后整机复位走完整重连流程 */
|
|
|
|
|
|
if (NET_GetActiveBackend() == NET_BACKEND_WIFI) {
|
2026-08-08 21:50:39 +08:00
|
|
|
|
log_warn("> BT RECOVERY: WiFi restore scheduled"); g_wifi_restore_req = 1;
|
2026-08-08 21:13:56 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
log_warn("> BT RECOVERY: 4G mode, WiFi module untouched, reboot only");
|
2026-08-08 21:50:39 +08:00
|
|
|
|
HAL_Delay(100);
|
|
|
|
|
|
NVIC_SystemReset();
|
2026-08-08 21:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (cmd_is(cmd, "HELP")) {
|
|
|
|
|
|
log_info("CMD: RESET, INFO, STATUS, R1ON/R1OFF~R4ON/R4OFF, ALLON, ALLOFF, OTA, RECOVERY, HELP");
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
log_warn("> BT unknown cmd: %s", cmd);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|