From 7459c0fa80cf4a16bb02960f727126664628451f Mon Sep 17 00:00:00 2001 From: wangfq Date: Mon, 6 Jul 2026 13:40:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(vd960DBN):=20iot=5Fmqtt=5Fpublish=5Fsensor?= =?UTF-8?q?=20=E6=A0=88=E6=BA=A2=E5=87=BA=20=E2=80=94=20data=5Fjson[2KB]?= =?UTF-8?q?=20+=20payload[2KB]=20=3D=204KB=20=E8=BF=9C=E8=B6=85=202KB=20?= =?UTF-8?q?=E6=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 每次主循环调 iot_mqtt_publish_sensor 都分配 4KB 栈变量, 直接栈溢出踩烂全局变量(含 g_iot_dev_serial→clientId=空), 最终在 iot_mqtt_send_connect→WCHNET_SocketSend 时 hard fault。 改为 static 彻底消除栈压力。 另加 iot_mqtt_send 调试日志。 --- vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c index 940a406..8c30c67 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c @@ -71,7 +71,9 @@ static int iot_make_topic(char *out, uint16_t out_len, const char *direction, /* 发送 MQTT 报文到 broker */ static int iot_mqtt_send(const uint8_t *buf, uint16_t len) { uint16_t slen = len; + PRINT("IOT: SocketSend sock=%d len=%d\n", g_iot_socket, len); uint8_t ret = WCHNET_SocketSend(g_iot_socket, (uint8_t *)buf, &slen); + PRINT("IOT: SocketSend ret=0x%02X sent=%d\n", ret, slen); return (ret == WCHNET_ERR_SUCCESS) ? 0 : -1; } @@ -390,7 +392,7 @@ void iot_mqtt_publish_sensor(void) { } /* 构建 loop_data JSON */ - char data_json[2048]; + static char data_json[2048]; // static: 避免 2KB 栈溢出 char *p = data_json; int remaining = sizeof(data_json); int written; @@ -433,7 +435,7 @@ void iot_mqtt_publish_sensor(void) { snprintf(p, remaining, "]}"); /* 包装为通用消息格式 */ - char payload[IOT_MQTT_SEND_BUF_LEN]; + static char payload[IOT_MQTT_SEND_BUF_LEN]; // static: 避免 2KB 栈溢出 snprintf(payload, sizeof(payload), "{\"msg_id\":%d,\"cmd\":\"loop_data\",\"ts\":%lu,\"data\":%s}", ++g_iot_msg_id, mstick() / 1000, data_json);