{"id":51794,"date":"2025-08-11T07:14:42","date_gmt":"2025-08-10T23:14:42","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/51794.html"},"modified":"2025-08-11T07:14:42","modified_gmt":"2025-08-10T23:14:42","slug":"mqtt%e4%b8%8e%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%80%9a%e8%ae%af","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/51794.html","title":{"rendered":"MQTT\u4e0e\u670d\u52a1\u5668\u901a\u8baf"},"content":{"rendered":"<h2>1. MQTT 3.1.1 \u6982\u89c8<\/h2>\n<ul>\n<li>\n<p>\u534f\u8bae\u5b9a\u4f4d&#xff1a;\u8f7b\u91cf\u7ea7\u53d1\u5e03\/\u8ba2\u9605&#xff08;Pub\/Sub&#xff09;&#xff0c;\u957f\u8fde\u63a5\u3001\u4f4e\u5e26\u5bbd\u3001\u5f31\u7f51\u7edc\u53cb\u597d\u3002<\/p>\n<\/li>\n<li>\n<p>\u89d2\u8272&#xff1a;<\/p>\n<ul>\n<li>\n<p>Broker&#xff08;\u670d\u52a1\u5668&#xff09;&#xff1a;\u8f6c\u53d1\u4e2d\u5fc3\u3002<\/p>\n<\/li>\n<li>\n<p>Client&#xff08;\u5ba2\u6237\u7aef&#xff09;&#xff1a;\u53d1\u5e03\u8005\/\u8ba2\u9605\u8005&#xff08;\u8bbe\u5907\/APP\/\u670d\u52a1\u7aef&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8fde\u63a5&#xff1a;TCP&#xff08;\u5e38\u89c1\u7aef\u53e3 1883&#xff09;&#xff0c;TLS \u65f6 8883&#xff1b;\u534f\u8bae\u540d &#034;MQTT&#034;\u3001\u534f\u8bae\u7ea7\u522b 4&#xff08;&#061;3.1.1&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u6d88\u606f\u6a21\u578b&#xff1a;Client PUBLISH \u2192 Broker \u2192 (\u6839\u636e\u8ba2\u9605) \u2192 Clients<\/p>\n<\/li>\n<li>\n<p>\u6838\u5fc3\u7279\u6027&#xff1a;Topic \u5c42\u7ea7\u547d\u540d\u3001QoS(0\/1\/2)\u3001\u9057\u5631&#xff08;LWT&#xff09;\u3001\u4fdd\u7559\u6d88\u606f&#xff08;Retain&#xff09;\u3001\u4f1a\u8bdd&#xff08;Clean Session\/\u6301\u4e45\u4f1a\u8bdd&#xff09;\u3001Keep Alive\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>2. \u4e3b\u9898&#xff08;Topic&#xff09;\u4e0e\u901a\u914d\u7b26&#xff08;\u975e\u5e38\u91cd\u8981&#xff09;<\/h2>\n<ul>\n<li>\n<p>Topic \u793a\u4f8b&#xff1a;<br \/>\nfactory\/a101\/dev\/001\/telemetry\/temperature<\/p>\n<\/li>\n<li>\n<p>**Topic Filter&#xff08;\u8ba2\u9605\u8fc7\u6ee4\u5668&#xff09;**\u652f\u6301\u901a\u914d\u7b26&#xff0c;\u53d1\u5e03\u4e3b\u9898\u4e0d\u5141\u8bb8\u901a\u914d\u7b26&#xff1a;<\/p>\n<ul>\n<li>\n<p>&#043;&#xff1a;\u5339\u914d\u5355\u5c42\u3002factory\/&#043;\/dev\/001\/telemetry\/#<\/p>\n<\/li>\n<li>\n<p>#&#xff1a;\u5339\u914d\u591a\u5c42\u4e14\u53ea\u80fd\u653e\u5728\u6700\u540e\u4e00\u5c42\u3002factory\/a101\/#<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u547d\u540d\u89c4\u8303\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u533a\u5206\u5927\u5c0f\u5199&#xff1b;\/\u5206\u5c42&#xff1b;UTF-8 \u6587\u672c&#xff0c;\u4e0d\u542b U&#043;0000\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e3b\u9898\u662f\u5b57\u8282\u5e8f\u5217&#xff0c;\u4f46\u9ed8\u8ba4\u89c6\u4f5c UTF-8&#xff1b;\u4e0d\u540c\u5e73\u53f0\u53ef\u80fd\u5bf9\u975e\u6cd5 UTF-8 \u4e25\u683c\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6700\u4f73\u5b9e\u8df5&#xff1a;\u7528\u660e\u786e\u5c42\u7ea7\u8868\u8fbe\u8bbe\u5907\/\u529f\u80fd\/\u65b9\u5411&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4e0a\u62a5&#xff1a;prod\/&lt;pid&gt;\/dev\/&lt;did&gt;\/up\/prop<\/p>\n<\/li>\n<li>\n<p>\u4e0b\u53d1&#xff1a;prod\/&lt;pid&gt;\/dev\/&lt;did&gt;\/down\/cmd<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u5751&#xff1a;\u628a # \u653e\u4e2d\u95f4\u3001\u5728 PUBLISH \u91cc\u7528 &#043;\/#\u3001\u6216\u5927\u5c0f\u5199\u9519\u4f4d&#xff0c;\u90fd\u4f1a\u76f4\u63a5\u5bfc\u81f4\u201c\u8ba2\u9605\u4e0d\u5230\/\u8def\u7531\u9519\u201d\u3002<\/p>\n<hr \/>\n<h2>3. QoS \u8bed\u4e49&#xff08;0\/1\/2&#xff09;\u4e0e\u5b8c\u6574\u63e1\u624b<\/h2>\n<ul>\n<li>\n<p>QoS 0&#xff08;\u6700\u591a\u4e00\u6b21&#xff09;&#xff1a;\u4e0d\u786e\u8ba4&#xff0c;\u4e22\u4e86\u5c31\u4e22&#xff0c;\u6700\u4f4e\u5ef6\u8fdf\u3001\u6700\u4f4e\u5f00\u9500\u3002<\/p>\n<\/li>\n<li>\n<p>QoS 1&#xff08;\u81f3\u5c11\u4e00\u6b21&#xff09;&#xff1a;PUBLISH \u2192 PUBACK&#xff1b;\u53ef\u80fd\u91cd\u590d\u5230\u8fbe&#xff08;\u9700\u8981\u53bb\u91cd&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>QoS 2&#xff08;\u6b63\u597d\u4e00\u6b21&#xff09;&#xff1a;\u56db\u6b65\u63e1\u624b PUBLISH\u2192PUBREC\u2192PUBREL\u2192PUBCOMP&#xff0c;\u6700\u91cd\u6700\u7a33&#xff0c;\u5ef6\u8fdf\u9ad8\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5e8f\u4fdd\u8bc1&#xff1a;\u540c\u4e00\u5ba2\u6237\u7aef\u540c\u4e00\u8ba2\u9605\u7684\u540c\u4e00 Topic \u4e0a&#xff0c;Broker \u9700\u6309\u53d1\u9001\u987a\u5e8f\u6295\u9012&#xff08;\u4e0d\u540c Topic \u95f4\u4e0d\u4fdd\u8bc1\u987a\u5e8f&#xff09;\u3002<\/p>\n<p>\u6ce8\u610f\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>QoS&gt;0 \u7684\u6d88\u606f\u5fc5\u987b\u643a\u5e26 Packet Identifier&#xff08;16\u4f4d&#xff0c;\u5728\u201c\u540c\u4e00\u65b9\u5411\u7684\u5728\u9014\u201d\u8981\u552f\u4e00&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u65ad\u7ebf\u91cd\u8fde\u8981\u91cd\u53d1\u672a\u786e\u8ba4\u6d88\u606f&#xff1a;\u91cd\u53d1\u65f6\u9700\u7f6e DUP&#061;1&#xff08;\u5e93\u4e00\u822c\u5e2e\u4f60\u505a&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>4. \u4fdd\u7559\u6d88\u606f&#xff08;Retain&#xff09;<\/h2>\n<ul>\n<li>\n<p>\u53d1\u5e03\u65f6\u8bbe\u7f6e retain&#061;1&#xff0c;Broker \u4f1a\u628a\u201c\u8be5 Topic \u7684\u6700\u540e\u4e00\u6761\u201d\u5b58\u4e3a\u4fdd\u7559\u6d88\u606f&#xff0c;\u65b0\u8ba2\u9605\u8005\u4e00\u8ba2\u9605\u7acb\u5373\u6536\u5230\u5f53\u524d\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5e38\u7528\u4e8e\u6700\u65b0\u72b6\u6001\/\u914d\u7f6e\u3002<\/p>\n<\/li>\n<li>\n<p>\u6e05\u9664\u65b9\u6cd5&#xff1a;\u5411\u76f8\u540c Topic \u53d1\u4e00\u6761\u7a7a\u8f7d\u8377&#043;retain&#061;1\u3002<\/p>\n<\/li>\n<li>\n<p>\u5e38\u89c1\u5751&#xff1a;<br \/>\n1&#xff09;\u628a\u4e00\u6b21\u6027\u7684\u201c\u62a5\u8b66\u201d\u4e5f\u8bbe\u6210 retain&#xff0c;\u540e\u6765\u8ba2\u9605\u7684\u4eba\u4f1a\u4e00\u76f4\u8bef\u6536\u5230\u65e7\u62a5\u8b66\u3002<br \/>\n2&#xff09;\u5fd8\u8bb0\u6e05\u9664&#xff0c;\u5bfc\u81f4 UI \u663e\u793a\u8001\u72b6\u6001\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>5. \u9057\u5631&#xff08;LWT, Last Will and Testament&#xff09;<\/h2>\n<ul>\n<li>\n<p>\u5728 CONNECT \u91cc\u58f0\u660e&#xff1a;\u9057\u5631\u4e3b\u9898 \/ \u8d1f\u8f7d \/ QoS \/ retain\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u5ba2\u6237\u7aef\u975e\u6b63\u5e38\u4e0b\u7ebf&#xff08;\u5982\u6389\u7ebf\u3001\u8d85\u65f6\u3001\u88ab Broker \u65ad\u5f00&#xff09;\u65f6&#xff0c;Broker \u81ea\u52a8\u53d1\u5e03\u9057\u5631\u3002<\/p>\n<\/li>\n<li>\n<p>\u5e38\u7528\u6a21\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u9057\u5631&#xff1a;status\/&lt;dev&gt; &#061; {&#034;online&#034;:false}&#xff08;retain&#061;1&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u4e0a\u7ebf\u540e\u624b\u52a8\u53d1\u5e03&#xff1a;status\/&lt;dev&gt; &#061; {&#034;online&#034;:true}&#xff08;retain&#061;1&#xff0c;\u8986\u76d6\u9057\u5631&#xff09;<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u6ce8\u610f&#xff1a;\u6b63\u5e38 DISCONNECT \u4e0d\u4f1a\u89e6\u53d1\u9057\u5631\u3002<\/p>\n<hr \/>\n<h2>6. \u4f1a\u8bdd\u4e0e Clean Session<\/h2>\n<ul>\n<li>\n<p>Clean Session&#061;1&#xff08;\u65e0\u72b6\u6001\/\u4e34\u65f6\u4f1a\u8bdd&#xff09;&#xff1a;\u65ad\u7ebf\u540e Broker \u4e0d\u4fdd\u7559\u8ba2\u9605\u548c\u672a\u6295\u9012\u6d88\u606f&#xff1b;\u91cd\u8fde\u9700\u91cd\u65b0 SUBSCRIBE\u3002<\/p>\n<\/li>\n<li>\n<p>Clean Session&#061;0&#xff08;\u6301\u4e45\u4f1a\u8bdd&#xff09;&#xff1a;Broker \u8bb0\u4f4f\u8ba2\u9605&#xff0c;QoS1\/2 \u672a\u6295\u9012\u6d88\u606f\u4f1a\u6392\u961f&#xff1b;\u91cd\u8fde\u540e\u7ee7\u7eed\u4e0b\u53d1\u3002<\/p>\n<\/li>\n<li>\n<p>CONNACK.Session Present&#xff1a;\u670d\u52a1\u7aef\u544a\u77e5\u201c\u662f\u5426\u5b58\u5728\u65e7\u4f1a\u8bdd\u201d\u3002<\/p>\n<\/li>\n<li>\n<p>\u7a7a ClientID&#xff08;3.1.1&#xff09;&#xff1a;\u53ea\u6709\u5728 Clean Session&#061;1 \u65f6\u670d\u52a1\u7aef\u53ef\u4ee5\u5206\u914d\u968f\u673a ClientID&#xff08;\u5f88\u591a\u5e73\u53f0\u7981\u7528&#xff0c;\u6700\u597d\u81ea\u5df1\u7ed9\u552f\u4e00 ID&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5751&#xff1a;Clean Session&#061;1 \u5374\u6307\u671b\u79bb\u7ebf\u4fdd\u7559\u6d88\u606f&#xff1b;\u6216 ClientID \u975e\u552f\u4e00\u5bfc\u81f4\u201c\u524d\u4e00\u4e2a\u4f1a\u8bdd\u88ab\u9876\u6389\u201d\u3002<\/p>\n<hr \/>\n<h2>7. Keep Alive&#xff08;\u5fc3\u8df3&#xff09;\u4e0e PING<\/h2>\n<ul>\n<li>\n<p>\u5ba2\u6237\u7aef\u5728 CONNECT \u91cc\u58f0\u660e\u79d2\u6570&#xff08;\u5982 60s&#xff09;\u3002\u5728\u4e00\u4e2a KeepAlive \u5468\u671f\u5185\u5fc5\u987b\u5411 Broker \u53d1\u4efb\u610f\u63a7\u5236\u62a5\u6587&#xff08;PUBLISH \u4e5f\u7b97&#xff09;\u2014\u2014\u5426\u5219\u5c31\u53d1 PINGREQ\u3002<\/p>\n<\/li>\n<li>\n<p>Broker \u56de PINGRESP&#xff1b;\u8d85\u65f6\u4f1a\u65ad\u5f00\u8fde\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p>Keep Alive&#061;0 \u8868\u793a\u4e0d\u4f7f\u7528\u5fc3\u8df3&#xff08;\u4e0d\u5efa\u8bae&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5d4c\u5165\u5f0f\u91cc\u7528 RTOS \u8f6f\u4ef6\u5b9a\u65f6\u5668\u6216\u6ef4\u7b54\u4efb\u52a1\u7ba1\u7406\u5fc3\u8df3\u66f4\u53ef\u9760\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>8. \u5b89\u5168&#xff08;3.1.1 \u672c\u8eab\u4e0d\u89c4\u5b9a&#xff0c;\u4f46\u5b9e\u9645\u5fc5\u987b&#xff09;<\/h2>\n<ul>\n<li>\n<p>TLS&#xff08;\u7aef\u53e3 8883&#xff09;&#043; \u8bc1\u4e66\u6821\u9a8c&#xff08;CA\/\u670d\u52a1\u7aef\u8bc1\u4e66&#xff09;&#xff0c;\u907f\u514d\u660e\u6587\u6cc4\u9732\u7528\u6237\u540d\/\u5bc6\u7801\u3002<\/p>\n<\/li>\n<li>\n<p>\u7528\u6237\u540d\/\u5bc6\u7801\u5728 CONNECT \u91cc\u660e\u6587&#xff08;\u65e0 TLS&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u7269\u8054\u7f51\u5e73\u53f0&#xff08;\u5982 OneNET&#xff09;\u901a\u5e38\u6709\u4ea7\u54c1ID\/\u8bbe\u5907\u540d\/\u9274\u6743\u7ea6\u5b9a&#xff1a;\u653e\u5728 ClientID\u3001Username\u3001Password \u4e2d\u7684\u67d0\u4e9b\u7ec4\u5408&#xff08;\u6309\u5e73\u53f0\u6587\u6863\u6765&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5751&#xff1a;\u53ea\u7528 1883 \u660e\u6587 &#043; \u56fa\u5b9a\u5bc6\u7801&#xff1b;\u65f6\u949f\u4e0d\u51c6\u5bfc\u81f4 TLS \u6821\u9a8c\u5931\u8d25&#xff08;\u8981\u5148 SNTP&#xff09;\u3002<\/p>\n<hr \/>\n<h2>9. \u63a7\u5236\u62a5\u6587&#xff08;\u4f60\u4f1a\u7ecf\u5e38\u770b\u5230\u7684&#xff09;<\/h2>\n<ul>\n<li>\n<p>\u8fde\u63a5\u7c7b&#xff1a;CONNECT\u2194CONNACK\u3001DISCONNECT<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u7c7b&#xff1a;PUBLISH&#xff08;\u5e26 DUP\/QoS\/RETAIN \u6807\u5fd7\u4f4d&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u786e\u8ba4\u7c7b&#xff1a;PUBACK\/PUBREC\/PUBREL\/PUBCOMP<\/p>\n<\/li>\n<li>\n<p>\u8ba2\u9605\u7c7b&#xff1a;SUBSCRIBE\u2194SUBACK\u3001UNSUBSCRIBE\u2194UNSUBACK<\/p>\n<\/li>\n<li>\n<p>\u5fc3\u8df3\u7c7b&#xff1a;PINGREQ\u2194PINGRESP<\/p>\n<\/li>\n<\/ul>\n<p>\u7ec6\u8282\u63d0\u9192&#xff1a;SUBSCRIBE\/UNSUBSCRIBE \u7684\u56fa\u5b9a\u62a5\u5934 QoS \u4f4d\u5fc5\u987b\u662f 1&#xff08;\u534f\u8bae\u8981\u6c42&#xff0c;\u5e93\u5df2\u5904\u7406&#xff09;\u3002<\/p>\n<hr \/>\n<h2>10. \u8d1f\u8f7d&#xff08;Payload&#xff09;<\/h2>\n<ul>\n<li>\n<p>\u4e8c\u8fdb\u5236\u900f\u660e&#xff1b;JSON\/CSV\/Protobuf\/CBOR \u90fd\u884c\u3002<\/p>\n<\/li>\n<li>\n<p>\u7269\u6a21\u578b\/\u5e73\u53f0\u5f80\u5f80\u89c4\u5b9a JSON \u683c\u5f0f&#xff1b;\u6ce8\u610f\u7f16\u7801&#xff08;UTF-8&#xff09;\u4e0e\u957f\u5ea6\u9650\u5236&#xff08;\u5f88\u591a Broker \u6709\u6700\u5927\u62a5\u6587\u957f\u5ea6\/\u901f\u7387\u9650\u5236&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>11. \u5e38\u89c1\u5751 &amp; \u8bbe\u8ba1\u6e05\u5355<\/h2>\n<li>\n<p>\u8ba2\u9605\u8fc7\u6ee4\u5668\u5199\u9519&#xff1a;# \u4e0d\u5728\u672b\u5c3e\u3001\u5728 PUBLISH \u91cc\u7528\u4e86\u901a\u914d\u7b26\u3002<\/p>\n<\/li>\n<li>\n<p>Retain \u6ee5\u7528&#xff0c;\u5bfc\u81f4\u201c\u9b3c\u5f71\u72b6\u6001\u201d\u3002<\/p>\n<\/li>\n<li>\n<p>ClientID \u91cd\u590d \u2192 \u8001\u8fde\u63a5\u88ab\u8e22&#xff1b;\u6216\u88ab\u5e73\u53f0\u9650\u957f\/\u9650\u5b57\u7b26\u3002<\/p>\n<\/li>\n<li>\n<p>Clean Session \u7528\u9519&#xff1a;\u4ee5\u4e3a\u80fd\u79bb\u7ebf\u5806\u79ef\u3002<\/p>\n<\/li>\n<li>\n<p>QoS 1 \u91cd\u590d\u6d88\u606f\u672a\u53bb\u91cd&#xff08;\u4ee5 PacketId &#043; \u4e1a\u52a1\u6d88\u606fID \u505a\u5e42\u7b49&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>KeepAlive \u592a\u77ed\/\u7f51\u7edc\u6296\u52a8\u5927 \u2192 \u9891\u7e41\u6389\u7ebf&#xff1b;\u592a\u957f \u2192 \u6545\u969c\u53d1\u73b0\u6162\u3002<\/p>\n<\/li>\n<li>\n<p>\u672a\u6b63\u786e\u5904\u7406\u201c\u91cd\u8fde &#043; \u91cd\u65b0\u8ba2\u9605\u201d&#xff08;Clean&#061;1&#xff09;\u6216\u201c\u8001\u4f1a\u8bdd\u6062\u590d\u201d&#xff08;Clean&#061;0&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u9057\u5631\u672a\u7528\/\u672a retain&#xff0c;\u4e91\u7aef\u5f88\u96be\u5224\u65ad\u8bbe\u5907\u5728\u7ebf\u79bb\u7ebf\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728 ISR \u91cc\u505a\u7f51\u7edc\/\u5185\u5b58\u91cd\u64cd\u4f5c&#xff1b;\u6216\u5355\u4e2a\u73af\u5f62\u7f13\u51b2\u533a\u4e0a\u4e0b\u6587\u4e0d\u5b89\u5168\u3002<\/p>\n<\/li>\n<li>\n<p>TLS \u6821\u9a8c\u5931\u8d25&#xff08;\u6ca1\u88c5 CA\u3001\u65f6\u95f4\u9519\u3001SNI \u4e0d\u5bf9&#xff09;\u3002<\/p>\n<\/li>\n<h2>12. \u4e00\u9875\u901f\u67e5&#xff08;Cheat-Sheet&#xff09;<\/h2>\n<ul>\n<li>\n<p>\u534f\u8bae\u7ea7\u522b&#xff1a;4&#xff1b;\u63a7\u5236\u62a5\u6587&#xff1a;CONNECT\/CONNACK\u3001PUBLISH(&#043;PUBACK|PUBREC|PUBREL|PUBCOMP)\u3001SUBSCRIBE\/SUBACK\u3001UNSUBSCRIBE\/UNSUBACK\u3001PINGREQ\/PINGRESP\u3001DISCONNECT<\/p>\n<\/li>\n<li>\n<p>QoS&#xff1a;0&#061;\u6700\u591a\u4e00\u6b21&#xff0c;1&#061;\u81f3\u5c11\u4e00\u6b21&#xff08;\u53bb\u91cd&#xff09;&#xff0c;2&#061;\u6b63\u597d\u4e00\u6b21&#xff08;\u6700\u7a33\u6700\u6162&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u9057\u5631&#xff1a;\u975e\u6b63\u5e38\u4e0b\u7ebf\u624d\u89e6\u53d1&#xff1b;\u4e0e\u201c\u4e0a\u7ebf\u4fdd\u7559\u6d88\u606f\u201d\u914d\u5408\u7528\u4e8e\u5728\u7ebf\u72b6\u6001<\/p>\n<\/li>\n<li>\n<p>Retain&#xff1a;\u5b58\u201c\u6700\u540e\u4e00\u6761\u201d&#xff0c;\u7a7a\u8f7d\u8377&#043;retain \u7f6e\u7a7a<\/p>\n<\/li>\n<li>\n<p>Clean Session&#xff1a;1&#061;\u4e34\u65f6&#xff08;\u91cd\u8fde\u9700\u91cd\u8ba2\u9605&#xff09;&#xff0c;0&#061;\u6301\u4e45&#xff08;\u53ef\u79bb\u7ebf\u5806\u79ef QoS1\/2&#xff09;<\/p>\n<\/li>\n<li>\n<p>Keep Alive&#xff1a;\u5efa\u8bae 30~120s&#xff1b;\u5fc3\u8df3\u5931\u8d25\u4f1a\u65ad\u5f00<\/p>\n<\/li>\n<li>\n<p>Topic&#xff1a;\u901a\u914d\u7b26\u53ea\u5728\u8ba2\u9605\u91cc&#xff1b;&#043; \u5355\u5c42&#xff0c;# \u591a\u5c42\u4e14\u672b\u5c3e<\/p>\n<\/li>\n<\/ul>\n<h2>\u62a5\u6587<\/h2>\n<h3>\u56fa\u5b9a\u62a5\u5934&#xff08;Fixed Header&#xff09;<\/h3>\n<ul>\n<li>\n<p>\u7b2c1\u5b57\u8282&#xff1a;\u9ad84\u4f4d&#061;\u62a5\u6587\u7c7b\u578b&#xff0c;\u4f4e4\u4f4d&#061;\u6807\u5fd7\u4f4d&#xff08;\u4e0d\u540c\u7c7b\u578b\u8981\u6c42\u4e0d\u540c&#xff09;<\/p>\n<ul>\n<li>\n<p>\u4f8b&#xff1a;SUBSCRIBE \u7684\u4f4e4\u4f4d\u5fc5\u987b\u662f 0010b \u2192 \u6240\u4ee5\u9996\u5b57\u8282 &#061; 1000 0010b &#061; 0x82<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5269\u4f59\u957f\u5ea6&#xff08;Remaining Length&#xff09;&#xff1a;\u53ef\u53d8\u957f\u5ea6\u6574\u578b&#xff08;VarInt&#xff0c;1~4\u5b57\u8282&#xff09;&#xff0c;&#061;\u201c\u53ef\u53d8\u62a5\u5934 &#043; \u8d1f\u8f7d\u201d\u7684\u603b\u5b57\u8282\u6570<\/p>\n<ul>\n<li>\n<p>\u7f16\u7801&#xff1a;\u6bcf\u5b57\u8282\u4f4e7\u4f4d\u5b58\u6570\u503c&#xff0c;\u9ad81\u4f4d\u4e3a\u201c\u540e\u7eed\u5b57\u8282\u6807\u5fd7\u201d\u3002<\/p>\n<ul>\n<li>\n<p>\u4f8b&#xff1a;321 \u2192 321 % 128 &#061; 65 \u2192 65|0x80&#061;0xC1&#xff1b;321\/128&#061;2&#xff08;\u6700\u540e\u4e00\u5b57\u8282&#xff09;\u2192 0x02<br \/>\n\u21d2 0xC1 0x02<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u53d8\u957f\u5b57\u7b26\u4e32&#xff08;UTF-8 Encoded String&#xff09;<\/h3>\n<ul>\n<li>\n<p>2\u5b57\u8282\u957f\u5ea6&#xff08;\u5927\u7aef&#xff09; &#043; \u5b57\u8282\u4e32<\/p>\n<ul>\n<li>\n<p>\u4f8b&#xff1a;&#034;MQTT&#034; \u2192 \u957f\u5ea64 &#061; 00 04&#xff0c;\u540e\u8ddf 4D 51 54 54<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>16\u4f4d\u6807\u8bc6&#xff08;Packet Identifier&#xff09;<\/h3>\n<ul>\n<li>\n<p>\u5927\u7aef&#xff1a;\u9ad8\u5b57\u8282\u5728\u524d<\/p>\n<ul>\n<li>\n<p>\u4f8b&#xff1a;0x002A \u2192 00 2A<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>1&#xff09;CONNECT&#xff08;\u6700\u5c0f\u53ef\u7528\u793a\u4f8b&#xff09;<\/h2>\n<p>\u9700\u6c42&#xff1a;CleanSession&#061;1&#xff0c;KeepAlive&#061;60&#xff0c;ClientID&#061;&#034;prodA_dev001&#034;&#xff1b;\u65e0\u7528\u6237\u540d\/\u5bc6\u7801&#xff0c;\u65e0\u9057\u5631&#xff08;\u4fbf\u4e8e\u770b\u6e05\u7ed3\u6784&#xff09;\u3002<\/p>\n<h4>\u8ba1\u7b97<\/h4>\n<ul>\n<li>\n<p>\u56fa\u5b9a\u62a5\u5934\u9996\u5b57\u8282&#xff1a;CONNECT&#061;\u7c7b\u578b1 \u2192 0001 &lt;&lt; 4 &#061; 0x10&#xff1b;flag \u5fc5\u987b 0000 \u2192 0x10<\/p>\n<\/li>\n<li>\n<p>\u53ef\u53d8\u62a5\u5934&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u534f\u8bae\u540d &#034;MQTT&#034;&#xff1a;00 04 4D 51 54 54&#xff08;6\u5b57\u8282&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u534f\u8bae\u7ea7\u522b&#xff1a;04<\/p>\n<\/li>\n<li>\n<p>\u8fde\u63a5\u6807\u5fd7&#xff08;Connect Flags&#xff09;&#xff1a;0000 0010b&#xff08;CleanSession&#061;1&#xff09;\u2192 0x02<\/p>\n<\/li>\n<li>\n<p>KeepAlive&#061;60 \u2192 00 3C<\/p>\n<\/li>\n<li>\n<p>\u5c0f\u8ba1&#061; 6 &#043; 1 &#043; 1 &#043; 2 &#061; 10<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8d1f\u8f7d&#xff1a;<\/p>\n<ul>\n<li>\n<p>ClientID &#034;prodA_dev001&#034; \u957f\u5ea6&#061;12 \u2192 00 0C &#043; (70 72 6F 64 41 5F 64 65 76 30 30 31)<\/p>\n<\/li>\n<li>\n<p>\u5c0f\u8ba1&#061; 14<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5269\u4f59\u957f\u5ea6 &#061; 10 &#043; 14 &#061; 24 \u2192 \u5355\u5b57\u8282 VarInt 0x18<\/p>\n<\/li>\n<\/ul>\n<h4>\u5b8c\u6574HEX<\/h4>\n<p>10 18                                     # \u56fa\u5b9a\u62a5\u5934: CONNECT, \u5269\u4f59\u957f\u5ea6&#061;0x18(24)<br \/>\n00 04 4D 51 54 54                         # \u534f\u8bae\u540d &#034;MQTT&#034;<br \/>\n04                                        # \u534f\u8bae\u7ea7\u522b&#061;4 (MQTT 3.1.1)<br \/>\n02                                        # \u8fde\u63a5\u6807\u5fd7: CleanSession&#061;1<br \/>\n00 3C                                     # KeepAlive&#061;60<br \/>\n00 0C 70 72 6F 64 41 5F 64 65 76 30 30 31 # ClientID &#034;prodA_dev001&#034;<\/p>\n<p>\u82e5\u52a0\u9057\u5631\/\u7528\u6237\u540d\/\u5bc6\u7801&#xff1a;\u8fde\u63a5\u6807\u5fd7\u4f4d\u76f8\u5e94\u7f6e1&#xff0c;\u5e76\u5728\u8d1f\u8f7d\u5c3e\u90e8\u8ffd\u52a0\u5bf9\u5e94\u7684\u201c\u957f\u5ea6&#043;\u5b57\u7b26\u4e32\u201d\u3002<\/p>\n<h2>2&#xff09;CONNACK&#xff08;\u670d\u52a1\u7aef\u56de\u5e94&#xff09;<\/h2>\n<p>\u6700\u5c0f\u6210\u529f\u793a\u4f8b&#xff1a;Session Present&#061;0&#xff0c;Return Code&#061;0x00&#xff08;\u6210\u529f&#xff09;<\/p>\n<p>20 02     # \u56fa\u5b9a\u62a5\u5934: CONNACK(0x20), \u5269\u4f59\u957f\u5ea6&#061;2<br \/>\n00        # Session Present&#061;0<br \/>\n00        # ReturnCode&#061;0x00 (Accepted)<\/p>\n<h2>3&#xff09;SUBSCRIBE&#xff08;\u91cd\u70b9&#xff1a;\u56fa\u5b9a\u62a5\u5934\u7684 QoS \u4f4d&#061;1&#xff09;<\/h2>\n<p>\u9700\u6c42&#xff1a;\u8ba2\u9605 prodA\/dev001\/down\/#&#xff0c;\u8bf7\u6c42QoS&#061;1&#xff0c;\u62a5\u6587\u6807\u8bc6 0x0010\u3002<\/p>\n<h4>\u8ba1\u7b97<\/h4>\n<ul>\n<li>\n<p>\u9996\u5b57\u8282&#xff1a;\u7c7b\u578b8&#xff08;SUBSCRIBE&#xff09;\u2192 1000 &lt;&lt;4 &#061; 0x80&#xff1b;flags&#061;0010 \u2192 0x82<\/p>\n<\/li>\n<li>\n<p>\u53ef\u53d8\u62a5\u5934&#xff1a;PacketId 00 10<\/p>\n<\/li>\n<li>\n<p>\u8d1f\u8f7d&#xff1a;TopicFilter \u4ee5UTF-8\u5b57\u7b26\u4e32\u7f16\u7801 &#043; \u6bcf\u4e2a\u540e\u97621\u5b57\u8282\u8bf7\u6c42QoS<\/p>\n<ul>\n<li>\n<p>\u5b57\u7b26\u4e32 &#034;prodA\/dev001\/down\/#&#034; \u957f\u5ea6&#061;19 \u2192 00 13<br \/>\nASCII&#xff1a;70 72 6F 64 41 2F 64 65 76 30 30 31 2F 64 6F 77 6E 2F 23<\/p>\n<\/li>\n<li>\n<p>\u8bf7\u6c42QoS &#061; 01<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5269\u4f59\u957f\u5ea6 &#061; 2&#xff08;PacketId&#xff09; &#043; (2&#043;19&#043;1) &#061; 24 \u2192 0x18<\/p>\n<\/li>\n<\/ul>\n<h4>\u5b8c\u6574HEX<\/h4>\n<p>82 18<br \/>\n00 10<br \/>\n00 13 70 72 6F 64 41 2F 64 65 76 30 30 31 2F 64 6F 77 6E 2F 23 01<\/p>\n<p>\u4e3a\u4ec0\u4e48\u9996\u5b57\u8282&#061;0x82&#xff1f;<br \/>\nSUBSCRIBE \u7c7b\u578b&#061;8 \u2192 \u9ad84\u4f4d 1000b&#061;0x8&#xff1b;\u56fa\u5b9a\u8981\u6c42 flags&#061;0010b&#061;0x2 \u2192 \u5408\u5e76\u6210 0x82\u3002<\/p>\n<h2>4&#xff09;SUBACK<\/h2>\n<p>\u5047\u8bbe\u540c\u4e00 PacketId&#xff0c;\u4e14\u670d\u52a1\u7aef\u540c\u610f QoS1&#xff1a;<\/p>\n<p>90 03     # SUBACK, \u5269\u4f59\u957f\u5ea6&#061;3<br \/>\n00 10     # PacketId<br \/>\n01        # \u8fd4\u56de\u7801: 0x00\/0x01\/0x02\u4ee3\u8868\u6388\u4e88\u7684QoS0\/1\/2&#xff1b;0x80&#061;\u5931\u8d25<\/p>\n<h2>5&#xff09;PUBLISH&#xff08;QoS1 \u793a\u4f8b&#xff09;<\/h2>\n<p>\u9700\u6c42&#xff1a;topic&#061;&#034;prodA\/dev001\/up\/temperature&#034;&#xff0c;\u8f7d\u8377 {&#034;temp&#034;:23.5}&#xff0c;QoS&#061;1&#xff0c;\u975e\u4fdd\u7559\u3001\u975eDUP\u3002PacketId&#061;0x002A\u3002<\/p>\n<h4>\u8ba1\u7b97<\/h4>\n<ul>\n<li>\n<p>\u9996\u5b57\u8282&#xff1a;\u7c7b\u578b3 \u2192 \u57fa\u6570 0x30&#xff1b;QoS1 \u2192 &#043;0x02&#xff1b;retain&#061;0&#xff1b;dup&#061;0 \u2192 0x32<\/p>\n<\/li>\n<li>\n<p>\u53ef\u53d8\u62a5\u5934&#xff1a;<\/p>\n<ul>\n<li>\n<p>TopicName \u957f\u5ea6&#xff1a;<br \/>\n&#034;prodA\/dev001\/up\/temperature&#034;<br \/>\n&#061; 5 &#043;1 &#043;6 &#043;1 &#043;2 &#043;1 &#043;11 &#061; 27 \u2192 00 1B<br \/>\n\u540e\u8ddfASCII&#xff1a;70 72 6F 64 41 2F 64 65 76 30 30 31 2F 75 70 2F 74 65 6D 70 65 72 61 74 75 72 65<\/p>\n<\/li>\n<li>\n<p>\u56e0\u4e3a QoS1&#xff0c;\u8981\u6709 PacketId&#xff1a;00 2A<\/p>\n<\/li>\n<li>\n<p>\u5c0f\u8ba1&#061; 2&#043;27 &#043; 2 &#061; 31<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8d1f\u8f7d&#xff1a;{&#034;temp&#034;:23.5} \u5b57\u8282\u6570&#061;13 \u2192 7B 22 74 65 6D 70 22 3A 32 33 2E 35 7D<\/p>\n<\/li>\n<li>\n<p>\u5269\u4f59\u957f\u5ea6 &#061; 31 &#043; 13 &#061; 44 \u2192 0x2C<\/p>\n<\/li>\n<\/ul>\n<h4>\u5b8c\u6574HEX<\/h4>\n<p>32 2C<br \/>\n00 1B 70 72 6F 64 41 2F 64 65 76 30 30 31 2F 75 70 2F 74 65 6D 70 65 72 61 74 75 72 65<br \/>\n00 2A<br \/>\n7B 22 74 65 6D 70 22 3A 32 33 2E 35 7D<\/p>\n<p>\u6ce8\u610f&#xff1a;QoS1 \u7684\u670d\u52a1\u7aef\u5e94\u7b54 PUBACK&#xff08;\u89c1\u4e0b\u4e00\u8282&#xff09;\u3002QoS0 \u65e0 PacketId&#xff0c;\u4e5f\u4e0d\u4f1a\u6709 PUBACK\u3002<\/p>\n<p>6&#xff09;PUBACK&#xff08;\u5bf9\u5e94\u4e0a\u9762\u7684 PUBLISH QoS1&#xff09;<\/p>\n<p>40 02   # PUBACK, \u5269\u4f59\u957f\u5ea6&#061;2<br \/>\n00 2A   # PacketId<\/p>\n<h2>7&#xff09;UNSUBSCRIBE \/ UNSUBACK&#xff08;\u540c\u6837 flags&#061;0010&#xff09;<\/h2>\n<p>\u9700\u6c42&#xff1a;\u9000\u8ba2\u540c\u4e00\u8fc7\u6ee4\u5668&#xff0c;PacketId&#061;0x0011\u3002<\/p>\n<h4>\u8ba1\u7b97<\/h4>\n<ul>\n<li>\n<p>\u9996\u5b57\u8282&#xff1a;\u7c7b\u578b10 \u2192 0xA0&#xff1b;flags 0010 \u2192 0xA2<\/p>\n<\/li>\n<li>\n<p>\u53ef\u53d8\u62a5\u5934&#xff1a;00 11<\/p>\n<\/li>\n<li>\n<p>\u8d1f\u8f7d&#xff1a;\u4ec5 TopicFilter&#xff08;\u65e0 QoS\u5b57\u8282&#xff09;<br \/>\n&#034;prodA\/dev001\/down\/#&#034; \u957f\u5ea6&#061;19 \u2192 00 13 &#043; (ASCII\u540c\u4e0a)<\/p>\n<\/li>\n<li>\n<p>\u5269\u4f59\u957f\u5ea6&#061; 2 &#043; (2&#043;19) &#061; 23 \u2192 0x17<\/p>\n<\/li>\n<\/ul>\n<h4>\u5b8c\u6574HEX<\/h4>\n<p>A2 17<br \/>\n00 11<br \/>\n00 13 70 72 6F 64 41 2F 64 65 76 30 30 31 2F 64 6F 77 6E 2F 23<\/p>\n<p>UNSUBACK<\/p>\n<p>B0 02<br \/>\n00 11<\/p>\n<p>8&#xff09;PINGREQ \/ PINGRESP\u3001DISCONNECT&#xff08;\u6700\u7b80\u5355&#xff09;<\/p>\n<p># PINGREQ<br \/>\nC0 00<\/p>\n<p># PINGRESP<br \/>\nD0 00<\/p>\n<p># DISCONNECT<br \/>\nE0 00<\/p>\n<h2>9&#xff09;\u4fdd\u7559\u6d88\u606f&#xff08;Retain&#xff09;\u793a\u4f8b&#xff08;\u5bf9\u6bd4\u770b\u9996\u5b57\u8282&#xff09;<\/h2>\n<p>\u9700\u6c42&#xff1a;\u72b6\u6001\u4e3b\u9898 status\/prodA\/dev001&#xff0c;\u8f7d\u8377 {&#034;online&#034;:true}&#xff0c;QoS1&#xff0c;retain&#061;1&#xff0c;PacketId&#061;0x002B\u3002<\/p>\n<ul>\n<li>\n<p>\u9996\u5b57\u8282&#xff1a;\u7c7b\u578b3(0x30) &#043; QoS1(0x02) &#043; Retain(0x01) &#061; 0x33<\/p>\n<\/li>\n<li>\n<p>Topic \u957f\u5ea6 &#034;status\/prodA\/dev001&#034; &#061; 6&#043;1&#043;5&#043;1&#043;6&#061;19 \u2192 00 13<\/p>\n<\/li>\n<li>\n<p>\u53d8\u5934\u5c0f\u8ba1&#061;2&#043;19 &#043;2&#061;23&#xff1b;\u8f7d\u8377 {&#034;online&#034;:true} &#061; 15<\/p>\n<\/li>\n<li>\n<p>\u5269\u4f59\u957f\u5ea6&#061;23&#043;15&#061;38 \u2192 0x26<\/p>\n<\/li>\n<\/ul>\n<p>\u5b8c\u6574HEX&#xff08;\u5173\u952e\u662f\u9996\u5b57\u82820x33&#xff09;<\/p>\n<p>33 26<br \/>\n00 13 73 74 61 74 75 73 2F 70 72 6F 64 41 2F 64 65 76 30 30 31<br \/>\n00 2B<br \/>\n7B 22 6F 6E 6C 69 6E 65 22 3A 74 72 75 65 7D<\/p>\n<p>10&#xff09;\u5982\u4f55\u201c\u7b97\u5f97\u5f88\u51c6\u201d&#xff1a;\u901a\u7528\u6253\u5305\u5c0f\u51fd\u6570&#xff08;C\u601d\u8def&#xff09;<\/p>\n<p>\/\/ \u53d8\u957f\u6574\u578b\u7f16\u7801&#xff08;Remaining Length&#xff09;<br \/>\nint encode_varint(unsigned char *buf, int value) {<br \/>\n    int i &#061; 0;<br \/>\n    do {<br \/>\n        unsigned char byte &#061; value % 128;<br \/>\n        value \/&#061; 128;<br \/>\n        if (value &gt; 0) byte |&#061; 0x80;  \/\/ \u7f6e\u540e\u7eed\u4f4d<br \/>\n        buf[i&#043;&#043;] &#061; byte;<br \/>\n    } while (value &gt; 0);<br \/>\n    return i; \/\/ \u8fd4\u56de\u5199\u5165\u5b57\u8282\u6570<br \/>\n}<\/p>\n<p>\/\/ \u5199UTF-8\u5b57\u7b26\u4e32: 2\u5b57\u8282\u957f\u5ea6(\u5927\u7aef) &#043; \u6570\u636e<br \/>\nint encode_utf8(unsigned char *buf, const char *s) {<br \/>\n    int n &#061; (int)strlen(s);<br \/>\n    buf[0] &#061; (n &gt;&gt; 8) &amp; 0xFF;<br \/>\n    buf[1] &#061; n &amp; 0xFF;<br \/>\n    memcpy(buf&#043;2, s, n);<br \/>\n    return 2 &#043; n;<br \/>\n}<\/p>\n<p>\/\/ \u519916\u4f4d\u5927\u7aef<br \/>\nstatic inline void put_u16(unsigned char *buf, uint16_t v) {<br \/>\n    buf[0] &#061; (v &gt;&gt; 8) &amp; 0xFF; buf[1] &#061; v &amp; 0xFF;<br \/>\n}<\/p>\n<p>\u7528\u8fd9\u4e9b\u57fa\u7840\u7816\u5757&#xff0c;\u5c31\u80fd\u201c\u673a\u68b0\u5f0f\u201d\u62fc\u51fa\u4e0a\u9762\u6240\u6709\u62a5\u6587&#xff1a;\u5148\u62fc\u53ef\u53d8\u62a5\u5934&#043;\u8d1f\u8f7d \u2192 \u7b97\u5269\u4f59\u957f\u5ea6 \u2192 \u6700\u540e\u5199\u56fa\u5b9a\u62a5\u5934\u9996\u5b57\u8282\u3002<\/p>\n<h2>11&#xff09;\u5bb9\u6613\u9519\u7684\u70b9&#xff08;\u518d\u6b21\u786e\u8ba4&#xff09;<\/h2>\n<ul>\n<li>\n<p>SUBSCRIBE\/UNSUBSCRIBE \u56fa\u5b9a\u62a5\u5934\u6807\u5fd7&#061;0010&#xff0c;\u4e0d\u662f 0000&#xff01;\u5426\u5219 Broker \u76f4\u63a5\u5224\u9519\u3002<\/p>\n<\/li>\n<li>\n<p>Remaining Length \u662f\u53ef\u53d8\u62a5\u5934&#043;\u8d1f\u8f7d\u4e4b\u548c&#xff0c;\u4e0d\u5305\u542b\u56fa\u5b9a\u62a5\u5934\u81ea\u8eab\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b57\u7b26\u4e32\u5747\u662f \u201c2\u5b57\u8282\u957f\u5ea6(\u5927\u7aef) &#043; \u5b57\u8282\u4e32\u201d&#xff0c;\u4e0d\u662f C \u98ce\u683c \\\\0 \u7ed3\u5c3e\u3002<\/p>\n<\/li>\n<li>\n<p>QoS1\/2 \u624d\u6709 Packet Identifier&#xff0c;QoS0 \u6ca1\u6709\u3002<\/p>\n<\/li>\n<li>\n<p>PUBLISH \u9996\u5b57\u8282\u4f4e\u56db\u4f4d&#xff1a;DUP(\u4f4d3) | QoS(\u4f4d1..2) | RETAIN(\u4f4d0)&#xff0c;\u7ec4\u5408\u522b\u5199\u53cd\u3002<\/p>\n<\/li>\n<li>\n<p>\u5927\u5c0f\u7aef&#xff1a;\u5168\u6309\u7f51\u7edc\u5b57\u8282\u5e8f&#xff08;\u5927\u7aef&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>Payload \u662f\u4e8c\u8fdb\u5236\u900f\u660e\u7684&#xff0c;\u4e0d\u4f1a\u81ea\u52a8\u52a0\u5f15\u53f7\u6216\\\\0\u3002<\/p>\n<\/li>\n<\/ul>\n<p>STM32 &#043; \u6e29\u6e7f\u5ea6\u4f20\u611f\u5668 \u2192 \u901a\u8fc7 ESP8266&#xff08;WiFi \u6a21\u5757&#xff09;\u2192 \u4f7f\u7528 MQTT 3.1.1 \u534f\u8bae\u4e0a\u4f20\u6570\u636e\u5230 OneNET \u5e73\u53f0\u3002<\/p>\n<p>main.c<\/p>\n<p>#include &#034;sys.h&#034;        \/\/ \u7cfb\u7edf\/\u65f6\u949f\u7b49\u57fa\u7840\u914d\u7f6e\u5934\u6587\u4ef6<br \/>\n#include &#034;delay.h&#034;      \/\/ \u6beb\u79d2\/\u5fae\u79d2\u5ef6\u65f6<br \/>\n#include &#034;led.h&#034;        \/\/ \u677f\u8f7dLED\u63a7\u5236<br \/>\n#include &#034;uart1.h&#034;      \/\/ USART1 \u6253\u5370\u8c03\u8bd5<br \/>\n#include &#034;dht11.h&#034;      \/\/ DHT11 \u4f20\u611f\u5668\u91c7\u96c6\u63a5\u53e3<br \/>\n#include &#034;esp8266.h&#034;    \/\/ ESP8266 AT \u9a71\u52a8&#xff08;\u900f\u4f20\/TCP&#xff09;<br \/>\n#include &#034;onenet.h&#034;     \/\/ MQTT \u6253\u5305\/\u6536\u53d1&#xff08;\u9762\u5411 OneNET&#xff09;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    HAL_Init();                         \/* \u521d\u59cb\u5316 HAL \u5e93&#xff08;\u590d\u4f4d\u5916\u8bbe\u3001Flash \u63a5\u53e3\u3001Systick&#xff09; *\/<br \/>\n    stm32_clock_init(RCC_PLL_MUL9);     \/* \u8bbe\u7f6e\u7cfb\u7edf\u65f6\u949f\u5230 72MHz&#xff08;HSE\u2192PLL\u00d79&#xff09; *\/<br \/>\n    led_init();                         \/* \u521d\u59cb\u5316 LED GPIO&#xff08;\u7528\u4e8e\u72b6\u6001\u6307\u793a&#xff09; *\/<br \/>\n    uart1_init(115200);                 \/* \u521d\u59cb\u5316 USART1&#061;115200&#xff0c;\u7528\u4e8e\u6253\u5370\u8c03\u8bd5 *\/<br \/>\n    esp8266_init(115200);               \/* \u521d\u59cb\u5316 ESP8266 \u4e32\u53e3\u4e0e\u8054\u7f51\u6d41\u7a0b&#xff08;STA \u8fde\u63a5\/AP\u3001TCP\u3001\u900f\u4f20&#xff09; *\/<br \/>\n    printf(&#034;hello world!\\\\r\\\\n&#034;);         \/* \u6253\u5370\u4e0a\u7535\u63d0\u793a *\/<\/p>\n<p>    printf(&#034;MQTT\u521d\u59cb\u5316&#8230;\\\\r\\\\n&#034;);        \/* \u63d0\u793a&#xff1a;\u5373\u5c06\u521d\u59cb\u5316 MQTT \u7f13\u51b2\u4e0e\u767b\u5f55\u53c2\u6570 *\/<br \/>\n    mqtt_init();                        \/* \u521d\u59cb\u5316 MQTT \u7f13\u51b2\u3001\u767b\u5f55\u53c2\u6570&#xff0c;\u5e76\u53d1\u9001\u4e00\u6b21 DISCONNECT \u6e05\u7406\u72b6\u6001 *\/<\/p>\n<p>    printf(&#034;MQTT\u8fde\u63a5&#8230;\\\\r\\\\n&#034;);          \/* \u63d0\u793a&#xff1a;\u5373\u5c06\u53d1\u8d77 MQTT CONNECT *\/<br \/>\n    mqtt_connect(MQTT_ClientID,         \/* ClientID&#xff08;\u5728 onenet.c\/mqtt_login_init \u4e2d\u586b\u5145&#xff09; *\/<br \/>\n                 MQTT_UserName,         \/* Username *\/<br \/>\n                 MQTT_PassWord);        \/* Password *\/<\/p>\n<p>    uint8_t data_send[512] &#061; {0};       \/* \u4e0a\u62a5 JSON \u6587\u672c\u7f13\u51b2&#xff08;\u53d1\u5e03\u8f7d\u8377&#xff09; *\/<br \/>\n    uint8_t dht11_data[4] &#061; {0};        \/* DHT11 \u539f\u59cb\u6570\u636e&#xff1a;\u6e7f\u5ea6\u6574\u6570\/\u5c0f\u6570 &#043; \u6e29\u5ea6\u6574\u6570\/\u5c0f\u6570 *\/<\/p>\n<p>    while(1)<br \/>\n    {<br \/>\n        memset(dht11_data, 0, 4);               \/* \u6e05\u7a7a\u4f20\u611f\u5668\u6570\u636e\u7f13\u5b58 *\/<br \/>\n        dht11_read(dht11_data);                 \/* \u89e6\u53d1 DHT11 \u91c7\u96c6&#xff08;\u963b\u585e\u5f0f\u65f6\u5e8f&#xff09; *\/<\/p>\n<p>        \/* \u7ec4\u7ec7 OneNET \u7269\u6a21\u578b\u517c\u5bb9\u7684\u5c5e\u6027\u4e0a\u62a5 JSON<br \/>\n           \u6ce8\u610f&#xff1a;%d.%d \u62fc\u63a5\u5c0f\u6570&#xff08;DHT11 \u7684 1\/10 \u7cbe\u5ea6&#xff09;&#xff0c;\u8d1f\u6e29\u5ea6\u4f20\u611f\u5668\u4e0d\u9002\u7528\u8be5\u5199\u6cd5 *\/<br \/>\n        sprintf((char *)data_send,<br \/>\n                &#034;{\\\\&#034;id\\\\&#034;:\\\\&#034;1386772172\\\\&#034;,\\\\&#034;version\\\\&#034;:\\\\&#034;1.0\\\\&#034;,\\\\&#034;params\\\\&#034;:{&#034;<br \/>\n                &#034;\\\\&#034;CurrentTemperature\\\\&#034;:{\\\\&#034;value\\\\&#034;:%d.%d},&#034;<br \/>\n                &#034;\\\\&#034;CurrentHumidity\\\\&#034;:{\\\\&#034;value\\\\&#034;:%d.%d}}}&#034;,<br \/>\n                dht11_data[2], dht11_data[3],   \/* \u6e29\u5ea6 \u6574\u6570.\u5c0f\u6570 *\/<br \/>\n                dht11_data[0], dht11_data[1]);  \/* \u6e7f\u5ea6 \u6574\u6570.\u5c0f\u6570 *\/<\/p>\n<p>        \/* \u53d1\u5e03\u5230\u5c5e\u6027\u4e0a\u62a5\u4e3b\u9898&#xff08;QoS&#061;0&#xff09;<br \/>\n           \u6ce8\u610f&#xff1a;\u82e5\u8981\u4e91\u7aef\u53ef\u9760\u5230\u8fbe\/\u56de\u6267&#xff0c;\u5efa\u8bae\u7528 QoS&#061;1&#xff0c;\u5e76\u5904\u7406 PUBACK *\/<br \/>\n        mqtt_publish_data(POST_TOPIC, (char *)data_send, 0);<\/p>\n<p>        delay_ms(3000);                         \/* \u5468\u671f 3s \u4e0a\u62a5\u4e00\u6b21&#xff08;\u4ec5\u793a\u4f8b&#xff09; *\/<\/p>\n<p>        printf(&#034;\\\\r\\\\n~~~~~~~~~~~~~~~~~\u53d1\u9001\u5fc3\u8df3\u5305~~~~~~~~~~~~~~~~~\\\\r\\\\n&#034;);<br \/>\n        mqtt_send_heart();                       \/* \u53d1\u9001 PINGREQ&#xff08;C0 00&#xff09;&#xff0c;\u7528\u4e8e\u7ef4\u6301 KeepAlive *\/<br \/>\n        printf(&#034;\\\\r\\\\n~~~~~~~~~~~~~~~~~\u53d1\u9001\u5fc3\u8df3\u5305\u7ed3\u675f~~~~~~~~~~~~~~~~~\\\\r\\\\n&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<p>dht11.h<\/p>\n<p>#ifndef __DHT11_H__<br \/>\n#define __DHT11_H__<\/p>\n<p>#include &#034;sys.h&#034;  \/\/ \u5305\u542b HAL\/GPIO \u7b49\u57fa\u7840\u7c7b\u578b\u5b9a\u4e49<\/p>\n<p>\/* DHT11 \u8fde\u63a5\u5230 GPIOA.5 \u5f15\u811a&#xff08;\u9700\u6839\u636e\u5b9e\u9645\u786c\u4ef6\u6539&#xff09; *\/<br \/>\n#define DHT11_PORT          GPIOA<br \/>\n#define DHT11_PIN           GPIO_PIN_5<br \/>\n#define DHT11_CLK_ENABLE()  __HAL_RCC_GPIOA_CLK_ENABLE()  \/\/ \u4f7f\u80fd GPIOA \u65f6\u949f<\/p>\n<p>\/* \u5355\u7ebf\u534f\u8bae\u7684\u8f93\u51fa\u63a7\u5236&#xff1a;<br \/>\n   DHT11_DQ_OUT(x)&#xff1a;\u8f93\u51fa\u7535\u5e73&#xff1b;x\u4e3a1\u8f93\u51fa\u9ad8\u7535\u5e73&#xff0c;0\u8f93\u51fa\u4f4e\u7535\u5e73 *\/<br \/>\n#define DHT11_DQ_OUT(x)     do{ x ? \\\\<br \/>\n                                HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_SET) : \\\\<br \/>\n                                HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_RESET);\\\\<br \/>\n                            }while(0)<\/p>\n<p>\/* \u8bfb\u53d6\u5f15\u811a\u7535\u5e73&#xff1a;\u8fd4\u56de GPIO_PIN_SET \u6216 GPIO_PIN_RESET *\/<br \/>\n#define DHT11_DQ_IN         HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN)<\/p>\n<p>\/* \u91c7\u96c6\u51fd\u6570&#xff1a;\u8bfb\u53d6 4 \u5b57\u8282\u6709\u6548\u6570\u636e&#xff08;\u6e7f\u5ea6\u6574\u6570\/\u5c0f\u6570\u3001\u6e29\u5ea6\u6574\u6570\/\u5c0f\u6570&#xff09;&#xff0c;\u6821\u9a8c\u5728 .c \u4e2d\u5b8c\u6210 *\/<br \/>\nvoid dht11_read(uint8_t *result);<\/p>\n<p>#endif<\/p>\n<p>dht11.c<\/p>\n<p>#include &#034;dht11.h&#034;   \/\/ \u672c\u6a21\u5757\u5934\u6587\u4ef6<br \/>\n#include &#034;delay.h&#034;   \/\/ \u5fae\u79d2\/\u6beb\u79d2\u5ef6\u65f6<br \/>\n#include &#034;string.h&#034;  \/\/ memcpy<br \/>\n#include &#034;stdio.h&#034;   \/\/ printf<\/p>\n<p>\/* DHT11 \u63a5\u6536\u7684 5 \u5b57\u8282\u539f\u59cb\u6570\u636e&#xff1a;H_int, H_dec, T_int, T_dec, Checksum<br \/>\n   \u8fd9\u91cc\u7528 char \u5b9a\u4e49&#xff0c;\u4f46\u66f4\u5efa\u8bae\u7528 uint8_t&#xff08;\u65e0\u7b26\u53f7&#xff09;&#xff0c;\u4ee5\u514d\u8d1f\u6570\u6269\u5c55\u95ee\u9898 *\/<br \/>\nchar dht11_data[5] &#061; {0};<\/p>\n<p>\/* \u5c06\u5f15\u811a\u914d\u7f6e\u4e3a\u8f93\u5165\u6a21\u5f0f&#xff1a;DHT11 \u6570\u636e\u7ebf\u91ca\u653e\u540e\u7531\u4f20\u611f\u5668\u9a71\u52a8 *\/<br \/>\nvoid dht11_gpio_input(void)<br \/>\n{<br \/>\n    GPIO_InitTypeDef gpio_initstruct;<br \/>\n    DHT11_CLK_ENABLE();                          \/* \u4f7f\u80fd\u7aef\u53e3\u65f6\u949f *\/<\/p>\n<p>    gpio_initstruct.Pin &#061; DHT11_PIN;             \/* \u9009\u62e9 DHT11 \u6240\u5728\u5f15\u811a *\/<br \/>\n    gpio_initstruct.Mode &#061; GPIO_MODE_INPUT;      \/* \u8f93\u5165\u6a21\u5f0f&#xff08;\u6d6e\u7a7a&#xff09; *\/<br \/>\n    gpio_initstruct.Speed &#061; GPIO_SPEED_FREQ_HIGH;\/* \u8f93\u51fa\u901f\u5ea6\u5b57\u6bb5\u6b64\u5904\u65e0\u6548&#xff0c;\u53ef\u5ffd\u7565 *\/<br \/>\n    HAL_GPIO_Init(DHT11_PORT, &amp;gpio_initstruct); \/* \u521d\u59cb\u5316 GPIO *\/<br \/>\n}<\/p>\n<p>\/* \u5c06\u5f15\u811a\u914d\u7f6e\u4e3a\u63a8\u633d\u8f93\u51fa&#xff1a;\u7528\u4e8e\u4e3b\u673a\u62c9\u4f4e\u8d77\u59cb\u4fe1\u53f7 *\/<br \/>\nvoid dht11_gpio_output(void)<br \/>\n{<br \/>\n    GPIO_InitTypeDef gpio_initstruct;<br \/>\n    DHT11_CLK_ENABLE();                          \/* \u4f7f\u80fd\u7aef\u53e3\u65f6\u949f *\/<\/p>\n<p>    gpio_initstruct.Pin &#061; DHT11_PIN;             \/* \u9009\u62e9 DHT11 \u6240\u5728\u5f15\u811a *\/<br \/>\n    gpio_initstruct.Mode &#061; GPIO_MODE_OUTPUT_PP;  \/* \u63a8\u633d\u8f93\u51fa *\/<br \/>\n    gpio_initstruct.Speed &#061; GPIO_SPEED_FREQ_HIGH;\/* \u9ad8\u901f&#xff08;\u5f71\u54cd\u4e0a\u5347\u6cbf&#xff09; *\/<br \/>\n    HAL_GPIO_Init(DHT11_PORT, &amp;gpio_initstruct); \/* \u521d\u59cb\u5316 GPIO *\/<br \/>\n}<\/p>\n<p>\/* \u8d77\u59cb\u65f6\u5e8f&#xff1a;<br \/>\n   \u4e3b\u673a\u62c9\u4f4e\u7ea6 &gt;&#061;18ms&#xff0c;\u968f\u540e\u62c9\u9ad8\u5e76\u5207\u6362\u4e3a\u8f93\u5165&#xff0c;\u7b49\u5f85 DHT11 \u7684\u54cd\u5e94\u8109\u51b2 *\/<br \/>\nvoid dht11_start(void)<br \/>\n{<br \/>\n    dht11_gpio_output();   \/* \u5207\u6362\u4e3a\u8f93\u51fa&#xff0c;\u4e3b\u673a\u9a71\u52a8\u6570\u636e\u7ebf *\/<br \/>\n    DHT11_DQ_OUT(1);       \/* \u5148\u62c9\u9ad8&#xff0c;\u786e\u4fdd\u8d77\u59cb\u72b6\u6001 *\/<br \/>\n    DHT11_DQ_OUT(0);       \/* \u62c9\u4f4e&#xff0c;\u542f\u52a8\u4fe1\u53f7 *\/<br \/>\n    delay_ms(20);          \/* \u7ef4\u6301\u81f3\u5c11 18ms&#xff0c;\u4fdd\u8bc1 DHT11 \u8bc6\u522b *\/<br \/>\n    DHT11_DQ_OUT(1);       \/* \u62c9\u9ad8&#xff0c;\u51c6\u5907\u63a5\u6536\u54cd\u5e94 *\/<\/p>\n<p>    dht11_gpio_input();    \/* \u91ca\u653e\u603b\u7ebf&#xff0c;\u5207\u4e3a\u8f93\u5165\u7b49\u5f85\u4f20\u611f\u5668\u54cd\u5e94 *\/<\/p>\n<p>    while(DHT11_DQ_IN);      \/* \u7b49\u5f85 DHT11 \u62c9\u4f4e&#xff08;80us \u5de6\u53f3&#xff09;&#xff0c;\u82e5\u5361\u6b7b\u9700\u52a0\u8d85\u65f6\u4fdd\u62a4 *\/<br \/>\n    while(!DHT11_DQ_IN);     \/* \u7b49\u5f85 DHT11 \u62c9\u9ad8&#xff08;80us \u5de6\u53f3&#xff09; *\/<br \/>\n    while(DHT11_DQ_IN);      \/* \u7b49\u5f85 DHT11 \u518d\u6b21\u62c9\u4f4e&#xff0c;\u8fdb\u5165\u6570\u636e\u4f4d\u8f93\u51fa\u9636\u6bb5 *\/<br \/>\n}<\/p>\n<p>\/* \u8bfb\u53d6 1 \u5b57\u8282&#xff08;8 \u4f4d&#xff09;&#xff1a;<br \/>\n   \u6bcf\u4f4d\u7684\u201c\u9ad8\u7535\u5e73\u6301\u7eed\u65f6\u95f4\u201d\u51b3\u5b9a\u4f4d\u503c&#xff1a;~26-28us \u4e3a 0&#xff0c;~70us \u4e3a 1 *\/<br \/>\nuint8_t dht11_read_byte(void)<br \/>\n{<br \/>\n    uint8_t temp &#061; 0;        \/* \u6682\u5b58\u6bcf\u4e00\u4f4d *\/<br \/>\n    uint8_t i &#061; 0;           \/* \u5faa\u73af\u8ba1\u6570 *\/<br \/>\n    uint8_t read_data &#061; 0;   \/* \u8fd4\u56de\u7684 8 \u4f4d\u6570\u636e *\/<\/p>\n<p>    for(i &#061; 0; i &lt; 8; i&#043;&#043;)<br \/>\n    {<br \/>\n        while(!DHT11_DQ_IN);   \/* \u7b49\u5f85\u62c9\u9ad8\u7684\u8d77\u59cb&#xff08;\u6bcf\u4f4d\u8d77\u59cb\u4f4e\u7535\u5e73 50us&#xff09; *\/<br \/>\n        delay_us(50);          \/* \u5ef6\u65f6 50us \u540e\u91c7\u6837\u7535\u5e73&#xff08;&gt;26us &lt;70us \u7684\u5206\u754c&#xff09; *\/<\/p>\n<p>        if(DHT11_DQ_IN &#061;&#061; 1)   \/* \u82e5\u4ecd\u4e3a\u9ad8\u7535\u5e73&#xff0c;\u5224\u5b9a\u8be5\u4f4d\u4e3a 1 *\/<br \/>\n        {<br \/>\n            temp &#061; 1;<br \/>\n            while(DHT11_DQ_IN);\/* \u7b49\u5f85\u8be5\u4f4d\u7ed3\u675f\u62c9\u4f4e&#xff0c;\u8fdb\u5165\u4e0b\u4e00\u4f4d *\/<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            temp &#061; 0;          \/* \u91c7\u6837\u5230\u4f4e\u7535\u5e73&#xff0c;\u5224\u5b9a\u8be5\u4f4d\u4e3a 0 *\/<br \/>\n        }<\/p>\n<p>        read_data &#061; read_data &lt;&lt; 1; \/* \u5de6\u79fb\u4e00\u4f4d&#xff0c;\u4e3a\u65b0\u4f4d\u817e\u4f4d\u7f6e *\/<br \/>\n        read_data |&#061; temp;          \/* \u53e0\u52a0\u672c\u4f4d *\/<br \/>\n    }<\/p>\n<p>    return read_data; \/* \u8fd4\u56de 8 \u4f4d\u6570\u636e *\/<br \/>\n}<\/p>\n<p>\/* \u8bfb\u53d6 5 \u5b57\u8282\u6570\u636e&#xff0c;\u6821\u9a8c\u901a\u8fc7\u540e\u5c06\u524d 4 \u5b57\u8282\u62f7\u8d1d\u5230 result<br \/>\n   result[0..3] &#061; {H_int, H_dec, T_int, T_dec} *\/<br \/>\nvoid dht11_read(uint8_t *result)<br \/>\n{<br \/>\n    uint8_t i &#061; 0;<\/p>\n<p>    dht11_start();        \/* \u53d1\u9001\u8d77\u59cb\u5e76\u7b49\u5f85\u54cd\u5e94 *\/<br \/>\n    dht11_gpio_input();   \/* \u786e\u4fdd\u4e3a\u8f93\u5165\u72b6\u6001&#xff0c;\u5f00\u59cb\u9010\u5b57\u8282\u8bfb\u53d6 *\/<\/p>\n<p>    for(i &#061; 0; i &lt; 5; i&#043;&#043;)<br \/>\n        dht11_data[i] &#061; dht11_read_byte();  \/* \u8fde\u7eed\u8bfb\u53d6 5 \u5b57\u8282 *\/<\/p>\n<p>    \/* \u7b80\u5355\u6821\u9a8c&#xff1a;\u524d\u56db\u5b57\u8282\u6c42\u548c &#061;&#061; \u7b2c\u4e94\u5b57\u8282 *\/<br \/>\n    if(dht11_data[0] &#043; dht11_data[1] &#043; dht11_data[2] &#043; dht11_data[3] &#061;&#061; dht11_data[4])<br \/>\n    {<br \/>\n        memcpy(result, dht11_data, 4);  \/* \u62f7\u8d1d\u6709\u6548\u7684 4 \u5b57\u8282\u8fd4\u56de *\/<br \/>\n        printf(&#034;\u6e7f\u5ea6&#xff1a;%d.%dRH ,&#034;, dht11_data[0], dht11_data[1]);     \/* \u6253\u5370\u6e7f\u5ea6 *\/<br \/>\n        printf(&#034;\u6e29\u5ea6&#xff1a;%d.%d\u2103\\\\r\\\\n&#034;, dht11_data[2], dht11_data[3]);   \/* \u6253\u5370\u6e29\u5ea6 *\/<br \/>\n    }<\/p>\n<p>    delay_ms(2000);  \/* \u91c7\u6837\u95f4\u9694&#xff08;DHT11 \u5efa\u8bae \u22651s&#xff09;&#xff0c;\u6b64\u5904 2s *\/<br \/>\n}<\/p>\n<p>esp8266.h<\/p>\n<p>#ifndef __ESP8266_H__<br \/>\n#define __ESP8266_H__<\/p>\n<p>#include &#034;sys.h&#034;   \/\/ HAL\/UART \u7b49\u57fa\u7840\u5b9a\u4e49<\/p>\n<p>\/* \u4e32\u53e3\u7f13\u51b2\u533a\u5927\u5c0f&#xff08;\u6309AT\u56de\u5e94\u957f\u5ea6\u914c\u60c5\u8c03\u5927&#xff09;<br \/>\n   \u6ce8\u610f&#xff1a;\u63a5\u6536\u7f13\u51b2\u8fc7\u5c0f\u4f1a\u622a\u65ad\u8fd4\u56de&#xff0c;\u5f71\u54cd\u5339\u914d *\/<br \/>\n#define ESP8266_RX_BUF_SIZE         128<br \/>\n#define ESP8266_TX_BUF_SIZE         64<\/p>\n<p>\/* \u8fd4\u56de\u7801&#xff08;EOK&#061;\u6210\u529f&#xff0c;ERROR&#061;\u5931\u8d25\u7b49&#xff09; *\/<br \/>\n#define ESP8266_EOK                 0<br \/>\n#define ESP8266_ERROR               1<br \/>\n#define ESP8266_ETIMEOUT            2<br \/>\n#define ESP8266_EINVAL              3<\/p>\n<p>\/* WiFi \u5de5\u4f5c\u6a21\u5f0f *\/<br \/>\n#define ESP8266_STA_MODE            1<br \/>\n#define ESP8266_AP_MODE             2<br \/>\n#define ESP8266_STA_AP_MODE         3<\/p>\n<p>\/* \u8fde\u63a5\u6a21\u5f0f&#xff08;\u5355\/\u591a\u8fde\u63a5&#xff09; *\/<br \/>\n#define ESP8266_SINGLE_CONNECTION   0<br \/>\n#define ESP8266_MULTI_CONNECTION    1<\/p>\n<p>\/* WiFi AP \u53c2\u6570&#xff08;\u6309\u9700\u4fee\u6539&#xff09; *\/<br \/>\n#define WIFI_SSID                   &#034;HuaweiAP-1ED0_Guest&#034;<br \/>\n#define WIFI_PWD                    &#034;gcc11111111&#034;<\/p>\n<p>\/* MQTT \u670d\u52a1\u5668&#xff08;\u57df\u540d&#043;\u7aef\u53e3&#xff09;<br \/>\n   \u6ce8\u610f&#xff1a;\u4f60\u5f53\u524d\u662f\u660e\u6587 TCP&#xff08;AT&#043;CIPSTART &#034;TCP&#034;&#8230;&#xff09;&#xff0c;<br \/>\n   \u4e3b\u673a\u540d\u53eb mqtts.heclouds.com \u901a\u5e38\u7528\u4e8e TLS(SSL) 8883 \u7aef\u53e3&#xff1b;<br \/>\n   \u82e5 1883 \u4e0d\u5141\u8bb8\u660e\u6587&#xff0c;\u5c06\u8fde\u63a5\u5931\u8d25\u3002 *\/<br \/>\n#define TCP_SERVER_IP               &#034;mqtts.heclouds.com&#034;<br \/>\n#define TCP_SERVER_PORT             &#034;1883&#034;<\/p>\n<p>\/* \u5bf9\u5916API *\/<br \/>\nvoid esp8266_init(uint32_t baudrate);                 \/\/ \u521d\u59cb\u5316UART\u4e0e\u8054\u7f51\u6d41\u7a0b<br \/>\nvoid esp8266_receive_data(void);                      \/\/ \u8c03\u8bd5\u6253\u5370\u63a5\u6536\u4e32<br \/>\nvoid esp8266_send_data(char *data, uint16_t len);     \/\/ \u53d1\u9001\u4efb\u610f\u6570\u636e<br \/>\nuint16_t esp8266_copy_rxdata(char *data);             \/\/ \u590d\u5236\u5f53\u524d\u63a5\u6536\u7f13\u51b2\u5230\u5916\u90e8<br \/>\nuint8_t esp8266_wait_receive(void);                   \/\/ \u8f6e\u8be2\u5f0f\u201c\u63a5\u6536\u5b8c\u6210\u201d\u5224\u65ad<\/p>\n<p>#endif<\/p>\n<p>esp8266.c<\/p>\n<p>#include &#034;esp8266.h&#034;     \/\/ ESP8266 \u5934\u6587\u4ef6<br \/>\n#include &#034;stdio.h&#034;       \/\/ printf<br \/>\n#include &#034;string.h&#034;      \/\/ memset\/memcpy\/strstr<br \/>\n#include &#034;delay.h&#034;       \/\/ \u5ef6\u65f6<br \/>\n#include &#034;stdarg.h&#034;      \/\/ \u53ef\u53d8\u53c2&#xff08;\u672c\u6587\u4ef6\u672a\u4f7f\u7528&#xff0c;\u53ef\u79fb\u9664&#xff09;<\/p>\n<p>\/* \u73af\u5f62\/\u7ebf\u6027\u63a5\u6536\u4e0e\u53d1\u9001\u7f13\u51b2 *\/<br \/>\nuint8_t esp8266_rx_buf[ESP8266_RX_BUF_SIZE];<br \/>\nuint8_t esp8266_tx_buf[ESP8266_TX_BUF_SIZE];<br \/>\nuint16_t esp8266_cnt &#061; 0, esp8266_cntPre &#061; 0;  \/\/ \u5f53\u524d\u63a5\u6536\u8ba1\u6570\u4e0e\u4e0a\u4e00\u6b21\u5224\u5b9a\u8ba1\u6570<\/p>\n<p>\/* USART2 \u53e5\u67c4&#xff08;\u8fde\u63a5\u5230 ESP8266&#xff09; *\/<br \/>\nUART_HandleTypeDef esp8266_handle &#061; {0};<\/p>\n<p>\/* \u521d\u59cb\u5316 USART2&#xff1a;\u6ce2\u7279\u7387\u6765\u81ea\u5f62\u53c2 *\/<br \/>\nvoid esp8266_uart_init(uint32_t baudrate)<br \/>\n{<br \/>\n    esp8266_handle.Instance &#061; USART2;                  \/* \u4f7f\u7528 USART2 *\/<br \/>\n    esp8266_handle.Init.BaudRate &#061; baudrate;           \/* \u914d\u7f6e\u6ce2\u7279\u7387 *\/<br \/>\n    esp8266_handle.Init.WordLength &#061; UART_WORDLENGTH_8B;\/* 8\u4f4d\u6570\u636e\u4f4d *\/<br \/>\n    esp8266_handle.Init.StopBits &#061; UART_STOPBITS_1;    \/* 1\u4f4d\u505c\u6b62\u4f4d *\/<br \/>\n    esp8266_handle.Init.Parity &#061; UART_PARITY_NONE;     \/* \u65e0\u6821\u9a8c *\/<br \/>\n    esp8266_handle.Init.HwFlowCtl &#061; UART_HWCONTROL_NONE;\/* \u65e0\u786c\u4ef6\u6d41\u63a7 *\/<br \/>\n    esp8266_handle.Init.Mode &#061; UART_MODE_TX_RX;        \/* \u6536\u53d1\u6a21\u5f0f *\/<br \/>\n    HAL_UART_Init(&amp;esp8266_handle);                    \/* \u521d\u59cb\u5316\u4e32\u53e3 *\/<br \/>\n}<\/p>\n<p>\/* USART2 \u4e2d\u65ad\u670d\u52a1&#xff1a;\u8bfb\u53d6 1 \u5b57\u8282\u5e76\u653e\u5165\u63a5\u6536\u7f13\u51b2<br \/>\n   \u6ce8\u610f&#xff1a;\u5728\u4e2d\u65ad\u91cc\u8c03\u7528 HAL_UART_Receive \u662f\u963b\u585e&#043;\u8f83\u91cd\u7684\u505a\u6cd5&#xff0c;\u5efa\u8bae\u76f4\u63a5\u8bfb DR \u5bc4\u5b58\u5668\u6216\u7528 HAL_UART_Receive_IT\/DMA *\/<br \/>\nvoid USART2_IRQHandler(void)<br \/>\n{<br \/>\n    uint8_t receive_data &#061; 0;                                        \/* \u6682\u5b58\u63a5\u6536\u5230\u7684\u5b57\u8282 *\/<\/p>\n<p>    if(__HAL_UART_GET_FLAG(&amp;esp8266_handle, UART_FLAG_RXNE) !&#061; RESET)\/* \u5224\u65ad\u63a5\u6536\u7f13\u51b2\u975e\u7a7a\u6807\u5fd7 *\/<br \/>\n    {<br \/>\n        if(esp8266_cnt &gt;&#061; sizeof(esp8266_rx_buf))                    \/* \u9632\u6b62\u8d8a\u754c&#xff1a;\u6ee1\u5219\u56de\u7ed5 *\/<br \/>\n            esp8266_cnt &#061; 0;<br \/>\n        HAL_UART_Receive(&amp;esp8266_handle, &amp;receive_data, 1, 1000);   \/* \u8bfb\u53d61\u5b57\u8282&#xff08;\u963b\u585e&#xff09; *\/<br \/>\n        esp8266_rx_buf[esp8266_cnt&#043;&#043;] &#061; receive_data;                \/* \u5b58\u5165\u63a5\u6536\u7f13\u51b2 *\/<br \/>\n        \/* \u82e5\u8981\u900f\u4f20\u5230\u8c03\u8bd5\u4e32\u53e3&#xff0c;\u53ef\u5728\u6b64\u5904\u8f6c\u53d1\u5230 UART1 *\/<br \/>\n    }<br \/>\n}<\/p>\n<p>\/* \u8f6e\u8be2\u5f0f\u5224\u65ad\u201c\u63a5\u6536\u7ed3\u675f\u201d&#xff1a;<br \/>\n   \u601d\u8def&#xff1a;\u8fde\u7eed\u4e24\u6b21\u8bfb\u53d6\u8ba1\u6570\u76f8\u540c&#xff0c;\u8ba4\u4e3a 1 \u5e27\u63a5\u6536\u5b8c\u6210&#xff08;\u7b80\u5355\u8d85\u65f6\u6cd5&#xff09; *\/<br \/>\nuint8_t esp8266_wait_receive(void)<br \/>\n{<br \/>\n    if(esp8266_cnt &#061;&#061; 0)                     \/* \u8fd8\u672a\u6536\u5230\u4efb\u4f55\u5b57\u8282 *\/<br \/>\n        return ESP8266_ERROR;<\/p>\n<p>    if(esp8266_cnt &#061;&#061; esp8266_cntPre)        \/* \u8ba1\u6570\u672a\u53d8\u5316 \u2192 \u8ba4\u4e3a\u4e00\u5e27\u5df2\u6536\u5b8c *\/<br \/>\n    {<br \/>\n        esp8266_cnt &#061; 0;                     \/* \u6e05\u96f6\u8ba1\u6570&#xff0c;\u4e3a\u4e0b\u4e00\u5e27\u505a\u51c6\u5907 *\/<br \/>\n        return ESP8266_EOK;                  \/* \u8fd4\u56de\u6210\u529f *\/<br \/>\n    }<\/p>\n<p>    esp8266_cntPre &#061; esp8266_cnt;            \/* \u8bb0\u5f55\u672c\u6b21\u8ba1\u6570&#xff0c;\u7b49\u5f85\u4e0b\u4e00\u8f6e\u6bd4\u8f83 *\/<br \/>\n    return ESP8266_ERROR;                    \/* \u6682\u672a\u5b8c\u6210 *\/<br \/>\n}<\/p>\n<p>\/* \u6e05\u7a7a\u63a5\u6536\u7f13\u51b2\u4e0e\u8ba1\u6570 *\/<br \/>\nvoid esp8266_rx_clear(void)<br \/>\n{<br \/>\n    memset(esp8266_rx_buf, 0, sizeof(esp8266_rx_buf));\/* \u6e05\u7a7a\u5185\u5bb9 *\/<br \/>\n    esp8266_cnt &#061; 0;                                   \/* \u6e05\u7a7a\u8ba1\u6570 *\/<br \/>\n}<\/p>\n<p>\/* \u8c03\u8bd5&#xff1a;\u82e5\u4fa6\u6d4b\u5230\u4e00\u5e27\u7ed3\u675f\u5219\u6253\u5370\u5185\u5bb9\u5e76\u6e05\u7a7a *\/<br \/>\nvoid esp8266_receive_data(void)<br \/>\n{<br \/>\n    if(esp8266_wait_receive() &#061;&#061; ESP8266_EOK)  \/* \u5224\u65ad\u63a5\u6536\u5b8c\u6210 *\/<br \/>\n    {<br \/>\n        printf(&#034;esp8266 recv: %s\\\\r\\\\n&#034;, esp8266_rx_buf);\/* \u6253\u5370 AT \u56de\u5e94 *\/<br \/>\n        esp8266_rx_clear();                              \/* \u6e05\u7a7a\u7f13\u51b2 *\/<br \/>\n    }<br \/>\n}<\/p>\n<p>\/* \u53d1\u9001\u4efb\u610f\u6570\u636e&#xff08;\u4e0d\u9644\u52a0 \\\\r\\\\n&#xff09; *\/<br \/>\nvoid esp8266_send_data(char *data, uint16_t len)<br \/>\n{<br \/>\n    esp8266_rx_clear();                                           \/* \u53d1\u9001\u524d\u6e05\u7a7a\u63a5\u6536\u7f13\u51b2&#xff08;\u907f\u514d\u6df7\u6dc6\u4e0a\u4e00\u6b21\u56de\u5e94&#xff09; *\/<br \/>\n    HAL_UART_Transmit(&amp;esp8266_handle, (unsigned char *)data,     \/* \u4e32\u53e3\u53d1\u9001 *\/<br \/>\n                      len, 100);<br \/>\n}<\/p>\n<p>\/* \u5c06\u5f53\u524d\u63a5\u6536\u7f13\u51b2\u590d\u5236\u5230\u5916\u90e8\u7f13\u51b2&#xff0c;\u8fd4\u56de\u957f\u5ea6&#xff08;\u4e0a\u4e00\u5e27\u957f\u5ea6&#xff09; *\/<br \/>\nuint16_t esp8266_copy_rxdata(char *data)<br \/>\n{<br \/>\n    memcpy(data, esp8266_rx_buf, esp8266_cntPre);  \/* \u590d\u5236\u6700\u8fd1\u4e00\u6b21\u201c\u7a33\u5b9a\u201d\u7684\u6570\u636e *\/<br \/>\n    return esp8266_cntPre;                         \/* \u8fd4\u56de\u957f\u5ea6 *\/<br \/>\n}<\/p>\n<p>\/* \u53d1\u9001 AT \u547d\u4ee4\u5e76\u7b49\u5f85\u5305\u542b\u5173\u952e\u5b57 res \u7684\u56de\u5e94&#xff0c;\u4f7f\u7528\u201c\u8d85\u65f6&#043;\u5e27\u9759\u6b62\u201d\u7b56\u7565 *\/<br \/>\nuint8_t esp8266_send_command(char *cmd, char *res)<br \/>\n{<br \/>\n    uint8_t time_out &#061; 250;                                  \/* \u8f6e\u8be2\u6b21\u6570&#xff08;\u00d710ms&#xff09;\u7ea6 2.5s *\/<br \/>\n    esp8266_rx_clear();                                      \/* \u6e05\u7a7a\u7f13\u51b2 *\/<br \/>\n    HAL_UART_Transmit(&amp;esp8266_handle, (uint8_t *)cmd,       \/* \u53d1\u9001\u547d\u4ee4 *\/<br \/>\n                      strlen(cmd), 100);<\/p>\n<p>    while(time_out&#8211;)<br \/>\n    {<br \/>\n        if(esp8266_wait_receive() &#061;&#061; ESP8266_EOK)            \/* \u4e00\u5e27\u7a33\u5b9a *\/<br \/>\n        {<br \/>\n            if(strstr((const char*)esp8266_rx_buf, res) !&#061; NULL) \/* \u5339\u914d\u671f\u671b\u5b50\u4e32 *\/<br \/>\n                return ESP8266_EOK;                          \/* \u6210\u529f *\/<br \/>\n        }<br \/>\n        delay_ms(10);                                        \/* \u95f4\u9694 10ms \u8f6e\u8be2 *\/<br \/>\n    }<\/p>\n<p>    return ESP8266_ERROR;                                    \/* \u8d85\u65f6\u5931\u8d25 *\/<br \/>\n}<\/p>\n<p>\/* AT \u6d4b\u8bd5&#xff1a;\u671f\u5f85 &#034;OK&#034; *\/<br \/>\nuint8_t esp8266_at_test(void)<br \/>\n{<br \/>\n    return esp8266_send_command(&#034;AT\\\\r\\\\n&#034;, &#034;OK&#034;);<br \/>\n}<\/p>\n<p>\/* \u8bbe\u7f6e\u5de5\u4f5c\u6a21\u5f0f&#xff1a;1&#061;STA 2&#061;AP 3&#061;STA&#043;AP *\/<br \/>\nuint8_t esp8266_set_mode(uint8_t mode)<br \/>\n{<br \/>\n    switch(mode)<br \/>\n    {<br \/>\n        case ESP8266_STA_MODE:<br \/>\n            return esp8266_send_command(&#034;AT&#043;CWMODE&#061;1\\\\r\\\\n&#034;, &#034;OK&#034;);<\/p>\n<p>        case ESP8266_AP_MODE:<br \/>\n            return esp8266_send_command(&#034;AT&#043;CWMODE&#061;2\\\\r\\\\n&#034;, &#034;OK&#034;);<\/p>\n<p>        case ESP8266_STA_AP_MODE:<br \/>\n            return esp8266_send_command(&#034;AT&#043;CWMODE&#061;3\\\\r\\\\n&#034;, &#034;OK&#034;);<\/p>\n<p>        default:<br \/>\n            return ESP8266_EINVAL;  \/* \u975e\u6cd5\u53c2\u6570 *\/<br \/>\n    }<br \/>\n}<\/p>\n<p>\/* \u8fde\u63a5 AP&#xff1a;AT&#043;CWJAP&#061;&#034;ssid&#034;,&#034;pwd&#034;<br \/>\n   \u8fd4\u56de\u5173\u952e\u5b57\u7b49\u5f85 &#034;WIFI GOT IP&#034; *\/<br \/>\nuint8_t esp8266_join_ap(char *ssid, char *pwd)<br \/>\n{<br \/>\n    char cmd[64];<br \/>\n    sprintf(cmd, &#034;AT&#043;CWJAP&#061;\\\\&#034;%s\\\\&#034;,\\\\&#034;%s\\\\&#034;\\\\r\\\\n&#034;, ssid, pwd); \/* \u62fc\u63a5\u6307\u4ee4 *\/<br \/>\n    return esp8266_send_command(cmd, &#034;WIFI GOT IP&#034;);       \/* \u8fde\u63a5\u5e76\u7b49\u5f85\u62ff\u5230 IP *\/<br \/>\n}<\/p>\n<p>\/* \u8bbe\u7f6e\u8fde\u63a5\u6a21\u5f0f&#xff08;\u5355\/\u591a&#xff09; *\/<br \/>\nuint8_t esp8266_connection_mode(uint8_t mode)<br \/>\n{<br \/>\n    char cmd[64];<br \/>\n    sprintf(cmd, &#034;AT&#043;CIPMUX&#061;%d\\\\r\\\\n&#034;, mode);        \/* 0 \u5355\u8def&#xff0c;1 \u591a\u8def *\/<br \/>\n    return esp8266_send_command(cmd, &#034;OK&#034;);<br \/>\n}<\/p>\n<p>\/* \u8fde\u63a5 TCP \u670d\u52a1\u5668&#xff1a;AT&#043;CIPSTART&#061;&#034;TCP&#034;,&#034;host&#034;,port<br \/>\n   \u6ce8\u610f&#xff1a;\u82e5\u8981 TLS&#xff0c;\u5e94\u4f7f\u7528 &#034;SSL&#034; \u5e76\u786e\u4fdd\u6a21\u7ec4\u56fa\u4ef6\u652f\u6301\u4e0e\u8bc1\u4e66\u914d\u7f6e *\/<br \/>\nuint8_t esp8266_connect_tcp_server(char *server_ip, char *server_port)<br \/>\n{<br \/>\n    char cmd[64];<br \/>\n    sprintf(cmd, &#034;AT&#043;CIPSTART&#061;\\\\&#034;TCP\\\\&#034;,\\\\&#034;%s\\\\&#034;,%s\\\\r\\\\n&#034;, server_ip, server_port); \/* \u660e\u6587 TCP *\/<br \/>\n    return esp8266_send_command(cmd, &#034;CONNECT&#034;);                                \/* \u7b49\u5f85 CONNECT *\/<br \/>\n}<\/p>\n<p>\/* \u8fdb\u5165\u900f\u4f20&#xff1a;CIPMODE&#061;1 &#043; CIPSEND<br \/>\n   \u4e4b\u540e\u4e32\u53e3\u6570\u636e\u4f1a\u76f4\u63a5\u53d1\u5f80 TCP&#xff08;\u76f4\u8fde MQTT \u62a5\u6587&#xff09; *\/<br \/>\nuint8_t esp8266_enter_unvarnished(void)<br \/>\n{<br \/>\n    uint8_t ret;<br \/>\n    ret &#061; esp8266_send_command(&#034;AT&#043;CIPMODE&#061;1\\\\r\\\\n&#034;, &#034;OK&#034;);  \/* \u5f00\u542f\u900f\u4f20\u6a21\u5f0f *\/<br \/>\n    ret &#043;&#061; esp8266_send_command(&#034;AT&#043;CIPSEND\\\\r\\\\n&#034;, &#034;&gt;&#034;);    \/* \u8fdb\u5165\u53d1\u9001\u6001&#xff0c;\u7b49\u5f85 &#039;&gt;&#039; \u63d0\u793a *\/<br \/>\n    if (ret &#061;&#061; ESP8266_EOK)<br \/>\n        return ESP8266_EOK;                                \/* \u6210\u529f *\/<br \/>\n    else<br \/>\n        return ESP8266_ERROR;                              \/* \u5931\u8d25 *\/<br \/>\n}<\/p>\n<p>\/* \u7efc\u5408\u521d\u59cb\u5316\u6d41\u7a0b&#xff1a;<br \/>\n   1) \u914d\u7f6e\u4e32\u53e3<br \/>\n   2) AT \u6d4b\u8bd5<br \/>\n   3) \u8bbe\u7f6e STA<br \/>\n   4) \u5355\u94fe\u63a5<br \/>\n   5) \u8fde\u63a5 WiFi<br \/>\n   6) \u8fde\u63a5 TCP \u670d\u52a1\u5668<br \/>\n   7) \u8fdb\u5165\u900f\u4f20 *\/<br \/>\nvoid esp8266_init(uint32_t baudrate)<br \/>\n{<br \/>\n    printf(&#034;esp8266\u521d\u59cb\u5316\u5f00\u59cb&#8230;\\\\r\\\\n&#034;);<br \/>\n    esp8266_uart_init(baudrate);<\/p>\n<p>    printf(&#034;1. \u6d4b\u8bd5esp8266\u662f\u5426\u5b58\u5728&#8230;\\\\r\\\\n&#034;);<br \/>\n    while(esp8266_at_test())                    \/* \u8fd4\u56de\u975e0\u8868\u793a\u5931\u8d25&#xff0c;\u5faa\u73af\u91cd\u8bd5 *\/<br \/>\n        delay_ms(500);<\/p>\n<p>    printf(&#034;2. \u8bbe\u7f6e\u5de5\u4f5c\u6a21\u5f0f\u4e3aSTA&#8230;\\\\r\\\\n&#034;);<br \/>\n    while(esp8266_set_mode(ESP8266_STA_MODE))   \/* \u8bbe\u7f6e\u4e3a STA *\/<br \/>\n        delay_ms(500);<\/p>\n<p>    printf(&#034;3. \u8bbe\u7f6e\u5355\u8def\u94fe\u63a5\u6a21\u5f0f&#8230;\\\\r\\\\n&#034;);<br \/>\n    while(esp8266_connection_mode(ESP8266_SINGLE_CONNECTION)) \/* \u5355\u8fde\u63a5 *\/<br \/>\n        delay_ms(500);<\/p>\n<p>    printf(&#034;4. \u8fde\u63a5wifi&#xff0c;SSID: %s, PWD: %s\\\\r\\\\n&#034;, WIFI_SSID, WIFI_PWD);<br \/>\n    while(esp8266_join_ap(WIFI_SSID, WIFI_PWD)) \/* \u52a0\u5165 AP \u5e76\u7b49\u5f85\u62ff\u5230 IP *\/<br \/>\n        delay_ms(1500);<\/p>\n<p>    printf(&#034;5. \u8fde\u63a5TCP\u670d\u52a1\u5668&#xff0c;server_ip:%s, server_port:%s\\\\r\\\\n&#034;, TCP_SERVER_IP, TCP_SERVER_PORT);<br \/>\n    while(esp8266_connect_tcp_server(TCP_SERVER_IP, TCP_SERVER_PORT)) \/* \u4e0e MQTT \u670d\u52a1\u5668\u5efa TCP \u8fde\u63a5 *\/<br \/>\n        delay_ms(500);<\/p>\n<p>    printf(&#034;6. \u8fdb\u5165\u5230\u900f\u4f20\u6a21\u5f0f&#8230;\\\\r\\\\n&#034;);<br \/>\n    while(esp8266_enter_unvarnished())          \/* \u8fdb\u5165\u900f\u4f20 *\/<br \/>\n        delay_ms(500);<\/p>\n<p>    printf(&#034;ESP8266\u5df2\u8fde\u63a5\u4e0aTCP\u670d\u52a1\u5668\u5e76\u8fdb\u5165\u900f\u4f20\u6a21\u5f0f\\\\r\\\\n&#034;);<br \/>\n    printf(&#034;ESP8266\u521d\u59cb\u5316\u5b8c\u6210&#xff01;\\\\r\\\\n&#034;);<br \/>\n}<\/p>\n<p>onenet.h<\/p>\n<p>#ifndef _ONENET_H_<br \/>\n#define _ONENET_H_<\/p>\n<p>#include &#034;string.h&#034;  \/\/ strlen\/memcpy\/sprintf<br \/>\n#include &#034;stdio.h&#034;   \/\/ printf<br \/>\n#include &#034;stdlib.h&#034;  \/\/ \u6807\u51c6\u5e93<br \/>\n#include &#034;stdarg.h&#034;  \/\/ \u53ef\u53d8\u53c2&#xff08;\u672c\u5934\u4e2d\u672a\u7528&#xff0c;\u53ef\u79fb\u9664&#xff09;<br \/>\n#include &#034;delay.h&#034;   \/\/ delay_ms<\/p>\n<p>\/* \u5c0f\u7aef\u5e73\u53f0\u4e0a\u901a\u8fc7\u5b57\u8282\u6307\u9488\u8bbf\u95ee 32\u4f4d\u6574\u578b\u7684\u5404\u5b57\u8282&#xff08;\u6ce8\u610f\u5bf9\u9f50\/\u522b\u8d8a\u754c&#xff09;<br \/>\n   \u7528\u4e8e\u5199\u5165 MQTT \u62a5\u6587\u7684 16\u4f4d\u957f\u5ea6&#xff08;BYTE1 \u9ad8\u5b57\u8282&#xff0c;BYTE0 \u4f4e\u5b57\u8282&#xff09; *\/<br \/>\n#define BYTE0(dwTemp)       (*( char *)(&amp;dwTemp))<br \/>\n#define BYTE1(dwTemp)       (*((char *)(&amp;dwTemp) &#043; 1))<br \/>\n#define BYTE2(dwTemp)       (*((char *)(&amp;dwTemp) &#043; 2))<br \/>\n#define BYTE3(dwTemp)       (*((char *)(&amp;dwTemp) &#043; 3))<\/p>\n<p>\/* \u4e09\u4e2a MQTT \u767b\u5f55\u5b57\u6bb5&#xff0c;\u7531 mqtt_login_init() \u751f\u6210 *\/<br \/>\nextern char MQTT_ClientID[100]; \/\/ ClientID<br \/>\nextern char MQTT_UserName[100]; \/\/ Username<br \/>\nextern char MQTT_PassWord[200]; \/\/ Password<\/p>\n<p>\/* MQTT \u6536\u5305\u89e3\u6790\u540e\u7684\u7ed3\u6784\u4f53&#xff08;\u6700\u591a 512B \u4e3b\u9898\/\u8f7d\u8377&#xff09; *\/<br \/>\ntypedef struct<br \/>\n{<br \/>\n    uint8_t topic[512];     \/* \u4e3b\u9898\u7f13\u51b2 *\/<br \/>\n    uint16_t topic_len;     \/* \u4e3b\u9898\u957f\u5ea6 *\/<br \/>\n    uint8_t payload[512];   \/* \u8f7d\u8377\u7f13\u51b2 *\/<br \/>\n    uint16_t payload_len;   \/* \u8f7d\u8377\u957f\u5ea6 *\/<br \/>\n} Mqtt_RxData_Type;<\/p>\n<p>\/* OneNET \u8bbe\u5907\u8bc1\u4e66&#xff08;\u793a\u4f8b\u7528&#xff0c;\u6309\u4f60\u7684\u5b9e\u9645\u9879\u76ee\u914d\u7f6e&#xff09; *\/<br \/>\n#define PRODUCT_KEY &#034;pC0uTV161W&#034;<br \/>\n#define DEVICE_NAME &#034;dht11_01&#034;<br \/>\n#define DEVICE_SECRET &#034;75AKO7FD5KBEuSJ6BTDLPFC227w%3D&#034;  \/* \u8fd9\u91cc\u770b\u8d77\u6765\u50cf URL \u7f16\u7801\u540e\u7684\u5bc6\u94a5 *\/<\/p>\n<p>\/* \u4e3b\u9898&#xff08;OneNET Studio \u7269\u6a21\u578b\u6807\u51c6\u4e3b\u9898&#xff09; *\/<br \/>\n#define RELY_PUBLISH_TOPIC  &#034;$sys\/pC0uTV161W\/dht11_01\/thing\/property\/set_reply&#034;  \/* \u5c5e\u6027\u8bbe\u7f6e\u5e94\u7b54\u53d1\u5e03\u4e3b\u9898 *\/<br \/>\n#define SET_TOPIC           &#034;$sys\/pC0uTV161W\/dht11_01\/thing\/property\/set&#034;        \/* \u5c5e\u6027\u8bbe\u7f6e\u4e0b\u884c\u4e3b\u9898&#xff08;\u8ba2\u9605&#xff09; *\/<br \/>\n#define POST_TOPIC          &#034;$sys\/pC0uTV161W\/dht11_01\/thing\/property\/post&#034;       \/* \u5c5e\u6027\u4e0a\u62a5\u53d1\u5e03\u4e3b\u9898 *\/<\/p>\n<p>\/* \u4e8b\u4ef6\u4e0a\u62a5\u4e3b\u9898&#xff08;\u793a\u4f8b&#xff09; *\/<br \/>\n#define EVENT_PUBLISH_TOPIC &#034;$sys\/pC0uTV161W\/dht11_01\/thing\/event\/post&#034;<\/p>\n<p>\/* \u767b\u5f55\u5b57\u6bb5\u751f\u6210&#xff08;\u6b64\u5904\u6df7\u5408\u4e86\u201c\u963f\u91cc\u4e91\u98ce\u683c\u6ce8\u91ca\u201d\u548c OneNET \u4ee4\u724c\u53c2\u6570&#xff0c;\u8bf7\u4ee5\u4f60\u5e73\u53f0\u4e3a\u51c6&#xff09; *\/<br \/>\nvoid mqtt_login_init(char *ProductKey,char *DeviceName,char *DeviceSecret);<\/p>\n<p>\/* MQTT \u76f8\u5173 API&#xff08;\u6253\u5305\u4e0e\u53d1\u9001\u3001\u8ba2\u9605\u3001\u8fde\u63a5\u7b49&#xff09; *\/<br \/>\nuint8_t mqtt_publish_data(char *topic, char *message, uint8_t qos);<br \/>\nuint8_t mqtt_subscribe_topic(char *topic,uint8_t qos,uint8_t whether);<br \/>\nvoid mqtt_init(void);<br \/>\nuint8_t mqtt_connect(char *ClientID,char *Username,char *Password);<br \/>\nvoid mqtt_send_heart(void);<br \/>\nvoid mqtt_disconnect(void);<br \/>\nvoid mqtt_send_data(uint8_t *buf,uint16_t len);<br \/>\nvoid mqtt_send_response(uint8_t *id);<br \/>\nuint8_t mqtt_receive_handle(uint8_t *data_received, Mqtt_RxData_Type *rx_data);<\/p>\n<p>#endif<\/p>\n<p>onenet.c<\/p>\n<p>#include &#034;onenet.h&#034;     \/\/ \u5bf9\u5e94\u5934\u6587\u4ef6<br \/>\n#include &#034;esp8266.h&#034;    \/\/ \u53d1\u9001\u5e95\u5c42\u901a\u8fc7 esp8266_send_data<\/p>\n<p>\/* \u4e09\u4e2a\u767b\u5f55\u5b57\u6bb5\u7f13\u51b2&#xff08;CONNECT \u8f7d\u8377\u4f7f\u7528&#xff09; *\/<br \/>\nchar MQTT_ClientID[100]; \/\/ MQTT ClientID<br \/>\nchar MQTT_UserName[100]; \/\/ MQTT Username<br \/>\nchar MQTT_PassWord[200]; \/\/ MQTT Password<\/p>\n<p>\/* MQTT \u53d1\u9001\/\u63a5\u6536\u7f13\u51b2&#xff08;\u6253\u5305\/\u89e3\u6790&#xff09; *\/<br \/>\nuint8_t *mqtt_rxbuf;       \/* \u6307\u5411\u63a5\u6536\u7f13\u5b58\u533a *\/<br \/>\nuint8_t *mqtt_txbuf;       \/* \u6307\u5411\u53d1\u9001\u7f13\u5b58\u533a *\/<br \/>\nuint16_t mqtt_rxlen;       \/* \u63a5\u6536\u533a\u957f\u5ea6 *\/<br \/>\nuint16_t mqtt_txlen;       \/* \u53d1\u9001\u533a\u957f\u5ea6 *\/<br \/>\nuint8_t _mqtt_txbuf[512];  \/* \u5b9e\u9645\u53d1\u9001\u7f13\u51b2 *\/<br \/>\nuint8_t _mqtt_rxbuf[512];  \/* \u5b9e\u9645\u63a5\u6536\u7f13\u51b2 *\/<\/p>\n<p>\/* MQTT \u62a5\u6587\u7c7b\u578b\u679a\u4e3e&#xff08;\u4e0e\u56fa\u5b9a\u62a5\u5934\u7c7b\u578b\u503c\u4e00\u81f4&#xff09; *\/<br \/>\ntypedef enum<br \/>\n{<br \/>\n    M_RESERVED1 &#061; 0,    \/* \u4fdd\u7559 *\/<br \/>\n    M_CONNECT,          \/* 1  CONNECT *\/<br \/>\n    M_CONNACK,          \/* 2  CONNACK *\/<br \/>\n    M_PUBLISH,          \/* 3  PUBLISH *\/<br \/>\n    M_PUBACK,           \/* 4  PUBACK  (QoS1) *\/<br \/>\n    M_PUBREC,           \/* 5  PUBREC  (QoS2 \u6b65\u9aa41) *\/<br \/>\n    M_PUBREL,           \/* 6  PUBREL  (QoS2 \u6b65\u9aa42) *\/<br \/>\n    M_PUBCOMP,          \/* 7  PUBCOMP (QoS2 \u6b65\u9aa43) *\/<br \/>\n    M_SUBSCRIBE,        \/* 8  SUBSCRIBE *\/<br \/>\n    M_SUBACK,           \/* 9  SUBACK *\/<br \/>\n    M_UNSUBSCRIBE,      \/* 10 UNSUBSCRIBE *\/<br \/>\n    M_UNSUBACK,         \/* 11 UNSUBACK *\/<br \/>\n    M_PINGREQ,          \/* 12 PINGREQ *\/<br \/>\n    M_PINGRESP,         \/* 13 PINGRESP *\/<br \/>\n    M_DISCONNECT,       \/* 14 DISCONNECT *\/<br \/>\n    M_RESERVED2,        \/* 15 \u4fdd\u7559 *\/<br \/>\n}_typdef_mqtt_message;<\/p>\n<p>\/* \u5e38\u89c1\u56fa\u5b9a\u62a5\u6587&#xff08;HEX \u6a21\u7248&#xff09;<br \/>\n   \u8fde\u63a5\u6210\u529f\u56de\u5e94\u901a\u5e38\u662f 20 02 00 00 (CONNACK) *\/<br \/>\nconst uint8_t parket_connetAck[] &#061; {0x20,0x02,0x00,0x00};  \/* CONNACK: session present&#061;0, return code&#061;0 *\/<br \/>\nconst uint8_t parket_disconnet[] &#061; {0xe0,0x00};            \/* DISCONNECT *\/<br \/>\nconst uint8_t parket_heart[]    &#061; {0xc0,0x00};            \/* PINGREQ *\/<br \/>\nconst uint8_t parket_heart_reply[] &#061; {0xc0,0x00};         \/* \u2605\u6ce8\u610f&#xff1a;PINGRESP \u5e94\u4e3a D0 00&#xff0c;\u8fd9\u91cc\u5199\u6210 C0 00&#xff08;PINGREQ&#xff09;&#xff0c;\u672a\u4f7f\u7528\u53ef\u5ffd\u7565 *\/<br \/>\nconst uint8_t parket_subAck[]   &#061; {0x90,0x03};            \/* SUBACK \u56fa\u5b9a\u5f00\u5934&#xff08;\u540e\u7eed\u542b packet id&#043;\u8fd4\u56de\u7801&#xff09; *\/<\/p>\n<p>\/* \u751f\u6210 MQTT \u767b\u5f55\u5b57\u6bb5&#xff08;ClientID\/Username\/Password&#xff09;<br \/>\n   \u8fd9\u91cc\u91c7\u7528 OneNET \u7684 token \u6837\u5f0f&#xff08;version\/res\/et\/method\/sign&#xff09;&#xff0c;\u5177\u4f53\u4ee5\u4f60\u5e73\u53f0\u6587\u6863\u4e3a\u51c6 *\/<br \/>\nvoid mqtt_login_init(char *ProductKey,char *DeviceName,char *DeviceSecret)<br \/>\n{<br \/>\n    \/* \u76f4\u63a5\u5c06\u8bbe\u5907\u540d\u4f5c\u4e3a ClientID&#xff08;\u9700\u552f\u4e00&#xff09; *\/<br \/>\n    sprintf(MQTT_ClientID,&#034;%s&#034;, DeviceName);<\/p>\n<p>    \/* \u5c06\u4ea7\u54c1ID\u4f5c\u4e3a Username&#xff08;\u90e8\u5206\u5e73\u53f0\u8981\u6c42 Username&#061;\u4ea7\u54c1ID \u6216 \u8bbe\u5907\u540d&amp;\u4ea7\u54c1ID&#xff09; *\/<br \/>\n    sprintf(MQTT_UserName,&#034;%s&#034;, ProductKey);<\/p>\n<p>    \/* \u751f\u6210 Password&#xff1a;<br \/>\n       \u8fd9\u91cc\u662f OneNET \u7684 token \u5f62\u5f0f&#xff08;\u4e3e\u4f8b&#xff09;&#xff0c;sign&#061;DEVICE_SECRET&#xff08;\u901a\u5e38\u5e94\u662f\u8ba1\u7b97\u540e\u7684\u7b7e\u540d&#xff09;\u3002<br \/>\n       \u6ce8\u610f&#xff1a;\u4f60\u7684 DEVICE_SECRET \u5b8f\u770b\u8d77\u6765\u50cf URL \u7f16\u7801\u540e\u7684\u5b57\u7b26\u4e32\u3002 *\/<br \/>\n    sprintf(MQTT_PassWord,<br \/>\n            &#034;version&#061;2018-10-31&amp;res&#061;products%%2F%s%%2Fdevices%%2F%s&amp;et&#061;2017881776&amp;method&#061;sha1&amp;sign&#061;%s&#034;,<br \/>\n            ProductKey, DeviceName, DEVICE_SECRET);<br \/>\n}<\/p>\n<p>\/* \u521d\u59cb\u5316 MQTT \u7f13\u51b2\u4e0e\u767b\u5f55\u53c2\u6570&#xff0c;\u5e76\u5c1d\u8bd5\u53d1\u9001 DISCONNECT \u6e05\u7406\u6b8b\u7559\u4f1a\u8bdd *\/<br \/>\nvoid mqtt_init(void)<br \/>\n{<br \/>\n    mqtt_login_init(PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET);  \/* \u586b\u5145 ClientID\/Username\/Password *\/<\/p>\n<p>    \/* \u7ed1\u5b9a\u5185\u90e8\u7f13\u51b2\u5230\u6307\u9488&#xff0c;\u8bb0\u5f55\u957f\u5ea6&#xff0c;\u6e05\u96f6\u5185\u5bb9 *\/<br \/>\n    mqtt_rxbuf &#061; _mqtt_rxbuf;<br \/>\n    mqtt_rxlen &#061; sizeof(_mqtt_rxbuf);<br \/>\n    mqtt_txbuf &#061; _mqtt_txbuf;<br \/>\n    mqtt_txlen &#061; sizeof(_mqtt_txbuf);<br \/>\n    memset(mqtt_rxbuf,0,mqtt_rxlen);<br \/>\n    memset(mqtt_txbuf,0,mqtt_txlen);<\/p>\n<p>    \/* \u5148\u53d1\u4e24\u6b21 DISCONNECT&#xff0c;\u9632\u6b62\u4e0a\u7535\u590d\u4f4d\u540e\u670d\u52a1\u5668\u4ecd\u4fdd\u6301\u65e7\u8fde\u63a5\u72b6\u6001 *\/<br \/>\n    mqtt_disconnect();<br \/>\n    delay_ms(100);<br \/>\n    mqtt_disconnect();<br \/>\n    delay_ms(100);<br \/>\n}<\/p>\n<p>\/* \u53d1\u9001 CONNECT \u62a5\u6587\u5e76\u7b49\u5f85 CONNACK<br \/>\n   \u8fd4\u56de 0&#061;\u6210\u529f&#xff0c;1&#061;\u5931\u8d25 *\/<br \/>\nuint8_t mqtt_connect(char *ClientID,char *Username,char *Password)<br \/>\n{<br \/>\n    uint8_t j;                                    \/* \u8f6e\u8be2\u8ba1\u6570 *\/<br \/>\n    int ClientIDLen &#061; strlen(ClientID);           \/* ClientID \u957f\u5ea6 *\/<br \/>\n    int UsernameLen &#061; strlen(Username);           \/* Username \u957f\u5ea6 *\/<br \/>\n    int PasswordLen &#061; strlen(Password);           \/* Password \u957f\u5ea6 *\/<br \/>\n    int DataLen;                                  \/* \u5269\u4f59\u957f\u5ea6&#xff1a;\u53ef\u53d8\u62a5\u5934 &#043; \u8d1f\u8f7d *\/<\/p>\n<p>    mqtt_txlen&#061;0;                                 \/* \u53d1\u9001\u7f13\u51b2\u5199\u6307\u9488\u6e05\u96f6 *\/<\/p>\n<p>    \/* \u8ba1\u7b97\u5269\u4f59\u957f\u5ea6&#xff1a;<br \/>\n       \u53ef\u53d8\u62a5\u593410\u5b57\u8282 &#043; ClientID(2&#043;Len) &#043; Username(2&#043;Len) &#043; Password(2&#043;Len) *\/<br \/>\n    DataLen &#061; 10 &#043; (ClientIDLen&#043;2) &#043; (UsernameLen&#043;2) &#043; (PasswordLen&#043;2);<\/p>\n<p>    \/* \u56fa\u5b9a\u62a5\u5934&#xff1a;CONNECT &#061; 0x10 *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 0x10;<\/p>\n<p>    \/* \u5269\u4f59\u957f\u5ea6\u7f16\u7801&#xff08;VarInt&#xff0c;\u53ef\u80fd 1~4 \u5b57\u8282&#xff09; *\/<br \/>\n    do<br \/>\n    {<br \/>\n        uint8_t encodedByte &#061; DataLen % 128;      \/* \u53d6\u4f4e 7 \u4f4d *\/<br \/>\n        DataLen &#061; DataLen \/ 128;                  \/* \u9664\u4ee5 128 *\/<br \/>\n        if ( DataLen &gt; 0 )                        \/* \u82e5\u540e\u7eed\u8fd8\u6709\u5b57\u8282&#xff0c;\u5c06\u6700\u9ad8\u4f4d\u8bbe 1 *\/<br \/>\n            encodedByte &#061; encodedByte | 128;<br \/>\n        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; encodedByte;   \/* \u5199\u5165\u4e00\u4e2a\u5b57\u8282 *\/<br \/>\n    }while ( DataLen &gt; 0 );<\/p>\n<p>    \/* \u53ef\u53d8\u62a5\u5934&#xff1a;\u534f\u8bae\u540d &#034;MQTT&#034; *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 0;                 \/* \u534f\u8bae\u540d\u957f\u5ea6 MSB *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 4;                 \/* \u534f\u8bae\u540d\u957f\u5ea6 LSB *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; &#039;M&#039;;<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; &#039;Q&#039;;<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; &#039;T&#039;;<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; &#039;T&#039;;<\/p>\n<p>    \/* \u534f\u8bae\u7ea7\u522b&#xff1a;3.1.1 &#061; 4 *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 4;<\/p>\n<p>    \/* \u8fde\u63a5\u6807\u5fd7&#xff08;Connect Flags&#xff09;&#xff1a;<br \/>\n       0xC2 &#061; 1100 0010b<br \/>\n       bit7 username flag&#061;1\u3001bit6 password flag&#061;1\u3001bit1 clean session&#061;1<br \/>\n       will flag\/retain\/qos \u5747\u4e3a 0&#xff08;\u65e0\u9057\u5631&#xff09; *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 0xc2;<\/p>\n<p>    \/* KeepAlive&#xff1a;100 \u79d2&#xff08;0x0064&#xff09; *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 0;      \/* KeepAlive MSB *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 100;    \/* KeepAlive LSB *\/<\/p>\n<p>    \/* \u8d1f\u8f7d&#xff1a;ClientID&#xff08;2\u5b57\u8282\u957f\u5ea6&#043;\u5185\u5bb9&#xff09; *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE1(ClientIDLen);  \/* \u957f\u5ea6\u9ad8\u5b57\u8282 *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE0(ClientIDLen);  \/* \u957f\u5ea6\u4f4e\u5b57\u8282 *\/<br \/>\n    memcpy(&amp;mqtt_txbuf[mqtt_txlen], ClientID, ClientIDLen); \/* \u62f7\u8d1d\u5b57\u7b26\u4e32 *\/<br \/>\n    mqtt_txlen &#043;&#061; ClientIDLen;<\/p>\n<p>    \/* \u8d1f\u8f7d&#xff1a;Username&#xff08;\u53ef\u9009&#xff09; *\/<br \/>\n    if(UsernameLen &gt; 0)<br \/>\n    {<br \/>\n        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE1(UsernameLen);<br \/>\n        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE0(UsernameLen);<br \/>\n        memcpy(&amp;mqtt_txbuf[mqtt_txlen], Username, UsernameLen);<br \/>\n        mqtt_txlen &#043;&#061; UsernameLen;<br \/>\n    }<\/p>\n<p>    \/* \u8d1f\u8f7d&#xff1a;Password&#xff08;\u53ef\u9009&#xff09; *\/<br \/>\n    if(PasswordLen &gt; 0)<br \/>\n    {<br \/>\n        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE1(PasswordLen);<br \/>\n        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE0(PasswordLen);<br \/>\n        memcpy(&amp;mqtt_txbuf[mqtt_txlen], Password, PasswordLen);<br \/>\n        mqtt_txlen &#043;&#061; PasswordLen;<br \/>\n    }<\/p>\n<p>    \/* \u53d1\u9001 CONNECT *\/<br \/>\n    memset(mqtt_rxbuf,0,mqtt_rxlen);               \/* \u6e05\u7a7a\u63a5\u6536\u7f13\u51b2 *\/<br \/>\n    mqtt_send_data(mqtt_txbuf,mqtt_txlen);         \/* \u900f\u4f20\u53d1\u9001\u6574\u4e2a\u62a5\u6587 *\/<\/p>\n<p>    \/* \u7b80\u5355\u8f6e\u8be2\u7b49\u5f85 CONNACK&#xff08;\u6700\u591a 10*50ms&#xff09; *\/<br \/>\n    for(j&#061;0;j&lt;10;j&#043;&#043;)<br \/>\n    {<br \/>\n        delay_ms(50);<br \/>\n        if (esp8266_wait_receive() &#061;&#061; ESP8266_EOK)         \/* \u4e00\u5e27\u7a33\u5b9a *\/<br \/>\n            esp8266_copy_rxdata((char *)mqtt_rxbuf);       \/* \u590d\u5236\u5230 mqtt_rxbuf *\/<\/p>\n<p>        \/* \u5339\u914d\u5f00\u5934 20 02 00&#xff0c;\u5373 CONNACK &#043; return code&#061;0 *\/<br \/>\n        if(mqtt_rxbuf[0]&#061;&#061;parket_connetAck[0] &amp;&amp;<br \/>\n           mqtt_rxbuf[1]&#061;&#061;parket_connetAck[1] &amp;&amp;<br \/>\n           mqtt_rxbuf[2]&#061;&#061;parket_connetAck[2])<br \/>\n        {<br \/>\n            return 0; \/* \u8fde\u63a5\u6210\u529f *\/<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 1; \/* \u8d85\u65f6\/\u5931\u8d25 *\/<br \/>\n}<\/p>\n<p>\/* \u8ba2\u9605\/\u53d6\u6d88\u8ba2\u9605\u6253\u5305\u5e76\u53d1\u9001<br \/>\n   topic&#061;\u4e3b\u9898\u5b57\u7b26\u4e32<br \/>\n   qos  &#061;\u8bf7\u6c42\u7684 QoS&#xff08;\u4ec5\u8ba2\u9605\u62a5\u6587\u9700\u8981\u9644\u5e26&#xff09;<br \/>\n   whether&#061;1 \u8ba2\u9605&#xff08;SUBSCRIBE&#xff09;&#xff0c;0 \u53d6\u6d88\u8ba2\u9605&#xff08;UNSUBSCRIBE&#xff09;<br \/>\n   \u8fd4\u56de 0 \u6210\u529f&#xff0c;1 \u5931\u8d25 *\/<br \/>\nuint8_t mqtt_subscribe_topic(char *topic,uint8_t qos,uint8_t whether)<br \/>\n{<br \/>\n    uint8_t j;                     \/* \u8f6e\u8be2\u8ba1\u6570 *\/<br \/>\n    mqtt_txlen&#061;0;                  \/* \u6e05\u96f6\u53d1\u9001\u5199\u6307\u9488 *\/<br \/>\n    int topiclen &#061; strlen(topic);  \/* \u4e3b\u9898\u957f\u5ea6 *\/<\/p>\n<p>    \/* \u5269\u4f59\u957f\u5ea6&#xff1a;\u53ef\u53d8\u62a5\u59342\u5b57\u8282(packet id) &#043; \u8d1f\u8f7d(2&#043;topiclen [&#043;1\u4e2aqos]) *\/<br \/>\n    int DataLen &#061; 2 &#043; (topiclen&#043;2) &#043; (whether?1:0);<\/p>\n<p>    \/* \u56fa\u5b9a\u62a5\u5934\u7b2c\u4e00\u5b57\u8282&#xff1a;<br \/>\n       SUBSCRIBE \u5fc5\u987b 0x82&#xff1b;UNSUBSCRIBE \u5fc5\u987b 0xA2&#xff08;flags&#061;0010&#xff09; *\/<br \/>\n    if(whether) mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 0x82; \/* SUBSCRIBE *\/<br \/>\n    else        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 0xA2; \/* UNSUBSCRIBE *\/<\/p>\n<p>    \/* \u5269\u4f59\u957f\u5ea6 VarInt \u7f16\u7801 *\/<br \/>\n    do<br \/>\n    {<br \/>\n        uint8_t encodedByte &#061; DataLen % 128;<br \/>\n        DataLen &#061; DataLen \/ 128;<br \/>\n        if ( DataLen &gt; 0 )<br \/>\n            encodedByte &#061; encodedByte | 128;<br \/>\n        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; encodedByte;<br \/>\n    }while ( DataLen &gt; 0 );<\/p>\n<p>    \/* \u53ef\u53d8\u62a5\u5934&#xff1a;Packet Identifier&#xff08;\u8fd9\u91cc\u56fa\u5b9a\u7528 0x0001&#xff09; *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 0;      \/* MSB *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 0x01;   \/* LSB *\/<\/p>\n<p>    \/* \u8d1f\u8f7d&#xff1a;\u4e3b\u9898&#xff08;2\u5b57\u8282\u957f\u5ea6 &#043; \u4e3b\u9898\u5b57\u7b26\u4e32&#xff09; *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE1(topiclen);<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE0(topiclen);<br \/>\n    memcpy(&amp;mqtt_txbuf[mqtt_txlen], topic, topiclen);<br \/>\n    mqtt_txlen &#043;&#061; topiclen;<\/p>\n<p>    \/* SUBSCRIBE \u8fd8\u9700\u5728\u6bcf\u4e2a\u8fc7\u6ee4\u5668\u540e\u9644\u5e26\u201c\u8bf7\u6c42 QoS\u201d\u5b57\u8282&#xff1b;UNSUBSCRIBE \u4e0d\u9700\u8981 *\/<br \/>\n    if(whether)<br \/>\n    {<br \/>\n       mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; qos;<br \/>\n    }<\/p>\n<p>    \/* \u53d1\u9001\u5e76\u7b49\u5f85 SUBACK&#xff08;\u6216\u8005 UNSUBACK&#xff09; *\/<br \/>\n    memset(mqtt_rxbuf,0,mqtt_rxlen);<br \/>\n    mqtt_send_data(mqtt_txbuf,mqtt_txlen);<\/p>\n<p>    for(j&#061;0;j&lt;10;j&#043;&#043;)<br \/>\n    {<br \/>\n        delay_ms(50);<br \/>\n        if (esp8266_wait_receive() &#061;&#061; ESP8266_EOK)<br \/>\n            esp8266_copy_rxdata((char *)mqtt_rxbuf);<\/p>\n<p>        \/* \u7c97\u7565\u5224\u65ad&#xff1a;SUBACK \u5f00\u5934 0x90 0x03\u3002\u66f4\u4e25\u8c28\u5e94\u6821\u9a8c packet id \u548c\u8fd4\u56de\u7801 *\/<br \/>\n        if(mqtt_rxbuf[0]&#061;&#061;parket_subAck[0] &amp;&amp; mqtt_rxbuf[1]&#061;&#061;parket_subAck[1])<br \/>\n        {<br \/>\n            return 0; \/* \u6210\u529f *\/<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 1; \/* \u5931\u8d25 *\/<br \/>\n}<\/p>\n<p>\/* \u53d1\u5e03 PUBLISH \u62a5\u6587<br \/>\n   topic&#061;\u4e3b\u9898&#xff1b;message&#061;\u8f7d\u8377\u5b57\u7b26\u4e32&#xff1b;qos&#061;0\/1&#xff08;\u5f53\u524d\u4ec5\u5904\u7406 0\/1&#xff09;<br \/>\n   \u8fd4\u56de \u53d1\u9001\u5b57\u8282\u6570&#xff08;\u4e0d\u7b49\u4e8e\u662f\u5426\u6210\u529f&#xff09; *\/<br \/>\nuint8_t mqtt_publish_data(char *topic, char *message, uint8_t qos)<br \/>\n{<br \/>\n    int topicLength &#061; strlen(topic);               \/* \u4e3b\u9898\u957f\u5ea6 *\/<br \/>\n    int messageLength &#061; strlen(message);           \/* \u8f7d\u8377\u957f\u5ea6 *\/<br \/>\n    static uint16_t id&#061;0;                          \/* Packet Identifier \u9012\u589e&#xff08;QoS1 \u4f7f\u7528&#xff09; *\/<br \/>\n    int DataLen;                                   \/* \u5269\u4f59\u957f\u5ea6 *\/<\/p>\n<p>    mqtt_txlen&#061;0;                                  \/* \u5199\u6307\u9488\u6e05\u96f6 *\/<\/p>\n<p>    \/* \u5269\u4f59\u957f\u5ea6 &#061; \u4e3b\u9898 (2&#043;len) &#043; [PacketId(2,\u82e5QoS&gt;0)] &#043; \u8f7d\u8377 *\/<br \/>\n    if(qos)  DataLen &#061; (2&#043;topicLength) &#043; 2 &#043; messageLength;<br \/>\n    else     DataLen &#061; (2&#043;topicLength) &#043;     messageLength;<\/p>\n<p>    \/* \u56fa\u5b9a\u62a5\u5934\u9996\u5b57\u8282&#xff1a;<br \/>\n       0x30 &#061; PUBLISH, dup&#061;0, qos&#061;0, retain&#061;0<br \/>\n       \u2605\u6ce8\u610f&#xff1a;\u82e5 qos&#061;1&#xff0c;\u5e94\u5c06\u9996\u5b57\u8282\u7f6e\u4e3a 0x32&#xff08;qos \u4f4d&#061;01&#xff09;&#xff0c;\u8fd9\u91cc\u672a\u968f qos \u4fee\u6539&#xff0c;\u4ec5\u505a\u793a\u4f8b *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; 0x30;   \/* \u5efa\u8bae&#xff1a;qos&#061;&#061;1 \u65f6\u6539\u4e3a 0x32 *\/<\/p>\n<p>    \/* \u5269\u4f59\u957f\u5ea6 VarInt \u7f16\u7801 *\/<br \/>\n    do<br \/>\n    {<br \/>\n        uint8_t encodedByte &#061; DataLen % 128;<br \/>\n        DataLen &#061; DataLen \/ 128;<br \/>\n        if ( DataLen &gt; 0 )<br \/>\n            encodedByte &#061; encodedByte | 128;<br \/>\n        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; encodedByte;<br \/>\n    }while ( DataLen &gt; 0 );<\/p>\n<p>    \/* \u4e3b\u9898&#xff1a;2 \u5b57\u8282\u957f\u5ea6 &#043; \u5185\u5bb9 *\/<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE1(topicLength);<br \/>\n    mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE0(topicLength);<br \/>\n    memcpy(&amp;mqtt_txbuf[mqtt_txlen], topic, topicLength);<br \/>\n    mqtt_txlen &#043;&#061; topicLength;<\/p>\n<p>    \/* \u82e5 QoS&gt;0&#xff0c;\u9700\u8981\u9644\u5e26 Packet Identifier&#xff08;\u5927\u7aef&#xff09; *\/<br \/>\n    if(qos)<br \/>\n    {<br \/>\n        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE1(id);<br \/>\n        mqtt_txbuf[mqtt_txlen&#043;&#043;] &#061; BYTE0(id);<br \/>\n        id&#043;&#043;;  \/* \u4e0b\u6b21\u9012\u589e&#xff08;\u6ce8\u610f\u6ea2\u51fa\u56de\u7ed5&#xff09; *\/<br \/>\n    }<\/p>\n<p>    \/* \u62f7\u8d1d\u8f7d\u8377 *\/<br \/>\n    memcpy(&amp;mqtt_txbuf[mqtt_txlen], message, messageLength);<br \/>\n    mqtt_txlen &#043;&#061; messageLength;<\/p>\n<p>    \/* \u900f\u4f20\u53d1\u9001\u5230 TCP *\/<br \/>\n    mqtt_send_data(mqtt_txbuf,mqtt_txlen);<br \/>\n    return mqtt_txlen;  \/* \u8fd4\u56de\u53d1\u9001\u957f\u5ea6&#xff08;\u7528\u4e8e\u8c03\u8bd5&#xff09; *\/<br \/>\n}<\/p>\n<p>\/* \u89e3\u6790\u63a5\u6536\u5230\u7684 PUBLISH \u62a5\u6587&#xff08;\u7b80\u5316\u7248&#xff0c;\u4ec5\u5904\u7406 0x30\/0x32\/0x34 \u4e09\u79cd\u9996\u5b57\u8282&#xff09;<br \/>\n   data_received &#061; \u539f\u59cb\u5b57\u8282\u6d41<br \/>\n   rx_data       &#061; \u8f93\u51fa\u7ed3\u6784\u4f53&#xff08;\u4e3b\u9898\/\u8f7d\u8377\/\u957f\u5ea6&#xff09;<br \/>\n   \u8fd4\u56de 0 \u6210\u529f&#xff1b;\u975e 0&#061;\u5931\u8d25 *\/<br \/>\nuint8_t mqtt_receive_handle(uint8_t *data_received, Mqtt_RxData_Type *rx_data)<br \/>\n{<br \/>\n    uint8_t *p;                       \/* \u8bfb\u6307\u9488 *\/<br \/>\n    uint8_t encodeByte &#061; 0;           \/* VarInt \u4e34\u65f6\u5b57\u8282 *\/<br \/>\n    uint32_t multiplier &#061; 1;          \/* VarInt \u8fdb\u4f4d\u4e58\u6570 *\/<br \/>\n    uint32_t Remaining_len &#061; 0;       \/* \u5269\u4f59\u957f\u5ea6 *\/<br \/>\n    uint8_t QS_level &#061; 0;             \/* \u6807\u8bb0 QoS \u662f\u5426&gt;0 *\/<\/p>\n<p>    p &#061; data_received;                                \/* \u6307\u5411\u8d77\u59cb *\/<br \/>\n    memset(rx_data, 0, sizeof(Mqtt_RxData_Type));     \/* \u6e05\u7a7a\u8f93\u51fa\u7ed3\u6784\u4f53 *\/<\/p>\n<p>    \/* \u4ec5\u5904\u7406 PUBLISH&#xff08;0x30\/0x32\/0x34&#xff09; *\/<br \/>\n    if((*p !&#061; 0x30)&amp;&amp;(*p !&#061; 0x32)&amp;&amp;(*p !&#061; 0x34))<br \/>\n        return 1; \/* \u975e PUBLISH *\/<\/p>\n<p>    if(*p !&#061; 0x30) QS_level &#061; 1;       \/* 0x32\/0x34 \u8868\u793a QoS1\/2&#xff0c;\u6807\u8bb0\u9700\u8981\u8df3\u8fc7 PacketId *\/<\/p>\n<p>    p&#043;&#043;;                                \/* \u6307\u5411\u5269\u4f59\u957f\u5ea6\u5b57\u6bb5 *\/<br \/>\n    \/* \u89e3\u6790 VarInt&#xff1a;\u6bcf\u5b57\u8282\u4f4e7\u4f4d\u6709\u6548&#xff0c;\u9ad8\u4f4d1\u8868\u793a\u8fd8\u6709\u540e\u7eed\u5b57\u8282 *\/<br \/>\n    do{<br \/>\n        encodeByte &#061; *p&#043;&#043;;<br \/>\n        Remaining_len &#043;&#061; (encodeByte &amp; 0x7F) * multiplier;<br \/>\n        multiplier *&#061; 128;<br \/>\n        if(multiplier &gt; 128*128*128)    \/* \u6700\u591a4\u5b57\u8282&#xff0c;\u8d85\u8fc7\u89c6\u4e3a\u9519\u8bef *\/<br \/>\n            return 2;<br \/>\n    }while((encodeByte &amp; 0x80) !&#061; 0);<\/p>\n<p>    \/* \u4e3b\u9898\u957f\u5ea6&#xff08;2\u5b57\u8282\u5927\u7aef&#xff09; *\/<br \/>\n    rx_data-&gt;topic_len &#061; *p&#043;&#043;;<br \/>\n    rx_data-&gt;topic_len &#061; rx_data-&gt;topic_len * 256 &#043; *p&#043;&#043;;<\/p>\n<p>    \/* \u62f7\u8d1d\u4e3b\u9898\u5b57\u7b26\u4e32&#xff08;\u975e \\\\0 \u7ed3\u5c3e&#xff0c;\u4f7f\u7528\u957f\u5ea6\u5b57\u6bb5&#xff09; *\/<br \/>\n    memcpy(rx_data-&gt;topic, p, rx_data-&gt;topic_len);<br \/>\n    p &#043;&#061; rx_data-&gt;topic_len;<\/p>\n<p>    \/* \u82e5 QoS&gt;0&#xff0c;\u7d27\u968f\u5176\u540e\u662f 2\u5b57\u8282 PacketId&#xff0c;\u9700\u8df3\u8fc7 *\/<br \/>\n    if(QS_level !&#061; 0)<br \/>\n        p &#043;&#061; 2;<\/p>\n<p>    \/* \u8f7d\u8377\u957f\u5ea6 &#061; \u5269\u4f59\u957f\u5ea6 &#8211; \u4e3b\u9898(2&#043;len) &#8211; [packetId(2,\u82e5QoS&gt;0)] *\/<br \/>\n    rx_data-&gt;payload_len &#061; Remaining_len &#8211; rx_data-&gt;topic_len &#8211; 2;<br \/>\n    memcpy(rx_data-&gt;payload, p, rx_data-&gt;payload_len);   \/* \u62f7\u8d1d\u8f7d\u8377 *\/<\/p>\n<p>    return 0; \/* \u89e3\u6790\u6210\u529f *\/<br \/>\n}<\/p>\n<p>\/* \u53d1\u9001\u201c\u5c5e\u6027\u8bbe\u7f6e\u5e94\u7b54\u201d&#xff08;\u793a\u4f8b&#xff09;<br \/>\n   id&#061;\u5bf9\u7aef\u8bf7\u6c42\u7684 id&#xff08;\u5b57\u7b26\u4e32&#xff09;&#xff0c;\u56de\u5305 {&#034;id&#034;:&#034;xxx&#034;,&#034;code&#034;:200,&#034;msg&#034;:&#034;success&#034;} *\/<br \/>\nvoid mqtt_send_response(uint8_t *id)<br \/>\n{<br \/>\n    char buf[128] &#061; {0};                                         \/* \u672c\u5730\u7f13\u51b2 *\/<br \/>\n    sprintf(buf,&#034;{\\\\&#034;id\\\\&#034;:\\\\&#034;%s\\\\&#034;,\\\\&#034;code\\\\&#034;:200,\\\\&#034;msg\\\\&#034;:\\\\&#034;success\\\\&#034;}&#034;,id); \/* \u7ec4\u5305 JSON *\/<\/p>\n<p>    mqtt_publish_data(RELY_PUBLISH_TOPIC,(char *)buf,0);         \/* \u53d1\u5e03\u5230\u5e94\u7b54\u4e3b\u9898&#xff0c;QoS&#061;0 *\/<\/p>\n<p>    printf(&#034;\\\\r\\\\n\u53d1\u5e03\u6570\u636e:\\\\r\\\\n&#034;);<br \/>\n    printf((const char *)buf);                                   \/* \u6253\u5370\u8c03\u8bd5 *\/<br \/>\n    printf(&#034;\\\\r\\\\n&#034;);<br \/>\n}<\/p>\n<p>\/* \u53d1\u9001\u5fc3\u8df3&#xff08;PINGREQ&#xff09; *\/<br \/>\nvoid mqtt_send_heart(void)<br \/>\n{<br \/>\n    mqtt_send_data((uint8_t *)parket_heart, sizeof(parket_heart));<br \/>\n}<\/p>\n<p>\/* \u53d1\u9001 DISCONNECT *\/<br \/>\nvoid mqtt_disconnect(void)<br \/>\n{<br \/>\n    mqtt_send_data((uint8_t *)parket_disconnet, sizeof(parket_disconnet));<br \/>\n}<\/p>\n<p>\/* \u5e95\u5c42\u53d1\u9001\u51fd\u6570\u5c01\u88c5&#xff1a;\u8d70 ESP8266 \u900f\u4f20 *\/<br \/>\nvoid mqtt_send_data(uint8_t *buf,uint16_t len)<br \/>\n{<br \/>\n    esp8266_send_data((char *)buf, len);  \/* \u76f4\u63a5\u4e0b\u53d1\u5230 TCP *\/<br \/>\n}<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb823\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf13\u6b21\u3002\u6458\u8981\uff1a\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86MQTT 3.1.1\u534f\u8bae\u7684\u6838\u5fc3\u529f\u80fd\u4e0e\u5b9e\u73b0\u8981\u70b9\uff0c\u5305\u62ec\u534f\u8bae\u7279\u6027\u3001\u6d88\u606f\u6a21\u578b\u3001\u4e3b\u9898\u89c4\u5219\u3001QoS\u7ea7\u522b\u3001\u9057\u5631\u673a\u5236\u7b49\u5173\u952e\u6280\u672f\u3002\u540c\u65f6\u63d0\u4f9b\u4e86\u57fa\u4e8eSTM32+ESP8266\u7684\u7269\u8054\u7f51\u7ec8\u7aef\u5b9e\u73b0\u65b9\u6848\uff0c\u901a\u8fc7\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668\u91c7\u96c6\u6570\u636e\u5e76\u4e0a\u4f20\u81f3OneNET\u5e73\u53f0\u3002\u5185\u5bb9\u6db5\u76d6\u534f\u8bae\u62a5\u6587\u89e3\u6790\u3001\u4e3b\u9898\u8bbe\u8ba1\u89c4\u8303\u3001\u9519\u8bef\u5904\u7406\u5efa\u8bae\uff0c\u4ee5\u53ca\u5b8c\u6574\u7684\u786c\u4ef6\u8fde\u63a5\u65b9\u6848\u548c\u4ee3\u7801\u5b9e\u73b0\uff08\u5305\u62ecDHT11\u9a71\u52a8\u3001ESP8266\u901a\u4fe1\u3001MQTT\u62a5\u6587\u6253\u5305\u7b49\u6a21\u5757\uff09\uff0c\u4e3a\u7269\u8054\u7f51\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4ece\u534f\u8bae\u7406\u89e3\u5230\u843d\u5730\u7684\u5b8c\u6574\u53c2\u8003\u3002<\/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":[217,270,5144],"topic":[],"class_list":["post-51794","post","type-post","status-publish","format-standard","hentry","category-server","tag-stm32","tag-270","tag-5144"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MQTT\u4e0e\u670d\u52a1\u5668\u901a\u8baf - \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\/51794.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MQTT\u4e0e\u670d\u52a1\u5668\u901a\u8baf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb823\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf13\u6b21\u3002\u6458\u8981\uff1a\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86MQTT 3.1.1\u534f\u8bae\u7684\u6838\u5fc3\u529f\u80fd\u4e0e\u5b9e\u73b0\u8981\u70b9\uff0c\u5305\u62ec\u534f\u8bae\u7279\u6027\u3001\u6d88\u606f\u6a21\u578b\u3001\u4e3b\u9898\u89c4\u5219\u3001QoS\u7ea7\u522b\u3001\u9057\u5631\u673a\u5236\u7b49\u5173\u952e\u6280\u672f\u3002\u540c\u65f6\u63d0\u4f9b\u4e86\u57fa\u4e8eSTM32+ESP8266\u7684\u7269\u8054\u7f51\u7ec8\u7aef\u5b9e\u73b0\u65b9\u6848\uff0c\u901a\u8fc7\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668\u91c7\u96c6\u6570\u636e\u5e76\u4e0a\u4f20\u81f3OneNET\u5e73\u53f0\u3002\u5185\u5bb9\u6db5\u76d6\u534f\u8bae\u62a5\u6587\u89e3\u6790\u3001\u4e3b\u9898\u8bbe\u8ba1\u89c4\u8303\u3001\u9519\u8bef\u5904\u7406\u5efa\u8bae\uff0c\u4ee5\u53ca\u5b8c\u6574\u7684\u786c\u4ef6\u8fde\u63a5\u65b9\u6848\u548c\u4ee3\u7801\u5b9e\u73b0\uff08\u5305\u62ecDHT11\u9a71\u52a8\u3001ESP8266\u901a\u4fe1\u3001MQTT\u62a5\u6587\u6253\u5305\u7b49\u6a21\u5757\uff09\uff0c\u4e3a\u7269\u8054\u7f51\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4ece\u534f\u8bae\u7406\u89e3\u5230\u843d\u5730\u7684\u5b8c\u6574\u53c2\u8003\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/51794.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-10T23:14:42+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=\"22 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/51794.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/51794.html\",\"name\":\"MQTT\u4e0e\u670d\u52a1\u5668\u901a\u8baf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-10T23:14:42+00:00\",\"dateModified\":\"2025-08-10T23:14:42+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/51794.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/51794.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/51794.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MQTT\u4e0e\u670d\u52a1\u5668\u901a\u8baf\"}]},{\"@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":"MQTT\u4e0e\u670d\u52a1\u5668\u901a\u8baf - \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\/51794.html","og_locale":"zh_CN","og_type":"article","og_title":"MQTT\u4e0e\u670d\u52a1\u5668\u901a\u8baf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb823\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf13\u6b21\u3002\u6458\u8981\uff1a\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86MQTT 3.1.1\u534f\u8bae\u7684\u6838\u5fc3\u529f\u80fd\u4e0e\u5b9e\u73b0\u8981\u70b9\uff0c\u5305\u62ec\u534f\u8bae\u7279\u6027\u3001\u6d88\u606f\u6a21\u578b\u3001\u4e3b\u9898\u89c4\u5219\u3001QoS\u7ea7\u522b\u3001\u9057\u5631\u673a\u5236\u7b49\u5173\u952e\u6280\u672f\u3002\u540c\u65f6\u63d0\u4f9b\u4e86\u57fa\u4e8eSTM32+ESP8266\u7684\u7269\u8054\u7f51\u7ec8\u7aef\u5b9e\u73b0\u65b9\u6848\uff0c\u901a\u8fc7\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668\u91c7\u96c6\u6570\u636e\u5e76\u4e0a\u4f20\u81f3OneNET\u5e73\u53f0\u3002\u5185\u5bb9\u6db5\u76d6\u534f\u8bae\u62a5\u6587\u89e3\u6790\u3001\u4e3b\u9898\u8bbe\u8ba1\u89c4\u8303\u3001\u9519\u8bef\u5904\u7406\u5efa\u8bae\uff0c\u4ee5\u53ca\u5b8c\u6574\u7684\u786c\u4ef6\u8fde\u63a5\u65b9\u6848\u548c\u4ee3\u7801\u5b9e\u73b0\uff08\u5305\u62ecDHT11\u9a71\u52a8\u3001ESP8266\u901a\u4fe1\u3001MQTT\u62a5\u6587\u6253\u5305\u7b49\u6a21\u5757\uff09\uff0c\u4e3a\u7269\u8054\u7f51\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4ece\u534f\u8bae\u7406\u89e3\u5230\u843d\u5730\u7684\u5b8c\u6574\u53c2\u8003\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/51794.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-10T23:14:42+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"22 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/51794.html","url":"https:\/\/www.wsisp.com\/helps\/51794.html","name":"MQTT\u4e0e\u670d\u52a1\u5668\u901a\u8baf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-10T23:14:42+00:00","dateModified":"2025-08-10T23:14:42+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/51794.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/51794.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/51794.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"MQTT\u4e0e\u670d\u52a1\u5668\u901a\u8baf"}]},{"@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\/51794","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=51794"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/51794\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=51794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=51794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=51794"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=51794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}