{"id":76731,"date":"2026-02-23T07:52:07","date_gmt":"2026-02-22T23:52:07","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/76731.html"},"modified":"2026-02-23T07:52:07","modified_gmt":"2026-02-22T23:52:07","slug":"%e4%bb%8e%e9%9b%b6%e6%9e%84%e5%bb%ba%e5%b5%8c%e5%85%a5%e5%bc%8fmqtt%e5%ae%a2%e6%88%b7%e7%ab%af%ef%bc%9astm32%e4%b8%8e%e5%8d%8e%e4%b8%ba%e4%ba%91iotda%e7%9a%84%e5%8d%8f%e8%ae%ae%e5%b1%82%e6%b7%b1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/76731.html","title":{"rendered":"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\uff1aSTM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd"},"content":{"rendered":"<h2>\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef&#xff1a;STM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd<\/h2>\n<p>\u5728\u7269\u8054\u7f51\u8bbe\u5907\u5f00\u53d1\u4e2d&#xff0c;\u76f4\u63a5\u4f7f\u7528MQTT\u534f\u8bae\u4e0e\u4e91\u5e73\u53f0\u8fdb\u884c\u5e95\u5c42\u4ea4\u4e92\u662f\u8bb8\u591a\u5d4c\u5165\u5f0f\u5de5\u7a0b\u5e08\u5fc5\u987b\u638c\u63e1\u7684\u6280\u80fd\u3002\u4e0d\u540c\u4e8e\u4f9d\u8d56\u9ad8\u7ea7SDK\u7684\u5feb\u901f\u63a5\u5165\u65b9\u5f0f&#xff0c;\u624b\u52a8\u5b9e\u73b0MQTT\u534f\u8bae\u6808\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u6df1\u5165\u7406\u89e3\u534f\u8bae\u7ec6\u8282&#xff0c;\u66f4\u597d\u5730\u63a7\u5236\u8bbe\u5907\u884c\u4e3a&#xff0c;\u5e76\u5728\u8d44\u6e90\u53d7\u9650\u7684\u73af\u5883\u4e2d\u5b9e\u73b0\u7cbe\u7ec6\u5316\u4f18\u5316\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u62a5\u6587\u7ed3\u6784\u89e3\u6790\u5f00\u59cb&#xff0c;\u9010\u6b65\u6784\u5efa\u4e00\u4e2a\u7a33\u5b9a\u53ef\u9760\u7684STM32MQTT\u5ba2\u6237\u7aef&#xff0c;\u5b9e\u73b0\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u9ad8\u6548\u901a\u4fe1\u3002<\/p>\n<h3>1. MQTT\u534f\u8bae\u6838\u5fc3\u673a\u5236\u4e0e\u534e\u4e3a\u4e91\u9002\u914d\u539f\u7406<\/h3>\n<p>MQTT\u534f\u8bae\u4f5c\u4e3a\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u53d1\u5e03\/\u8ba2\u9605\u6d88\u606f\u4f20\u8f93\u534f\u8bae&#xff0c;\u5176\u8bbe\u8ba1\u54f2\u5b66\u5b8c\u7f8e\u5951\u5408\u4e86\u7269\u8054\u7f51\u8bbe\u5907\u5bf9\u4f4e\u5e26\u5bbd\u3001\u9ad8\u5ef6\u8fdf\u6216\u4e0d\u53ef\u9760\u7f51\u7edc\u73af\u5883\u7684\u9700\u6c42\u3002\u534f\u8bae\u57fa\u4e8eTCP\/IP\u6784\u5efa&#xff0c;\u901a\u8fc7\u56fa\u5b9a\u5934\u90e8&#043;\u53ef\u53d8\u5934\u90e8&#043;\u6d88\u606f\u4f53\u7684\u7ed3\u6784\u5b9e\u73b0\u9ad8\u6548\u901a\u4fe1\u3002<\/p>\n<p>\u5728\u534e\u4e3a\u4e91IoTDA\u5e73\u53f0\u4e2d&#xff0c;\u8bbe\u5907\u8fde\u63a5\u9700\u8981\u5b8c\u6210\u4e09\u4e2a\u5173\u952e\u8ba4\u8bc1\u6b65\u9aa4&#xff1a;\u5ba2\u6237\u7aef\u6807\u8bc6\u7b26\u9a8c\u8bc1\u3001\u7528\u6237\u540d\u5bc6\u7801\u9274\u6743\u548cSSL\/TLS\u52a0\u5bc6\u901a\u9053\u5efa\u7acb&#xff08;\u672c\u6587\u805a\u7126\u975e\u52a0\u5bc6\u8fde\u63a5\u76841883\u7aef\u53e3\u5b9e\u73b0&#xff09;\u3002\u6bcf\u4e2a\u8fde\u63a5\u8bf7\u6c42\u90fd\u5fc5\u987b\u5305\u542b\u4ee5\u4e0b\u8981\u7d20&#xff1a;<\/p>\n<table>\n<tr>\u53c2\u6570\u7c7b\u578b\u5fc5\u9700\u5b57\u6bb5\u793a\u4f8b\u503c\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u5ba2\u6237\u7aefID<\/td>\n<td>clientId<\/td>\n<td>device_001<\/td>\n<td>\u5e73\u53f0\u6ce8\u518c\u7684\u8bbe\u5907\u552f\u4e00\u6807\u8bc6<\/td>\n<\/tr>\n<tr>\n<td>\u7528\u6237\u540d<\/td>\n<td>username<\/td>\n<td>user_001<\/td>\n<td>\u901a\u5e38\u4e0e\u5ba2\u6237\u7aefID\u76f8\u540c<\/td>\n<\/tr>\n<tr>\n<td>\u5bc6\u7801<\/td>\n<td>password<\/td>\n<td>\u52a0\u5bc6\u4e32<\/td>\n<td>\u4f7f\u7528\u5bc6\u94a5\u751f\u6210\u7684\u52a0\u5bc6\u7b7e\u540d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u63d0\u793a&#xff1a;\u534e\u4e3a\u4e91\u5bc6\u7801\u751f\u6210\u9700\u4f7f\u7528\u8bbe\u5907\u5bc6\u94a5\u901a\u8fc7HMAC-SHA256\u7b97\u6cd5\u8ba1\u7b97&#xff0c;\u5177\u4f53\u516c\u5f0f\u4e3a&#xff1a;password &#061; HMAC-SHA256(deviceSecret, timestamp)<\/p>\n<p>\u8fde\u63a5\u4fdd\u6301\u673a\u5236\u901a\u8fc7Keep Alive\u53c2\u6570\u5b9e\u73b0&#xff0c;\u8bbe\u5907\u9700\u8981\u5728\u8be5\u65f6\u95f4\u95f4\u9694\u5185\u53d1\u9001\u5fc3\u8df3\u5305&#xff08;PINGREQ&#xff09;\u7ef4\u6301\u8fde\u63a5\u3002\u82e5\u670d\u52a1\u5668\u57281.5\u500dKeep Alive\u65f6\u95f4\u5185\u672a\u6536\u5230\u4efb\u4f55\u6570\u636e\u5305&#xff0c;\u5c06\u4e3b\u52a8\u65ad\u5f00\u8fde\u63a5\u3002<\/p>\n<h3>2. STM32\u786c\u4ef6\u5e73\u53f0\u4e0e4G\u6a21\u5757\u96c6\u6210\u7b56\u7565<\/h3>\n<p>STM32\u7cfb\u5217MCU\u5728\u7269\u8054\u7f51\u8bbe\u5907\u4e2d\u5e7f\u6cdb\u5e94\u7528&#xff0c;\u5176\u4e30\u5bcc\u7684\u5916\u8bbe\u8d44\u6e90\u548c\u4f4e\u529f\u8017\u7279\u6027\u4f7f\u5176\u6210\u4e3a\u8fb9\u7f18\u8ba1\u7b97\u7684\u7406\u60f3\u9009\u62e9\u3002\u6211\u4eec\u9009\u62e9STM32F407\u7cfb\u5217\u4f5c\u4e3a\u4e3b\u63a7\u82af\u7247&#xff0c;\u5176\u5185\u7f6e\u7684256KB RAM\u548c1MB Flash\u4e3a\u534f\u8bae\u6808\u8fd0\u884c\u63d0\u4f9b\u4e86\u5145\u8db3\u7a7a\u95f4\u3002<\/p>\n<p>\u6709\u4ebaWH-LTE-7S1 4G\u6a21\u5757\u901a\u8fc7\u4e32\u53e3\u4e0eSTM32\u901a\u4fe1&#xff0c;\u652f\u6301\u900f\u4f20\u6a21\u5f0f\u548cAT\u6307\u4ee4\u6a21\u5f0f\u3002\u4e3a\u5b9e\u73b0\u53ef\u9760\u6570\u636e\u4f20\u8f93&#xff0c;\u6211\u4eec\u91c7\u7528\u4ee5\u4e0b\u914d\u7f6e\u7b56\u7565&#xff1a;<\/p>\n<p>\/\/ \u4e32\u53e3DMA\u914d\u7f6e\u7ed3\u6784\u4f53\u793a\u4f8b<br \/>\nUART_HandleTypeDef huart3;<br \/>\nDMA_HandleTypeDef hdma_usart3_rx;<br \/>\nDMA_HandleTypeDef hdma_usart3_tx;<\/p>\n<p>void MX_USART3_UART_Init(void)<br \/>\n{<br \/>\n  huart3.Instance &#061; USART3;<br \/>\n  huart3.Init.BaudRate &#061; 115200;<br \/>\n  huart3.Init.WordLength &#061; UART_WORDLENGTH_8B;<br \/>\n  huart3.Init.StopBits &#061; UART_STOPBITS_1;<br \/>\n  huart3.Init.Parity &#061; UART_PARITY_NONE;<br \/>\n  huart3.Init.Mode &#061; UART_MODE_TX_RX;<br \/>\n  huart3.Init.HwFlowCtl &#061; UART_HWCONTROL_NONE;<br \/>\n  huart3.Init.OverSampling &#061; UART_OVERSAMPLING_16;<br \/>\n  HAL_UART_Init(&amp;huart3);<br \/>\n}<\/p>\n<p>\u6a21\u5757\u521d\u59cb\u5316\u6d41\u7a0b\u5305\u542b\u786c\u4ef6\u590d\u4f4d\u548cAT\u6307\u4ee4\u914d\u7f6e\u4e24\u4e2a\u9636\u6bb5\u3002\u786c\u4ef6\u590d\u4f4d\u901a\u8fc7\u7279\u5b9a\u5f15\u811a\u7535\u5e73\u53d8\u5316\u89e6\u53d1&#xff0c;\u800c\u5de5\u4f5c\u6a21\u5f0f\u5219\u901a\u8fc7AT\u6307\u4ee4\u8bbe\u7f6e&#xff1a;<\/p>\n<li>\u53d1\u9001AT&#043;Z\u6307\u4ee4\u8fdb\u884c\u8f6f\u590d\u4f4d&#xff0c;\u7b49\u5f85\u6a21\u5757\u8fd4\u56de&#034;OK&#034;<\/li>\n<li>\u8bbe\u7f6e\u900f\u4f20\u6a21\u5f0f&#xff1a;AT&#043;WMODE&#061;NET&#xff08;\u7f51\u7edc\u900f\u4f20\u6a21\u5f0f&#xff09;<\/li>\n<li>\u914d\u7f6e\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3&#xff1a;AT&#043;SSET&#061;&#034;iot-mqtts.cn-north-4.myhuaweicloud.com&#034;,&#034;1883&#034;<\/li>\n<li>\u4fdd\u5b58\u914d\u7f6e\u5e76\u91cd\u542f&#xff1a;AT&#043;Z<\/li>\n<p>\u6ce8\u610f&#xff1a;\u6bcf\u6b21\u914d\u7f6e\u4fee\u6539\u540e\u5fc5\u987b\u6267\u884c\u4fdd\u5b58\u64cd\u4f5c&#xff0c;\u5426\u5219\u91cd\u542f\u540e\u914d\u7f6e\u5c06\u4e22\u5931\u3002\u5efa\u8bae\u5728\u8bbe\u5907\u9996\u6b21\u542f\u52a8\u65f6\u68c0\u67e5NVM\u4e2d\u7684\u914d\u7f6e\u6807\u8bb0\u4f4d&#xff0c;\u907f\u514d\u91cd\u590d\u914d\u7f6e\u3002<\/p>\n<h3>3. MQTT\u534f\u8bae\u6808\u5b9e\u73b0\u4e0e\u62a5\u6587\u5e8f\u5217\u5316<\/h3>\n<p>\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\u5b9e\u73b0\u7684\u6838\u5fc3\u5728\u4e8e\u534f\u8bae\u62a5\u6587\u7684\u6b63\u786e\u7ec4\u88c5\u548c\u89e3\u6790\u3002Paho MQTT\u5d4c\u5165\u5f0fC\u5e93\u63d0\u4f9b\u4e86\u4f18\u79c0\u7684\u5e8f\u5217\u5316\/\u53cd\u5e8f\u5217\u5316\u57fa\u7840&#xff0c;\u6211\u4eec\u9700\u8981\u5728\u6b64\u57fa\u7840\u4e0a\u5b9e\u73b0\u4e0e\u786c\u4ef6\u5e73\u53f0\u7684\u9002\u914d\u3002<\/p>\n<p>\u8fde\u63a5\u62a5\u6587\u7684\u7ec4\u88c5\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65&#xff0c;\u5b83\u51b3\u5b9a\u4e86\u8bbe\u5907\u80fd\u5426\u6210\u529f\u63a5\u5165\u4e91\u5e73\u53f0&#xff1a;<\/p>\n<p>#define CLIENT_ID    &#034;device_001&#034;<br \/>\n#define USER_NAME    &#034;user_001&#034;<br \/>\n#define PASSWORD     &#034;\u52a0\u5bc6\u5bc6\u7801&#034;<\/p>\n<p>int serialize_connect_packet(uint8_t *buf, size_t buf_len)<br \/>\n{<br \/>\n    MQTTPacket_connectData data &#061; MQTTPacket_connectData_initializer;<\/p>\n<p>    data.keepAliveInterval &#061; 120;<br \/>\n    data.cleansession &#061; 1;<br \/>\n    data.clientID.cstring &#061; CLIENT_ID;<br \/>\n    data.username.cstring &#061; USER_NAME;<br \/>\n    data.password.cstring &#061; PASSWORD;<\/p>\n<p>    return MQTTSerialize_connect(buf, buf_len, &amp;data);<br \/>\n}<\/p>\n<p>\u62a5\u6587\u53d1\u9001\u51fd\u6570\u9700\u8981\u5904\u7406\u4e32\u53e3\u4f20\u8f93\u7684\u5b9e\u9645\u60c5\u51b5&#xff0c;\u5305\u62ec\u8d85\u65f6\u91cd\u4f20\u548c\u9519\u8bef\u5904\u7406&#xff1a;<\/p>\n<p>void mqtt_packet_send(uint8_t *packet, int length)<br \/>\n{<br \/>\n    HAL_StatusTypeDef status;<br \/>\n    uint32_t timeout &#061; 1000; \/\/ 1\u79d2\u8d85\u65f6<\/p>\n<p>    status &#061; HAL_UART_Transmit(&amp;huart3, packet, length, timeout);<\/p>\n<p>    if (status !&#061; HAL_OK) {<br \/>\n        \/\/ \u8bb0\u5f55\u9519\u8bef\u5e76\u89e6\u53d1\u91cd\u4f20\u673a\u5236<br \/>\n        log_error(&#034;UART transmit failed: %d&#034;, status);<br \/>\n        mqtt_retransmit_packet(packet, length);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u5bf9\u4e8eQoS 1\u7ea7\u522b\u7684\u6d88\u606f\u4f20\u8f93&#xff0c;\u9700\u8981\u5b9e\u73b0\u6d88\u606fID\u7ba1\u7406\u548c\u786e\u8ba4\u673a\u5236&#xff1a;<\/p>\n<p>static uint16_t packet_id &#061; 0;<\/p>\n<p>uint16_t generate_packet_id(void)<br \/>\n{<br \/>\n    return &#043;&#043;packet_id;<br \/>\n}<\/p>\n<p>void handle_puback(uint8_t *response)<br \/>\n{<br \/>\n    uint16_t recv_id &#061; (response[2] &lt;&lt; 8) | response[3];<\/p>\n<p>    \/\/ \u5728\u6d88\u606f\u5217\u8868\u4e2d\u67e5\u627e\u5bf9\u5e94ID\u5e76\u79fb\u9664<br \/>\n    remove_from_ack_list(recv_id);<br \/>\n}<\/p>\n<h3>4. \u7f51\u7edc\u5f02\u5e38\u5904\u7406\u4e0e\u8fde\u63a5\u4fdd\u6301\u673a\u5236<\/h3>\n<p>\u5728\u79fb\u52a8\u7f51\u7edc\u73af\u5883\u4e2d&#xff0c;\u8fde\u63a5\u4e0d\u7a33\u5b9a\u662f\u5e38\u6001\u800c\u975e\u4f8b\u5916\u3002\u4e00\u4e2a\u5065\u58ee\u7684MQTT\u5ba2\u6237\u7aef\u5fc5\u987b\u80fd\u591f\u5904\u7406\u5404\u79cd\u7f51\u7edc\u5f02\u5e38\u60c5\u51b5&#xff0c;\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\u77ac\u65f6\u7f51\u7edc\u4e2d\u65ad&#xff1a;\u901a\u8fc7\u5fc3\u8df3\u5305\u8d85\u65f6\u68c0\u6d4b\u5feb\u901f\u53d1\u73b0<\/li>\n<li>\u670d\u52a1\u5668\u4e3b\u52a8\u65ad\u5f00&#xff1a;\u89e3\u6790DISCONNECT\u62a5\u6587\u5e76\u6267\u884c\u91cd\u8fde<\/li>\n<li>\u6a21\u5757\u65e0\u54cd\u5e94&#xff1a;\u786c\u4ef6\u770b\u95e8\u72d7\u548c\u8f6f\u4ef6\u8d85\u65f6\u53cc\u91cd\u4fdd\u969c<\/li>\n<li>\u4fe1\u53f7\u5f3a\u5ea6\u4e0d\u8db3&#xff1a;\u901a\u8fc7AT\u6307\u4ee4\u67e5\u8be2\u4fe1\u53f7\u8d28\u91cf\u5e76\u51b3\u7b56<\/li>\n<\/ul>\n<p>\u5fc3\u8df3\u673a\u5236\u662f\u5b9e\u73b0\u8fde\u63a5\u4fdd\u6301\u7684\u6838\u5fc3&#xff0c;\u9700\u8981\u5728Keep Alive\u65f6\u95f4\u95f4\u9694\u5185\u53d1\u9001PINGREQ\u62a5\u6587&#xff1a;<\/p>\n<p>void mqtt_keepalive_task(void *argument)<br \/>\n{<br \/>\n    uint32_t last_ping_time &#061; 0;<br \/>\n    const uint32_t keepalive_interval &#061; 120000; \/\/ 2\u5206\u949f<\/p>\n<p>    while (1) {<br \/>\n        uint32_t current_time &#061; osKernelGetTickCount();<\/p>\n<p>        if (current_time &#8211; last_ping_time &gt;&#061; keepalive_interval) {<br \/>\n            if (mqtt_state &#061;&#061; MQTT_CONNECTED) {<br \/>\n                send_pingreq();<br \/>\n                last_ping_time &#061; current_time;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        osDelay(1000); \/\/ \u6bcf\u79d2\u68c0\u67e5\u4e00\u6b21<br \/>\n    }<br \/>\n}<\/p>\n<p>\u5f02\u5e38\u5904\u7406\u72b6\u6001\u673a\u662f\u5b9e\u73b0\u53ef\u9760\u901a\u4fe1\u7684\u5173\u952e\u8bbe\u8ba1&#xff0c;\u5305\u542b\u4ee5\u4e0b\u72b6\u6001\u8f6c\u6362&#xff1a;<\/p>\n<p>\u521d\u59cb\u72b6\u6001 \u2192 \u6a21\u5757\u521d\u59cb\u5316 \u2192 TCP\u8fde\u63a5 \u2192 MQTT\u8fde\u63a5 \u2192 \u8ba2\u9605\u4e3b\u9898 \u2192 \u6570\u636e\u901a\u4fe1<br \/>\n    \u2191         |            |           |           |           |<br \/>\n    |         \u2193            \u2193           \u2193           \u2193           \u2193<br \/>\n    \u2514\u2500\u2500 \u5f02\u5e38\u68c0\u6d4b \u2190\u2500\u2500 \u8d85\u65f6\u5904\u7406 \u2190\u2500\u2500 \u9519\u8bef\u5904\u7406 \u2190\u2500\u2500 \u91cd\u8fde\u673a\u5236 \u2190\u2500\u2500 \u5fc3\u8df3\u68c0\u6d4b<\/p>\n<p>\u5b9a\u65f6\u5668\u5728\u5f02\u5e38\u5904\u7406\u4e2d\u626e\u6f14\u91cd\u8981\u89d2\u8272&#xff0c;\u6211\u4eec\u4f7f\u7528STM32\u7684\u786c\u4ef6\u5b9a\u65f6\u5668\u5b9e\u73b0\u591a\u7ec4\u8d85\u65f6\u68c0\u6d4b&#xff1a;<\/p>\n<p>typedef struct {<br \/>\n    uint32_t connect_timeout;<br \/>\n    uint32_t publish_timeout;<br \/>\n    uint32_t ping_timeout;<br \/>\n    uint32_t general_timeout;<br \/>\n} mqtt_timeout_t;<\/p>\n<p>void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)<br \/>\n{<br \/>\n    if (htim-&gt;Instance &#061;&#061; TIM1) {<br \/>\n        \/\/ \u6bcf\u79d2\u89e6\u53d1\u4e00\u6b21\u7684\u8d85\u65f6\u5904\u7406<br \/>\n        check_connection_timeout();<br \/>\n        check_publish_timeout();<br \/>\n        check_ping_timeout();<br \/>\n    }<br \/>\n}<\/p>\n<h3>5. \u5185\u5b58\u4f18\u5316\u4e0e\u6027\u80fd\u8c03\u4f18\u7b56\u7565<\/h3>\n<p>\u5728\u8d44\u6e90\u53d7\u9650\u7684\u5d4c\u5165\u5f0f\u73af\u5883\u4e2d&#xff0c;\u5185\u5b58\u7ba1\u7406\u76f4\u63a5\u5f71\u54cd\u7cfb\u7edf\u7a33\u5b9a\u6027\u548c\u6027\u80fd\u3002\u6211\u4eec\u91c7\u7528\u4ee5\u4e0b\u4f18\u5316\u7b56\u7565&#xff1a;<\/p>\n<p>\u9759\u6001\u5185\u5b58\u5206\u914d\u4f18\u5148&#xff1a;\u907f\u514d\u52a8\u6001\u5185\u5b58\u5206\u914d\u5e26\u6765\u7684\u788e\u7247\u5316\u95ee\u9898&#xff0c;\u4e3a\u6bcf\u4e2aMQTT\u62a5\u6587\u9884\u5206\u914d\u56fa\u5b9a\u5927\u5c0f\u7684\u7f13\u51b2\u533a&#xff1a;<\/p>\n<p>#pragma pack(push, 1)<br \/>\ntypedef struct {<br \/>\n    uint8_t fixed_header[2];<br \/>\n    uint16_t remaining_length;<br \/>\n    uint8_t variable_header[64];<br \/>\n    uint8_t payload[256];<br \/>\n} mqtt_packet_buffer_t;<br \/>\n#pragma pack(pop)<\/p>\n<p>static mqtt_packet_buffer_t tx_buffer;<br \/>\nstatic mqtt_packet_buffer_t rx_buffer;<\/p>\n<p>\u5185\u5b58\u6c60\u7ba1\u7406&#xff1a;\u4e3a\u4e0d\u540c\u957f\u5ea6\u7684\u62a5\u6587\u521b\u5efa\u5185\u5b58\u6c60&#xff0c;\u51cf\u5c11\u5185\u5b58\u6d6a\u8d39&#xff1a;<\/p>\n<table>\n<tr>\u5185\u5b58\u6c60\u7c7b\u578b\u7f13\u51b2\u533a\u5927\u5c0f\u6570\u91cf\u7528\u9014<\/tr>\n<tbody>\n<tr>\n<td>\u5c0f\u7f13\u51b2\u533a<\/td>\n<td>128\u5b57\u8282<\/td>\n<td>4<\/td>\n<td>PINGREQ\u3001PUBACK\u7b49\u77ed\u62a5\u6587<\/td>\n<\/tr>\n<tr>\n<td>\u4e2d\u7f13\u51b2\u533a<\/td>\n<td>512\u5b57\u8282<\/td>\n<td>2<\/td>\n<td>PUBLISH\u62a5\u6587&#xff08;QoS 0&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5927\u7f13\u51b2\u533a<\/td>\n<td>1024\u5b57\u8282<\/td>\n<td>1<\/td>\n<td>CONNECT\u62a5\u6587\u548c\u957f\u6d88\u606f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u901a\u4fe1\u6027\u80fd\u4f18\u5316&#xff1a;\u901a\u8fc7DMA\u4f20\u8f93\u51cf\u5c11CPU\u5360\u7528&#xff0c;\u91c7\u7528\u53cc\u7f13\u51b2\u673a\u5236\u5b9e\u73b0\u4f20\u8f93\u4e0e\u7ec4\u5305\u5e76\u884c&#xff1a;<\/p>\n<p>void uart_dma_receive_init(void)<br \/>\n{<br \/>\n    \/\/ \u914d\u7f6e\u53cc\u7f13\u51b2DMA\u63a5\u6536<br \/>\n    HAL_UARTEx_ReceiveToIdle_DMA(&amp;huart3, rx_buf[0], RX_BUF_SIZE);<br \/>\n    __HAL_DMA_DISABLE_IT(&amp;hdma_usart3_rx, DMA_IT_HT);<br \/>\n    __HAL_DMA_ENABLE_IT(&amp;hdma_usart3_rx, DMA_IT_TC);<br \/>\n}<\/p>\n<p>\u529f\u8017\u63a7\u5236&#xff1a;\u6839\u636e\u7f51\u7edc\u72b6\u6001\u52a8\u6001\u8c03\u6574\u6a21\u5757\u5de5\u4f5c\u6a21\u5f0f&#xff0c;\u5728\u7a7a\u95f2\u65f6\u6bb5\u8fdb\u5165\u4f4e\u529f\u8017\u72b6\u6001&#xff1a;<\/p>\n<li>\u6570\u636e\u53d1\u9001\u5b8c\u6210\u540e&#xff0c;\u5ef6\u8fdf10\u79d2\u65e0\u6d3b\u52a8\u5219\u53d1\u9001\u4f11\u7720\u6307\u4ee4<\/li>\n<li>\u5524\u9192\u901a\u8fc7\u6a21\u5757\u7684DR\u5f15\u811a\u6216\u5916\u90e8\u4e2d\u65ad\u89e6\u53d1<\/li>\n<li>\u5fc3\u8df3\u5305\u53d1\u9001\u95f4\u9694\u53ef\u6839\u636e\u7f51\u7edc\u8d28\u91cf\u52a8\u6001\u8c03\u6574&#xff08;60-120\u79d2&#xff09;<\/li>\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d&#xff0c;\u8fd9\u4e9b\u4f18\u5316\u63aa\u65bd\u80fd\u591f\u5c06\u5185\u5b58\u4f7f\u7528\u91cf\u964d\u4f4e40%\u4ee5\u4e0a&#xff0c;CPU\u5360\u7528\u7387\u51cf\u5c1125%&#xff0c;\u663e\u8457\u63d0\u5347\u7cfb\u7edf\u7eed\u822a\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef&#xff1a;STM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd<br \/>\n\u5728\u7269\u8054\u7f51\u8bbe\u5907\u5f00\u53d1\u4e2d&#xff0c;\u76f4\u63a5\u4f7f\u7528MQTT\u534f\u8bae\u4e0e\u4e91\u5e73\u53f0\u8fdb\u884c\u5e95\u5c42\u4ea4\u4e92\u662f\u8bb8\u591a\u5d4c\u5165\u5f0f\u5de5\u7a0b\u5e08\u5fc5\u987b\u638c\u63e1\u7684\u6280\u80fd\u3002\u4e0d\u540c\u4e8e\u4f9d\u8d56\u9ad8\u7ea7SDK\u7684\u5feb\u901f\u63a5\u5165\u65b9\u5f0f&#xff0c;\u624b\u52a8\u5b9e\u73b0MQTT\u534f\u8bae\u6808\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u6df1\u5165\u7406\u89e3\u534f\u8bae\u7ec6\u8282&#xff0c;\u66f4\u597d\u5730\u63a7\u5236\u8bbe\u5907\u884c\u4e3a&#xff0c;\u5e76\u5728\u8d44\u6e90\u53d7\u9650\u7684\u73af\u5883\u4e2d\u5b9e\u73b0\u7cbe\u7ec6\u5316\u4f18\u5316\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u62a5\u6587\u7ed3\u6784\u89e3\u6790\u5f00\u59cb&#xff0c;\u9010\u6b65\u6784\u5efa\u4e00\u4e2a\u7a33\u5b9a\u53ef\u9760\u7684STM32MQTT\u5ba2\u6237\u7aef&amp;#x<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[8315,986,217,2896],"topic":[],"class_list":["post-76731","post","type-post","status-publish","format-standard","hentry","category-server","tag-iotda","tag-mqtt","tag-stm32","tag-2896"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\uff1aSTM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/76731.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\uff1aSTM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef&#xff1a;STM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd \u5728\u7269\u8054\u7f51\u8bbe\u5907\u5f00\u53d1\u4e2d&#xff0c;\u76f4\u63a5\u4f7f\u7528MQTT\u534f\u8bae\u4e0e\u4e91\u5e73\u53f0\u8fdb\u884c\u5e95\u5c42\u4ea4\u4e92\u662f\u8bb8\u591a\u5d4c\u5165\u5f0f\u5de5\u7a0b\u5e08\u5fc5\u987b\u638c\u63e1\u7684\u6280\u80fd\u3002\u4e0d\u540c\u4e8e\u4f9d\u8d56\u9ad8\u7ea7SDK\u7684\u5feb\u901f\u63a5\u5165\u65b9\u5f0f&#xff0c;\u624b\u52a8\u5b9e\u73b0MQTT\u534f\u8bae\u6808\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u6df1\u5165\u7406\u89e3\u534f\u8bae\u7ec6\u8282&#xff0c;\u66f4\u597d\u5730\u63a7\u5236\u8bbe\u5907\u884c\u4e3a&#xff0c;\u5e76\u5728\u8d44\u6e90\u53d7\u9650\u7684\u73af\u5883\u4e2d\u5b9e\u73b0\u7cbe\u7ec6\u5316\u4f18\u5316\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u62a5\u6587\u7ed3\u6784\u89e3\u6790\u5f00\u59cb&#xff0c;\u9010\u6b65\u6784\u5efa\u4e00\u4e2a\u7a33\u5b9a\u53ef\u9760\u7684STM32MQTT\u5ba2\u6237\u7aef&amp;#x\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/76731.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-22T23:52:07+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76731.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/76731.html\",\"name\":\"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\uff1aSTM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-22T23:52:07+00:00\",\"dateModified\":\"2026-02-22T23:52:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/76731.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/76731.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76731.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\uff1aSTM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\uff1aSTM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/76731.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\uff1aSTM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef&#xff1a;STM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd \u5728\u7269\u8054\u7f51\u8bbe\u5907\u5f00\u53d1\u4e2d&#xff0c;\u76f4\u63a5\u4f7f\u7528MQTT\u534f\u8bae\u4e0e\u4e91\u5e73\u53f0\u8fdb\u884c\u5e95\u5c42\u4ea4\u4e92\u662f\u8bb8\u591a\u5d4c\u5165\u5f0f\u5de5\u7a0b\u5e08\u5fc5\u987b\u638c\u63e1\u7684\u6280\u80fd\u3002\u4e0d\u540c\u4e8e\u4f9d\u8d56\u9ad8\u7ea7SDK\u7684\u5feb\u901f\u63a5\u5165\u65b9\u5f0f&#xff0c;\u624b\u52a8\u5b9e\u73b0MQTT\u534f\u8bae\u6808\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u6df1\u5165\u7406\u89e3\u534f\u8bae\u7ec6\u8282&#xff0c;\u66f4\u597d\u5730\u63a7\u5236\u8bbe\u5907\u884c\u4e3a&#xff0c;\u5e76\u5728\u8d44\u6e90\u53d7\u9650\u7684\u73af\u5883\u4e2d\u5b9e\u73b0\u7cbe\u7ec6\u5316\u4f18\u5316\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u62a5\u6587\u7ed3\u6784\u89e3\u6790\u5f00\u59cb&#xff0c;\u9010\u6b65\u6784\u5efa\u4e00\u4e2a\u7a33\u5b9a\u53ef\u9760\u7684STM32MQTT\u5ba2\u6237\u7aef&amp;#x","og_url":"https:\/\/www.wsisp.com\/helps\/76731.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-22T23:52:07+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/76731.html","url":"https:\/\/www.wsisp.com\/helps\/76731.html","name":"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\uff1aSTM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-22T23:52:07+00:00","dateModified":"2026-02-22T23:52:07+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/76731.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/76731.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/76731.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u96f6\u6784\u5efa\u5d4c\u5165\u5f0fMQTT\u5ba2\u6237\u7aef\uff1aSTM32\u4e0e\u534e\u4e3a\u4e91IoTDA\u7684\u534f\u8bae\u5c42\u6df1\u5ea6\u5bf9\u8bdd"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=76731"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76731\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=76731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=76731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=76731"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=76731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}