{"id":53440,"date":"2025-08-12T11:40:07","date_gmt":"2025-08-12T03:40:07","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/53440.html"},"modified":"2025-08-12T11:40:07","modified_gmt":"2025-08-12T03:40:07","slug":"mqtt%e5%8d%8f%e8%ae%ae%ef%bc%88%e5%85%ad%ef%bc%89%e8%ae%be%e5%a4%87%e7%8a%b6%e6%80%81%e7%9b%91%e6%8e%a7%e7%9a%84%e9%81%97%e5%98%b1%e6%b6%88%e6%81%af%ef%bc%88lwt%ef%bc%89%e4%b8%8e%e4%bf%9d%e7%95%99","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/53440.html","title":{"rendered":"MQTT\u534f\u8bae\uff08\u516d\uff09\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u9057\u5631\u6d88\u606f\uff08LWT\uff09\u4e0e\u4fdd\u7559\u6d88\u606f\u6df1\u5ea6\u89e3\u6790"},"content":{"rendered":"<p>\u5728\u7269\u8054\u7f51\u7cfb\u7edf\u4e2d&#xff0c;30%\u7684\u8bbe\u5907\u5f02\u5e38\u6389\u7ebf\u4e8b\u4ef6\u56e0\u6d88\u606f\u673a\u5236\u914d\u7f6e\u4e0d\u5f53\u800c\u65e0\u6cd5\u88ab\u53ca\u65f6\u611f\u77e5\u3002<\/p>\n<p>\u60f3\u8c61\u8fd9\u6837\u4e00\u4e2a\u573a\u666f&#xff1a;\u667a\u80fd\u5de5\u5382\u7684AGV\u5c0f\u8f66\u7a81\u7136\u65ad\u7535\u79bb\u7ebf&#xff0c;\u76d1\u63a7\u7cfb\u7edf\u5374\u8fdf\u8fdf\u672a\u6536\u5230\u544a\u8b66&#xff0c;\u5bfc\u81f4\u540e\u7eed\u5de5\u5e8f\u56e0\u7269\u6599\u77ed\u7f3a\u505c\u6ede&#xff1b;\u5bb6\u5ead\u4e2d\u7684\u667a\u80fd\u95e8\u9501\u7535\u6c60\u8017\u5c3d&#xff0c;\u7528\u6237\u901a\u8fc7APP\u67e5\u770b\u65f6\u4ecd\u663e\u793a\u201c\u5728\u7ebf\u201d&#xff0c;\u53cd\u590d\u53d1\u9001\u5f00\u9501\u6307\u4ee4\u5374\u6beb\u65e0\u54cd\u5e94\u2026\u2026\u8fd9\u4e9b\u95ee\u9898\u7684\u6839\u6e90&#xff0c;\u5f80\u5f80\u5728\u4e8e\u5bf9MQTT\u534f\u8bae\u4e2d\u9057\u5631\u6d88\u606f&#xff08;LWT&#xff09; \u4e0e\u4fdd\u7559\u6d88\u606f\u7684\u7406\u89e3\u4e0d\u8db3\u3002<\/p>\n<p>\u8fd9\u4e24\u79cd\u673a\u5236\u770b\u4f3c\u7b80\u5355&#xff0c;\u5374\u627f\u8f7d\u7740\u7269\u8054\u7f51\u8bbe\u5907\u201c\u72b6\u6001\u53ef\u89c1\u6027\u201d\u7684\u6838\u5fc3\u9700\u6c42\u3002\u672c\u6587\u5c06\u4ece\u5e95\u5c42\u903b\u8f91\u51fa\u53d1&#xff0c;\u8be6\u89e3\u5176\u5de5\u4f5c\u539f\u7406\u3001\u5b9e\u6218\u914d\u7f6e\u6280\u5de7&#xff0c;\u5e76\u901a\u8fc7\u771f\u5b9e\u6848\u4f8b\u63ed\u793a\u90a3\u4e9b\u201c\u770b\u4f3c\u6b63\u786e\u5374\u9690\u85cf\u98ce\u9669\u201d\u7684\u9677\u9631&#xff0c;\u5e2e\u52a9\u5f00\u53d1\u8005\u6784\u5efa\u771f\u6b63\u53ef\u9760\u7684\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u4f53\u7cfb\u3002<\/p>\n<hr \/>\n<h4>\u4e00\u3001\u6838\u5fc3\u6982\u5ff5\u89e3\u6790&#xff1a;LWT\u4e0e\u4fdd\u7559\u6d88\u606f\u7684\u672c\u8d28\u533a\u522b<\/h4>\n<p>LWT\u4e0e\u4fdd\u7559\u6d88\u606f\u5e38\u88ab\u6df7\u6dc6&#xff0c;\u4f46\u4e8c\u8005\u7684\u8bbe\u8ba1\u76ee\u6807\u622a\u7136\u4e0d\u540c&#xff1a;LWT\u5173\u6ce8\u201c\u8bbe\u5907\u5f02\u5e38\u79bb\u7ebf\u7684\u88ab\u52a8\u901a\u77e5\u201d&#xff0c;\u4fdd\u7559\u6d88\u606f\u5219\u4e13\u6ce8\u201c\u4e3b\u9898\u6700\u65b0\u72b6\u6001\u7684\u4e3b\u52a8\u7f13\u5b58\u201d\u3002\u7406\u89e3\u8fd9\u4e00\u672c\u8d28\u5dee\u5f02&#xff0c;\u662f\u6b63\u786e\u5e94\u7528\u7684\u524d\u63d0\u3002<\/p>\n<h5>1.1 \u9057\u5631\u6d88\u606f&#xff08;Last Will and Testament&#xff09;&#xff1a;\u8bbe\u5907\u7684\u201c\u4e34\u7ec8\u9057\u8a00\u201d<\/h5>\n<p>LWT\u662f\u8bbe\u5907\u5728\u8fde\u63a5\u65f6\u9884\u5148\u5411Broker\u767b\u8bb0\u7684\u201c\u5f02\u5e38\u79bb\u7ebf\u901a\u77e5\u201d&#xff0c;\u76f8\u5f53\u4e8e\u7ed9Broker\u7559\u4e0b\u7684\u201c\u9057\u5631\u201d\u2014\u2014\u82e5\u8bbe\u5907\u610f\u5916\u65ad\u5f00\u8fde\u63a5&#xff0c;Broker\u5c06\u4ee3\u4e3a\u53d1\u5e03\u8fd9\u6761\u6d88\u606f&#xff0c;\u544a\u77e5\u5176\u4ed6\u8ba2\u9605\u8005\u201c\u8bbe\u5907\u51fa\u95ee\u9898\u4e86\u201d\u3002<\/p>\n<ul>\n<li>\n<p>\u89e6\u53d1\u6761\u4ef6&#xff1a;\u4ec5\u5728\u8bbe\u5907\u5f02\u5e38\u65ad\u5f00\u65f6\u751f\u6548\u3002\u5177\u4f53\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\u7f51\u7edc\u4e2d\u65ad\u5bfc\u81f4TCP\u8fde\u63a5\u5f02\u5e38\u5173\u95ed&#xff08;\u5982\u7f51\u7ebf\u88ab\u62d4\u3001\u4fe1\u53f7\u4e22\u5931&#xff09;&#xff1b;<\/li>\n<li>\u8bbe\u5907\u65ad\u7535\u3001\u6b7b\u673a\u7b49\u786c\u4ef6\u6545\u969c&#xff1b;<\/li>\n<li>\u8d85\u8fc7KeepAlive\u65f6\u95f4\u672a\u53d1\u9001PINGREQ&#xff08;Broker\u5224\u5b9a\u8bbe\u5907\u65e0\u54cd\u5e94&#xff09;&#xff1b;<\/li>\n<li>\u56e0\u534f\u8bae\u9519\u8bef\u88abBroker\u4e3b\u52a8\u65ad\u5f00&#xff08;\u5982\u53d1\u9001\u975e\u6cd5\u62a5\u6587&#xff09;\u3002<br \/>\n\u6ce8\u610f&#xff1a;\u82e5\u8bbe\u5907\u6b63\u5e38\u8c03\u7528disconnect()\u65ad\u5f00\u8fde\u63a5&#xff0c;LWT\u4f1a\u88abBroker\u7acb\u5373\u5220\u9664&#xff0c;\u4e0d\u4f1a\u53d1\u5e03\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u914d\u7f6e\u65b9\u5f0f&#xff1a;\u5728CONNECT\u62a5\u6587\u65f6\u901a\u8fc7will_set\u58f0\u660e&#xff0c;\u6838\u5fc3\u53c2\u6570\u5305\u62ec\u4e3b\u9898\u3001 payload\u3001QoS\u548c\u4fdd\u7559\u6807\u5fd7&#xff1a;<\/p>\n<p><span class=\"token comment\"># Paho-MQTT\u5ba2\u6237\u7aef\u914d\u7f6e\u793a\u4f8b<\/span><br \/>\nclient <span class=\"token operator\">&#061;<\/span> mqtt<span class=\"token punctuation\">.<\/span>Client<span class=\"token punctuation\">(<\/span>client_id<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;agv_1001&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u767b\u8bb0\u9057\u5631&#xff1a;\u5f02\u5e38\u79bb\u7ebf\u65f6\u53d1\u5e03&#034;offline&#034;\u5230&#034;device\/agv_1001\/status&#034;<\/span><br \/>\nclient<span class=\"token punctuation\">.<\/span>will_set<span class=\"token punctuation\">(<\/span><br \/>\n    topic<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;device\/agv_1001\/status&#034;<\/span><span class=\"token punctuation\">,<\/span>  <span class=\"token comment\"># \u4e13\u7528\u72b6\u6001\u4e3b\u9898<\/span><br \/>\n    payload<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;{&#034;status&#034;:&#034;offline&#034;,&#034;error&#034;:&#034;unexpected_disconnect&#034;}&#039;<\/span><span class=\"token punctuation\">,<\/span>  <span class=\"token comment\"># \u643a\u5e26\u6545\u969c\u4fe1\u606f<\/span><br \/>\n    qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>  <span class=\"token comment\"># \u786e\u4fdd\u6d88\u606f\u5fc5\u8fbe<\/span><br \/>\n    retain<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span>  <span class=\"token comment\"># \u8bbe\u4e3a\u4fdd\u7559\u6d88\u606f&#xff0c;\u65b0\u8ba2\u9605\u8005\u80fd\u7acb\u5373\u770b\u5230\u79bb\u7ebf\u72b6\u6001<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><br \/>\nclient<span class=\"token punctuation\">.<\/span>connect<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;broker.example.com&#034;<\/span><span class=\"token punctuation\">,<\/span> keepalive<span class=\"token operator\">&#061;<\/span><span class=\"token number\">60<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5fc3\u8df3\u95f4\u969460\u79d2<\/span><\/p>\n<\/li>\n<li>\n<p>\u751f\u547d\u5468\u671f&#xff1a;<\/p>\n<ul>\n<li>\u8bbe\u5907\u6b63\u5e38\u5728\u7ebf\u65f6&#xff0c;LWT\u4ec5\u5b58\u50a8\u5728Broker\u4e2d&#xff0c;\u4e0d\u53d1\u5e03&#xff1b;<\/li>\n<li>\u5f02\u5e38\u65ad\u5f00\u540e&#xff0c;Broker\u7acb\u5373\u53d1\u5e03LWT&#xff08;\u82e5\u914d\u7f6eWillDelayInterval&#xff0c;\u5219\u5ef6\u8fdf\u53d1\u5e03&#xff09;&#xff1b;<\/li>\n<li>\u8bbe\u5907\u91cd\u8fde\u540e&#xff0c;\u9700\u91cd\u65b0\u767b\u8bb0LWT&#xff08;\u65e7LWT\u5df2\u5931\u6548&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>1.2 \u4fdd\u7559\u6d88\u606f&#xff08;Retained Message&#xff09;&#xff1a;\u4e3b\u9898\u7684\u201c\u6700\u65b0\u5feb\u7167\u201d<\/h5>\n<p>\u4fdd\u7559\u6d88\u606f\u662fBroker\u4e3a\u4e3b\u9898\u5b58\u50a8\u7684\u201c\u6700\u65b0\u72b6\u6001\u526f\u672c\u201d&#xff0c;\u5f53\u65b0\u8bbe\u5907\u8ba2\u9605\u8be5\u4e3b\u9898\u65f6&#xff0c;Broker\u4f1a\u7acb\u5373\u63a8\u9001\u8fd9\u6761\u6d88\u606f&#xff0c;\u65e0\u9700\u7b49\u5f85\u53d1\u5e03\u8005\u518d\u6b21\u53d1\u9001\u3002\u5176\u6838\u5fc3\u4ef7\u503c\u662f\u201c\u6d88\u9664\u65b0\u8ba2\u9605\u8005\u7684\u7b49\u5f85\u5ef6\u8fdf\u201d\u3002<\/p>\n<ul>\n<li>\n<p>\u6838\u5fc3\u4f5c\u7528&#xff1a;\u7f13\u5b58\u4e3b\u9898\u7684\u6700\u65b0\u72b6\u6001&#xff0c;\u89e3\u51b3\u201c\u65b0\u8ba2\u9605\u8005\u9519\u8fc7\u5386\u53f2\u6d88\u606f\u201d\u7684\u95ee\u9898\u3002\u4f8b\u5982&#xff1a;\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668\u6bcf10\u5206\u949f\u4e0a\u62a5\u4e00\u6b21\u6570\u636e&#xff0c;\u65b0\u63a5\u5165\u7684\u76d1\u63a7\u5e73\u53f0\u65e0\u9700\u7b49\u5f8510\u5206\u949f&#xff0c;\u8ba2\u9605\u540e\u7acb\u5373\u83b7\u53d6\u6700\u65b0\u8bfb\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u5de5\u4f5c\u6d41\u7a0b&#xff1a;<\/p>\n<p>graph LR<br \/>\n  A[\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668] &#8212; PUBLISH(retain&#061;true) &#8211;&gt; B(Broker)<br \/>\n  B &#8212; \u66ff\u6362\u65e7\u4fdd\u7559\u6d88\u606f&#xff0c;\u5b58\u50a8\u6700\u65b0\u503c &#8211;&gt; C[\u6301\u4e45\u5316\u5b58\u50a8]<br \/>\n  D[\u65b0\u76d1\u63a7\u5e73\u53f0] &#8212; SUBSCRIBE &#8211;&gt; B<br \/>\n  B &#8212; \u7acb\u5373\u63a8\u9001\u4fdd\u7559\u6d88\u606f(\u5f53\u524d\u6e29\u5ea625\u2103) &#8211;&gt; D<\/p>\n<\/li>\n<li>\n<p>\u5173\u952e\u7279\u6027&#xff1a;<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u4e3b\u9898\u4ec5\u4fdd\u7559\u6700\u540e\u4e00\u6761\u8bbe\u7f6eretain&#061;true\u7684\u6d88\u606f&#xff08;\u65b0\u6d88\u606f\u4f1a\u8986\u76d6\u65e7\u6d88\u606f&#xff09;&#xff1b;<\/li>\n<li>\u82e5\u53d1\u5e03payload&#061;null\u4e14retain&#061;true\u7684\u6d88\u606f&#xff0c;Broker\u4f1a\u5220\u9664\u8be5\u4e3b\u9898\u7684\u4fdd\u7559\u6d88\u606f&#xff1b;<\/li>\n<li>\u4fdd\u7559\u6d88\u606f\u72ec\u7acb\u4e8e\u4f1a\u8bdd\u5b58\u5728&#xff08;\u5373\u4f7f\u6240\u6709\u8ba2\u9605\u8005\u79bb\u7ebf&#xff0c;\u4fdd\u7559\u6d88\u606f\u4ecd\u4f1a\u5b58\u50a8&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<table>\n<tr>\u5bf9\u6bd4\u7ef4\u5ea6\u9057\u5631\u6d88\u606f&#xff08;LWT&#xff09;\u4fdd\u7559\u6d88\u606f<\/tr>\n<tbody>\n<tr>\n<td>\u8bbe\u8ba1\u76ee\u6807<\/td>\n<td>\u5f02\u5e38\u79bb\u7ebf\u901a\u77e5<\/td>\n<td>\u4e3b\u9898\u6700\u65b0\u72b6\u6001\u7f13\u5b58<\/td>\n<\/tr>\n<tr>\n<td>\u53d1\u5e03\u65f6\u673a<\/td>\n<td>\u8bbe\u5907\u5f02\u5e38\u65ad\u5f00\u65f6&#xff08;Broker\u4ee3\u53d1&#xff09;<\/td>\n<td>\u53d1\u5e03\u8005\u4e3b\u52a8\u53d1\u9001\u65f6<\/td>\n<\/tr>\n<tr>\n<td>\u751f\u547d\u5468\u671f<\/td>\n<td>\u5f02\u5e38\u53d1\u5e03\u540e\u5931\u6548\/\u6b63\u5e38\u65ad\u5f00\u5220\u9664<\/td>\n<td>\u88ab\u65b0\u4fdd\u7559\u6d88\u606f\u8986\u76d6\u6216\u624b\u52a8\u5220\u9664<\/td>\n<\/tr>\n<tr>\n<td>\u4f9d\u8d56\u4f1a\u8bdd<\/td>\n<td>\u4f9d\u8d56&#xff08;\u8bbe\u5907\u91cd\u8fde\u9700\u91cd\u65b0\u914d\u7f6e&#xff09;<\/td>\n<td>\u4e0d\u4f9d\u8d56&#xff08;\u72ec\u7acb\u5b58\u50a8&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h4>\u4e8c\u3001\u8bbe\u5907\u5f02\u5e38\u4e0b\u7ebf\u573a\u666f\u7684LWT\u5b9e\u6218\u5e94\u7528<\/h4>\n<p>LWT\u662f\u76d1\u63a7\u8bbe\u5907\u201c\u5065\u5eb7\u72b6\u6001\u201d\u7684\u6838\u5fc3\u673a\u5236&#xff0c;\u4f46\u914d\u7f6e\u4e0d\u5f53\u4f1a\u5bfc\u81f4\u201c\u6f0f\u62a5\u201d\u201c\u8bef\u62a5\u201d\u6216\u201c\u72b6\u6001\u6df7\u4e71\u201d\u3002\u4ee5\u4e0b\u7ed3\u5408\u771f\u5b9e\u573a\u666f\u8be6\u89e3\u5176\u6b63\u786e\u7528\u6cd5\u3002<\/p>\n<h5>2.1 \u5178\u578b\u5f02\u5e38\u573a\u666f\u4e0eLWT\u89e6\u53d1\u903b\u8f91<\/h5>\n<p>\u4e0d\u540c\u5f02\u5e38\u573a\u666f\u4e0b&#xff0c;LWT\u7684\u89e6\u53d1\u65f6\u673a\u548c\u6548\u679c\u5b58\u5728\u7ec6\u5fae\u5dee\u5f02&#xff0c;\u9700\u9488\u5bf9\u6027\u914d\u7f6e&#xff1a;<\/p>\n<table>\n<tr>\u5f02\u5e38\u7c7b\u578b\u89e6\u53d1\u673a\u5236\u4e1a\u52a1\u5f71\u54cdLWT\u914d\u7f6e\u5efa\u8bae<\/tr>\n<tbody>\n<tr>\n<td>\u7f51\u7edc\u95ea\u65ad&#xff08;\u59824G\u5207\u6362&#xff09;<\/td>\n<td>KeepAlive\u8d85\u65f6&#xff08;\u9ed8\u8ba41.5\u500d\u95f4\u9694&#xff09;<\/td>\n<td>\u77ed\u65f6\u79bb\u7ebf&#xff0c;\u53ef\u80fd\u81ea\u52a8\u6062\u590d<\/td>\n<td>\u8bbe\u7f6eWillDelayInterval&#061;30&#xff08;\u5ef6\u8fdf30\u79d2\u53d1\u5e03&#xff0c;\u907f\u514d\u95ea\u65ad\u8bef\u62a5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u8bbe\u5907\u65ad\u7535\/\u6b7b\u673a<\/td>\n<td>TCP\u8fde\u63a5\u7a81\u7136\u4e2d\u65ad<\/td>\n<td>\u9700\u4eba\u5de5\u5e72\u9884\u6062\u590d<\/td>\n<td>QoS&#061;1&#043;retain&#061;true&#xff08;\u786e\u4fdd\u544a\u8b66\u5fc5\u8fbe\u4e14\u65b0\u8ba2\u9605\u8005\u53ef\u89c1&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u534f\u8bae\u9519\u8bef\u88abBroker\u65ad\u5f00<\/td>\n<td>Broker\u53d1\u9001DISCONNECT\u540e\u5173\u95ed\u8fde\u63a5<\/td>\n<td>\u53ef\u80fd\u662f\u5ba2\u6237\u7aefbug\u5bfc\u81f4<\/td>\n<td>payload\u643a\u5e26\u9519\u8bef\u7801&#xff08;\u5982{&#034;error&#034;:&#034;invalid_packet&#034;}&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>2.2 \u667a\u80fd\u5bb6\u5c45\u6e29\u63a7\u5668\u7684LWT\u914d\u7f6e\u6848\u4f8b<\/h5>\n<p>\u67d0\u667a\u80fd\u5bb6\u5c45\u7cfb\u7edf\u4e2d&#xff0c;\u6e29\u63a7\u5668\u9700\u5728\u5f02\u5e38\u79bb\u7ebf\u65f6\u53ca\u65f6\u901a\u77e5\u7528\u6237&#xff0c;\u4e14\u4fdd\u7559\u6700\u540e\u72b6\u6001\u4fbf\u4e8e\u6392\u67e5\u95ee\u9898\u3002\u5176LWT\u914d\u7f6e\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u6e29\u63a7\u5668\u8fde\u63a5\u4ee3\u7801<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">connect_thermostat<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    client <span class=\"token operator\">&#061;<\/span> mqtt<span class=\"token punctuation\">.<\/span>Client<span class=\"token punctuation\">(<\/span>client_id<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;thermo_room201&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u767b\u8bb0LWT&#xff1a;\u5305\u542b\u6700\u540e\u6e29\u5ea6\u548c\u79bb\u7ebf\u539f\u56e0<\/span><br \/>\n    last_temp <span class=\"token operator\">&#061;<\/span> read_current_temp<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u83b7\u53d6\u79bb\u7ebf\u524d\u6e29\u5ea6<\/span><br \/>\n    lwt_payload <span class=\"token operator\">&#061;<\/span> json<span class=\"token punctuation\">.<\/span>dumps<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token string\">&#034;status&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;offline&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token string\">&#034;last_temp&#034;<\/span><span class=\"token punctuation\">:<\/span> last_temp<span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token string\">&#034;timestamp&#034;<\/span><span class=\"token punctuation\">:<\/span> time<span class=\"token punctuation\">.<\/span>time<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    client<span class=\"token punctuation\">.<\/span>will_set<span class=\"token punctuation\">(<\/span><br \/>\n        topic<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;home\/thermo\/room201\/status&#034;<\/span><span class=\"token punctuation\">,<\/span>  <span class=\"token comment\"># \u4e13\u7528\u72b6\u6001\u4e3b\u9898<\/span><br \/>\n        payload<span class=\"token operator\">&#061;<\/span>lwt_payload<span class=\"token punctuation\">,<\/span><br \/>\n        qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>  <span class=\"token comment\"># \u786e\u4fdd\u6d88\u606f\u4e0d\u4e22\u5931<\/span><br \/>\n        retain<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span>  <span class=\"token comment\"># \u4fdd\u7559\u79bb\u7ebf\u72b6\u6001&#xff0c;\u65b0\u8ba2\u9605\u8005\u4e0a\u7ebf\u5373\u89c1<\/span><br \/>\n    <span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u8bbe\u7f6eKeepAlive&#061;60\u79d2&#xff08;1\u5206\u949f\u65e0\u54cd\u5e94\u5219\u5224\u5b9a\u79bb\u7ebf&#xff09;<\/span><br \/>\n    client<span class=\"token punctuation\">.<\/span>connect<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;iot-broker.local&#034;<\/span><span class=\"token punctuation\">,<\/span> keepalive<span class=\"token operator\">&#061;<\/span><span class=\"token number\">60<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> client<\/p>\n<p>\u6548\u679c\u89e3\u6790&#xff1a;<\/p>\n<ul>\n<li>\u5f53\u6e29\u63a7\u5668\u56e0\u7535\u6c60\u8017\u5c3d\u65ad\u7535\u65f6&#xff0c;Broker\u57281.5\u00d760&#061;90\u79d2\u540e\u5224\u5b9a\u79bb\u7ebf&#xff0c;\u53d1\u5e03LWT&#xff1b;<\/li>\n<li>\u624b\u673aAPP\u8ba2\u9605home\/thermo\/room201\/status\u540e&#xff0c;\u7acb\u5373\u6536\u5230\u79bb\u7ebf\u901a\u77e5\u53ca\u6700\u540e\u6e29\u5ea6&#xff1b;<\/li>\n<li>\u7ef4\u4fee\u4eba\u5458\u53ef\u6839\u636elast_temp\u5224\u65ad\u662f\u5426\u56e0\u4f4e\u6e29\u4fdd\u62a4\u5bfc\u81f4\u8bbe\u5907\u79bb\u7ebf\u3002<\/li>\n<\/ul>\n<h5>2.3 LWT\u7684\u4e09\u5927\u7ecf\u5178\u9677\u9631\u4e0e\u89c4\u907f\u65b9\u6848<\/h5>\n<h6>\u9677\u96311&#xff1a;LWT\u4e0e\u666e\u901a\u72b6\u6001\u6d88\u606f\u4e3b\u9898\u51b2\u7a81<\/h6>\n<p>\u67d0\u56e2\u961f\u5c06\u8bbe\u5907\u7684\u5728\u7ebf\u72b6\u6001&#xff08;online&#xff09;\u548cLWT&#xff08;offline&#xff09;\u90fd\u53d1\u5e03\u5230\u540c\u4e00\u4e3b\u9898device\/status&#xff0c;\u4e14\u5747\u8bbe\u7f6eretain&#061;true\u3002\u7ed3\u679c\u8bbe\u5907\u6b63\u5e38\u5728\u7ebf\u65f6&#xff0c;online\u6d88\u606f\u8986\u76d6\u4e86LWT\u7684offline&#xff1b;\u4f46\u5f53\u8bbe\u5907\u5f02\u5e38\u79bb\u7ebf\u65f6&#xff0c;LWT\u53d1\u5e03\u7684offline\u53c8\u4f1a\u88ab\u540e\u7eed\u4e0a\u7ebf\u7684\u6b63\u5e38\u72b6\u6001\u8986\u76d6\u2014\u2014\u5bfc\u81f4\u76d1\u63a7\u7cfb\u7edf\u65f6\u800c\u663e\u793a\u79bb\u7ebf&#xff0c;\u65f6\u800c\u663e\u793a\u5728\u7ebf&#xff0c;\u72b6\u6001\u6df7\u4e71\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4e3b\u9898\u5206\u79bb\u7b56\u7565<br \/>\n\u4e3aLWT\u548c\u6b63\u5e38\u72b6\u6001\u5206\u914d\u72ec\u7acb\u4e3b\u9898&#xff0c;\u907f\u514d\u76f8\u4e92\u8986\u76d6&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u6b63\u786e\u4e3b\u9898\u8bbe\u8ba1<\/span><br \/>\nnormal_topic <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;device\/sensor1\/state&#034;<\/span>  <span class=\"token comment\"># \u53d1\u5e03\u5728\u7ebf\u72b6\u6001&#xff08;\u5982&#034;online&#034;&#xff09;<\/span><br \/>\nlwt_topic <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;device\/sensor1\/lwt&#034;<\/span>       <span class=\"token comment\"># \u4ec5\u7528\u4e8eLWT&#xff08;\u5982&#034;offline&#034;&#xff09;<\/span><\/p>\n<p><span class=\"token comment\"># \u6b63\u5e38\u72b6\u6001\u53d1\u5e03<\/span><br \/>\nclient<span class=\"token punctuation\">.<\/span>publish<span class=\"token punctuation\">(<\/span>normal_topic<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;online&#034;<\/span><span class=\"token punctuation\">,<\/span> qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> retain<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># LWT\u914d\u7f6e<\/span><br \/>\nclient<span class=\"token punctuation\">.<\/span>will_set<span class=\"token punctuation\">(<\/span>lwt_topic<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;offline&#034;<\/span><span class=\"token punctuation\">,<\/span> qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> retain<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h6>\u9677\u96312&#xff1a;LWT\u672a\u8bbe\u7f6eQoS&#061;1\u5bfc\u81f4\u544a\u8b66\u4e22\u5931<\/h6>\n<p>\u67d0\u5de5\u4e1a\u8bbe\u5907\u7684LWT\u4f7f\u7528QoS&#061;0&#xff0c;\u5f53\u7f51\u7edc\u6ce2\u52a8\u65f6&#xff0c;Broker\u53d1\u5e03\u7684LWT\u6d88\u606f\u53ef\u80fd\u4e22\u5931&#xff0c;\u76d1\u63a7\u7cfb\u7edf\u672a\u6536\u5230\u79bb\u7ebf\u544a\u8b66&#xff0c;\u5bfc\u81f4\u8bbe\u5907\u79bb\u7ebf\u6570\u5c0f\u65f6\u672a\u88ab\u53d1\u73b0\u3002<\/p>\n<p>\u539f\u7406&#xff1a;LWT\u7684\u53d1\u5e03\u7531Broker\u6267\u884c&#xff0c;\u82e5QoS&#061;0&#xff0c;\u6d88\u606f\u53ef\u80fd\u5728\u4f20\u8f93\u4e2d\u4e22\u5931&#xff08;\u5c24\u5176\u5728\u5f31\u7f51\u73af\u5883&#xff09;\u3002\u800c\u8bbe\u5907\u5df2\u79bb\u7ebf&#xff0c;\u65e0\u6cd5\u91cd\u4f20\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;LWT\u5fc5\u987b\u4f7f\u7528QoS\u22651&#xff0c;\u786e\u4fdd\u544a\u8b66\u53ef\u9760\u9001\u8fbe&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u9519\u8bef\u914d\u7f6e<\/span><br \/>\nclient<span class=\"token punctuation\">.<\/span>will_set<span class=\"token punctuation\">(<\/span>topic<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;alarm&#034;<\/span><span class=\"token punctuation\">,<\/span> payload<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;offline&#034;<\/span><span class=\"token punctuation\">,<\/span> qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u98ce\u9669&#xff1a;\u53ef\u80fd\u4e22\u5931<\/span><\/p>\n<p><span class=\"token comment\"># \u6b63\u786e\u914d\u7f6e<\/span><br \/>\nclient<span class=\"token punctuation\">.<\/span>will_set<span class=\"token punctuation\">(<\/span>topic<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;alarm&#034;<\/span><span class=\"token punctuation\">,<\/span> payload<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;offline&#034;<\/span><span class=\"token punctuation\">,<\/span> qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u786e\u4fddBroker\u6536\u5230\u786e\u8ba4<\/span><\/p>\n<h6>\u9677\u96313&#xff1a;\u5ffd\u7565MQTT 5.0\u7684WillDelayInterval\u7279\u6027<\/h6>\n<p>\u5728\u7f51\u7edc\u4e0d\u7a33\u5b9a\u7684\u573a\u666f&#xff08;\u5982\u6237\u5916\u4f20\u611f\u5668&#xff09;&#xff0c;\u8bbe\u5907\u53ef\u80fd\u56e0\u77ed\u6682\u4fe1\u53f7\u4e22\u5931\u79bb\u7ebf&#xff0c;\u51e0\u79d2\u540e\u81ea\u52a8\u91cd\u8fde\u3002\u82e5\u7acb\u5373\u53d1\u5e03LWT&#xff0c;\u4f1a\u5bfc\u81f4\u76d1\u63a7\u7cfb\u7edf\u9891\u7e41\u8bef\u62a5\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u5229\u7528MQTT 5.0\u7684WillDelayInterval\u8bbe\u7f6e\u5ef6\u8fdf\u53d1\u5e03\u65f6\u95f4&#xff0c;\u8fc7\u6ee4\u77ed\u65f6\u79bb\u7ebf&#xff1a;<\/p>\n<p><span class=\"token comment\"># Paho-MQTT 5.0\u793a\u4f8b&#xff1a;\u5ef6\u8fdf30\u79d2\u53d1\u5e03LWT<\/span><br \/>\nclient <span class=\"token operator\">&#061;<\/span> mqtt<span class=\"token punctuation\">.<\/span>Client<span class=\"token punctuation\">(<\/span>client_id<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;outdoor_sensor&#034;<\/span><span class=\"token punctuation\">,<\/span> protocol<span class=\"token operator\">&#061;<\/span>mqtt<span class=\"token punctuation\">.<\/span>MQTTv5<span class=\"token punctuation\">)<\/span><br \/>\nclient<span class=\"token punctuation\">.<\/span>connect<span class=\"token punctuation\">(<\/span><br \/>\n    host<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;broker.example.com&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    keepalive<span class=\"token operator\">&#061;<\/span><span class=\"token number\">60<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    will_delay_interval<span class=\"token operator\">&#061;<\/span><span class=\"token number\">30<\/span>  <span class=\"token comment\"># \u8bbe\u5907\u79bb\u7ebf\u540e&#xff0c;30\u79d2\u5185\u91cd\u8fde\u5219\u4e0d\u53d1\u5e03LWT<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><br \/>\nclient<span class=\"token punctuation\">.<\/span>will_set<span class=\"token punctuation\">(<\/span>topic<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;sensor\/status&#034;<\/span><span class=\"token punctuation\">,<\/span> payload<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;offline&#034;<\/span><span class=\"token punctuation\">,<\/span> qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<hr \/>\n<h4>\u4e09\u3001\u4fdd\u7559\u6d88\u606f\u5b9e\u73b0\u8bbe\u5907\u72b6\u6001\u7f13\u5b58\u7684\u9ad8\u7ea7\u6280\u5de7<\/h4>\n<p>\u4fdd\u7559\u6d88\u606f\u7684\u6838\u5fc3\u4ef7\u503c\u662f\u201c\u8ba9\u65b0\u8ba2\u9605\u8005\u5feb\u901f\u83b7\u53d6\u6700\u65b0\u72b6\u6001\u201d&#xff0c;\u4f46\u6ee5\u7528\u4f1a\u5bfc\u81f4\u201c\u810f\u6570\u636e\u6b8b\u7559\u201d\u201c\u72b6\u6001\u51b2\u7a81\u201d\u7b49\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u5b9e\u6218\u4e2d\u7684\u4f18\u5316\u65b9\u6848\u3002<\/p>\n<h5>3.1 \u519c\u4e1a\u5927\u68da\u4f20\u611f\u5668\u7684\u4fdd\u7559\u6d88\u606f\u5e94\u7528<\/h5>\n<p>\u67d0\u519c\u4e1a\u5927\u68da\u7684\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668\u6bcf5\u5206\u949f\u4e0a\u62a5\u4e00\u6b21\u6570\u636e&#xff0c;\u65b0\u63a5\u5165\u7684\u76d1\u63a7\u5e73\u53f0\u9700\u7b49\u5f855\u5206\u949f\u624d\u80fd\u83b7\u53d6\u9996\u6761\u6570\u636e&#xff0c;\u5f71\u54cd\u5b9e\u65f6\u51b3\u7b56\u3002\u901a\u8fc7\u4fdd\u7559\u6d88\u606f\u53ef\u5f7b\u5e95\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u4f20\u611f\u5668\u6570\u636e\u4e0a\u62a5\u4ee3\u7801<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">publish_sensor_data<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    temp <span class=\"token operator\">&#061;<\/span> read_temperature<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8bfb\u53d6\u5f53\u524d\u6e29\u5ea6<\/span><br \/>\n    humidity <span class=\"token operator\">&#061;<\/span> read_humidity<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    payload <span class=\"token operator\">&#061;<\/span> json<span class=\"token punctuation\">.<\/span>dumps<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token string\">&#034;temp&#034;<\/span><span class=\"token punctuation\">:<\/span> temp<span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token string\">&#034;humidity&#034;<\/span><span class=\"token punctuation\">:<\/span> humidity<span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token string\">&#034;timestamp&#034;<\/span><span class=\"token punctuation\">:<\/span> time<span class=\"token punctuation\">.<\/span>time<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u52a0\u5165\u65f6\u95f4\u6233&#xff0c;\u4fbf\u4e8e\u5224\u65ad\u6570\u636e\u65b0\u9c9c\u5ea6<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u53d1\u5e03\u65f6\u8bbe\u7f6eretain&#061;True&#xff0c;Broker\u5b58\u50a8\u6700\u65b0\u503c<\/span><br \/>\n    client<span class=\"token punctuation\">.<\/span>publish<span class=\"token punctuation\">(<\/span><br \/>\n        topic<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;farm\/greenhouse1\/env&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        payload<span class=\"token operator\">&#061;<\/span>payload<span class=\"token punctuation\">,<\/span><br \/>\n        qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        retain<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><br \/>\n    <span class=\"token punctuation\">)<\/span><\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<ul>\n<li>\u65b0\u76d1\u63a7\u5e73\u53f0\u8ba2\u9605farm\/greenhouse1\/env\u540e&#xff0c;\u7acb\u5373\u6536\u5230Broker\u63a8\u9001\u7684\u6700\u65b0\u6e29\u6e7f\u5ea6\u6570\u636e&#xff1b;<\/li>\n<li>\u5373\u4f7f\u4f20\u611f\u5668\u6682\u65f6\u79bb\u7ebf&#xff0c;\u5e73\u53f0\u4ecd\u80fd\u67e5\u770b\u6700\u540e\u4e00\u6b21\u4e0a\u62a5\u7684\u72b6\u6001&#xff0c;\u8f85\u52a9\u5224\u65ad\u5927\u68da\u73af\u5883\u8d8b\u52bf\u3002<\/li>\n<\/ul>\n<h5>3.2 \u5de5\u4e1a\u8bbe\u5907\u72b6\u6001\u770b\u677f\u7684\u4fdd\u7559\u6d88\u606f\u4f18\u5316<\/h5>\n<p>\u67d0\u5de5\u5382\u7684\u8bbe\u5907\u72b6\u6001\u770b\u677f\u9700\u8981\u5b9e\u65f6\u5c55\u793a\u6570\u767e\u53f0\u673a\u5668\u7684\u8fd0\u884c\u72b6\u6001&#xff08;\u5982\u201c\u8fd0\u884c\u4e2d\u201d\u201c\u505c\u673a\u201d&#xff09;&#xff0c;\u82e5\u770b\u677f\u91cd\u542f\u540e\u7b49\u5f85\u6240\u6709\u8bbe\u5907\u91cd\u65b0\u4e0a\u62a5\u72b6\u6001&#xff0c;\u4f1a\u5bfc\u81f4\u957f\u65f6\u95f4\u7a7a\u767d\u3002\u901a\u8fc7\u4fdd\u7559\u6d88\u606f\u53ef\u5b9e\u73b0\u201c\u79d2\u7ea7\u52a0\u8f7d\u201d&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u8bbe\u5907\u72b6\u6001\u66f4\u65b0\u903b\u8f91<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">update_machine_state<\/span><span class=\"token punctuation\">(<\/span>machine_id<span class=\"token punctuation\">,<\/span> state<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u8bbe\u5907\u72b6\u6001\u53d8\u5316\u65f6&#xff0c;\u53d1\u5e03\u4fdd\u7559\u6d88\u606f&#034;&#034;&#034;<\/span><br \/>\n    topic <span class=\"token operator\">&#061;<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;factory\/machine\/<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>machine_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\/state&#034;<\/span><\/span><br \/>\n    payload <span class=\"token operator\">&#061;<\/span> json<span class=\"token punctuation\">.<\/span>dumps<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token string\">&#034;state&#034;<\/span><span class=\"token punctuation\">:<\/span> state<span class=\"token punctuation\">,<\/span>  <span class=\"token comment\"># \u5982&#034;running&#034;\u3001&#034;stopped&#034;<\/span><br \/>\n        <span class=\"token string\">&#034;updated_at&#034;<\/span><span class=\"token punctuation\">:<\/span> time<span class=\"token punctuation\">.<\/span>time<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    client<span class=\"token punctuation\">.<\/span>publish<span class=\"token punctuation\">(<\/span>topic<span class=\"token punctuation\">,<\/span> payload<span class=\"token punctuation\">,<\/span> qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> retain<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u770b\u677f\u521d\u59cb\u5316\u903b\u8f91<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">init_dashboard<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u542f\u52a8\u65f6\u8ba2\u9605\u6240\u6709\u8bbe\u5907\u72b6\u6001\u4e3b\u9898&#xff0c;\u7acb\u5373\u83b7\u53d6\u4fdd\u7559\u6d88\u606f&#034;&#034;&#034;<\/span><br \/>\n    client<span class=\"token punctuation\">.<\/span>subscribe<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;factory\/machine\/&#043;\/state&#034;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u901a\u914d\u7b26\u8ba2\u9605\u6240\u6709\u8bbe\u5907<\/span><br \/>\n    <span class=\"token comment\"># \u6536\u5230\u4fdd\u7559\u6d88\u606f\u540e&#xff0c;\u76f4\u63a5\u6e32\u67d3\u5230\u770b\u677f<\/span><br \/>\n    client<span class=\"token punctuation\">.<\/span>on_message <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">lambda<\/span> client<span class=\"token punctuation\">,<\/span> userdata<span class=\"token punctuation\">,<\/span> msg<span class=\"token punctuation\">:<\/span> render_state<span class=\"token punctuation\">(<\/span>msg<span class=\"token punctuation\">)<\/span><\/p>\n<h5>3.3 \u4fdd\u7559\u6d88\u606f\u7684\u4e24\u5927\u81f4\u547d\u9677\u9631\u4e0e\u6e05\u9664\u7b56\u7565<\/h5>\n<h6>\u9677\u96311&#xff1a;\u8bbe\u5907\u62a5\u5e9f\u540e\u4fdd\u7559\u6d88\u606f\u6b8b\u7559&#xff08;\u810f\u6570\u636e&#xff09;<\/h6>\n<p>\u67d0\u667a\u80fd\u6c34\u8868\u62a5\u5e9f\u540e&#xff0c;\u5176\u4fdd\u7559\u6d88\u606f&#xff08;{&#034;status&#034;:&#034;online&#034;}&#xff09;\u4ecd\u5b58\u50a8\u5728Broker\u4e2d\u3002\u65b0\u7528\u6237\u5165\u4f4f\u65f6&#xff0c;APP\u8ba2\u9605\u540e\u770b\u5230\u201c\u5728\u7ebf\u201d\u72b6\u6001&#xff0c;\u53cd\u590d\u53d1\u9001\u6307\u4ee4\u5374\u65e0\u54cd\u5e94&#xff0c;\u5f15\u53d1\u6295\u8bc9\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u8bbe\u5907\u751f\u547d\u5468\u671f\u7ba1\u7406&#043;\u4fdd\u7559\u6d88\u606f\u6e05\u9664<\/p>\n<ul>\n<li>\u8bbe\u5907\u62a5\u5e9f\/\u4e0b\u7ebf\u65f6&#xff0c;\u4e3b\u52a8\u53d1\u9001\u7a7a\u6d88\u606f\u6e05\u9664\u4fdd\u7559\u6d88\u606f&#xff1a;<span class=\"token keyword\">def<\/span> <span class=\"token function\">disable_device<\/span><span class=\"token punctuation\">(<\/span>device_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u8bbe\u5907\u62a5\u5e9f\u65f6\u8c03\u7528&#xff0c;\u6e05\u9664\u4fdd\u7559\u6d88\u606f&#034;&#034;&#034;<\/span><br \/>\n    topic <span class=\"token operator\">&#061;<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;device\/<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>device_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\/state&#034;<\/span><\/span><br \/>\n    <span class=\"token comment\"># \u53d1\u9001\u7a7apayload&#043;retain&#061;true&#xff0c;Broker\u4f1a\u5220\u9664\u8be5\u4e3b\u9898\u7684\u4fdd\u7559\u6d88\u606f<\/span><br \/>\n    client<span class=\"token punctuation\">.<\/span>publish<span class=\"token punctuation\">(<\/span>topic<span class=\"token punctuation\">,<\/span> payload<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">None<\/span><span class=\"token punctuation\">,<\/span> qos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> retain<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<\/li>\n<li>\u76d1\u63a7\u7cfb\u7edf\u5b9a\u671f\u68c0\u67e5\u201c\u957f\u671f\u65e0\u66f4\u65b0\u201d\u7684\u4fdd\u7559\u6d88\u606f&#xff08;\u5982\u8d85\u8fc77\u5929&#xff09;&#xff0c;\u81ea\u52a8\u6e05\u7406&#xff1a;<span class=\"token comment\"># EMQX\u901a\u8fc7API\u67e5\u8be2\u5e76\u5220\u966430\u5929\u672a\u66f4\u65b0\u7684\u4fdd\u7559\u6d88\u606f<\/span><br \/>\n<span class=\"token function\">curl<\/span> -X DELETE <span class=\"token string\">&#034;http:\/\/broker:8081\/api\/v5\/retained_messages&#034;<\/span> <span class=\"token punctuation\">\\\\<\/span><br \/>\n  -H <span class=\"token string\">&#034;Authorization: Basic YWRtaW46YWRtaW4&#061;&#034;<\/span> <span class=\"token punctuation\">\\\\<\/span><br \/>\n  -d <span class=\"token string\">&#039;{&#034;where&#034;: {&#034;last_update_time&#034;: {&#034;lt&#034;: &#034;30d&#034;}}}&#039;<\/span><\/p>\n<\/li>\n<\/ul>\n<h6>\u9677\u96312&#xff1a;\u9ad8\u9891\u6d88\u606f\u6ee5\u7528\u4fdd\u7559\u6d88\u606f\u5bfc\u81f4Broker\u5b58\u50a8\u81a8\u80c0<\/h6>\n<p>\u67d0\u632f\u52a8\u4f20\u611f\u5668\u6bcf\u79d2\u53d1\u5e03\u4e00\u6b21\u6570\u636e&#xff0c;\u4e14\u5168\u90e8\u8bbe\u7f6eretain&#061;true\u3002\u7531\u4e8e\u6bcf\u6761\u6d88\u606f\u90fd\u4f1a\u8986\u76d6\u65e7\u4fdd\u7559\u6d88\u606f&#xff0c;Broker\u7684\u78c1\u76d8IO\u88ab\u9891\u7e41\u5360\u7528&#xff0c;3\u5929\u540e\u56e0\u5b58\u50a8\u538b\u529b\u5d29\u6e83\u3002<\/p>\n<p>\u539f\u7406&#xff1a;\u4fdd\u7559\u6d88\u606f\u7684\u201c\u8986\u76d6\u201d\u64cd\u4f5c\u9700\u8981Broker\u6267\u884c\u78c1\u76d8\u5199\u5165&#xff08;\u6301\u4e45\u5316&#xff09;&#xff0c;\u9ad8\u9891\u66f4\u65b0\u4f1a\u5bfc\u81f4IO\u98ce\u66b4\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u533a\u5206\u201c\u72b6\u6001\u6d88\u606f\u201d\u4e0e\u201c\u6d41\u6570\u636e\u201d&#xff0c;\u4ec5\u5bf9\u72b6\u6001\u6d88\u606f\u4f7f\u7528\u4fdd\u7559\u6d88\u606f&#xff1a;<\/p>\n<ul>\n<li>\u632f\u52a8\u4f20\u611f\u5668\u7684\u5b9e\u65f6\u6ce2\u5f62&#xff08;\u6d41\u6570\u636e&#xff09;&#xff1a;\u4f7f\u7528QoS&#061;0&#xff0c;\u4e0d\u4fdd\u7559&#xff1b;<\/li>\n<li>\u4f20\u611f\u5668\u7684\u8fd0\u884c\u72b6\u6001&#xff08;\u5982\u201c\u6b63\u5e38\u201d\u201c\u5f02\u5e38\u201d&#xff09;&#xff1a;\u6bcf30\u79d2\u53d1\u5e03\u4e00\u6b21&#xff0c;\u8bbe\u7f6eretain&#061;true\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>\u56db\u3001\u53cc\u5251\u5408\u74a7&#xff1a;LWT&#043;\u4fdd\u7559\u6d88\u606f\u7684\u5b8c\u7f8e\u534f\u4f5c<\/h4>\n<p>LWT\u4e0e\u4fdd\u7559\u6d88\u606f\u5e76\u975e\u5b64\u7acb\u5b58\u5728&#xff0c;\u4e8c\u8005\u7ed3\u5408\u53ef\u6784\u5efa\u5b8c\u6574\u7684\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u95ed\u73af\u2014\u2014\u65e2\u5b9e\u65f6\u611f\u77e5\u5f02\u5e38\u79bb\u7ebf&#xff0c;\u53c8\u80fd\u7f13\u5b58\u6700\u65b0\u72b6\u6001&#xff0c;\u6ee1\u8db3\u76d1\u63a7\u7cfb\u7edf\u7684\u201c\u53ef\u89c1\u6027\u201d\u9700\u6c42\u3002<\/p>\n<h5>4.1 \u667a\u80fd\u8f66\u5e93\u95e8\u7684\u72b6\u6001\u76d1\u63a7\u65b9\u6848<\/h5>\n<p>\u67d0\u667a\u80fd\u8f66\u5e93\u95e8\u9700\u8981\u5b9e\u73b0&#xff1a;<\/p>\n<li>\u5b9e\u65f6\u5c55\u793a\u5f00\u5173\u72b6\u6001&#xff08;\u5f00\/\u5173&#xff09;&#xff1b;<\/li>\n<li>\u5f02\u5e38\u79bb\u7ebf\u65f6\u7acb\u5373\u544a\u8b66&#xff1b;<\/li>\n<li>\u65b0APP\u767b\u5f55\u65f6\u80fd\u770b\u5230\u5f53\u524d\u72b6\u6001\u548c\u79bb\u7ebf\u5386\u53f2\u3002<\/li>\n<p>\u4e3b\u9898\u8bbe\u8ba1\u4e0e\u6d88\u606f\u534f\u4f5c&#xff1a;<\/p>\n<table>\n<tr>\u6d88\u606f\u7c7b\u578b\u4e3b\u9898\u5178\u578bpayload\u4fdd\u7559\u6807\u5fd7\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>\u6b63\u5e38\u72b6\u6001<\/td>\n<td>garage\/door1\/state<\/td>\n<td>{&#034;open&#034;: false, &#034;battery&#034;: 80}<\/td>\n<td>true<\/td>\n<td>\u7f13\u5b58\u6700\u65b0\u5f00\u5173\u72b6\u6001\u548c\u7535\u6c60\u7535\u91cf<\/td>\n<\/tr>\n<tr>\n<td>LWT\u9057\u5631\u6d88\u606f<\/td>\n<td>garage\/door1\/alarm<\/td>\n<td>{&#034;status&#034;: &#034;offline&#034;, &#034;cause&#034;: &#034;power_loss&#034;}<\/td>\n<td>true<\/td>\n<td>\u5f02\u5e38\u79bb\u7ebf\u65f6\u89e6\u53d1\u544a\u8b66<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u534f\u4f5c\u6d41\u7a0b&#xff1a;<\/p>\n<li>\u8f66\u5e93\u95e8\u6b63\u5e38\u8fd0\u884c\u65f6&#xff0c;\u6bcf30\u79d2\u5411garage\/door1\/state\u53d1\u5e03\u4fdd\u7559\u6d88\u606f&#xff0c;\u66f4\u65b0\u5f00\u5173\u72b6\u6001\u548c\u7535\u6c60\u7535\u91cf&#xff1b;<\/li>\n<li>\u624b\u673aAPP\u8ba2\u9605\u4e0a\u8ff0\u4e24\u4e2a\u4e3b\u9898&#xff1a;\n<ul>\n<li>\u4ecestate\u4e3b\u9898\u83b7\u53d6\u5f53\u524d\u72b6\u6001&#xff08;\u65b0\u8ba2\u9605\u65f6\u7acb\u5373\u6536\u5230\u4fdd\u7559\u6d88\u606f&#xff09;&#xff1b;<\/li>\n<li>\u4ecealarm\u4e3b\u9898\u76d1\u542c\u5f02\u5e38&#xff08;\u5982\u65ad\u7535\u65f6\u6536\u5230\u79bb\u7ebf\u544a\u8b66&#xff09;&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u82e5\u8f66\u5e93\u95e8\u7a81\u7136\u65ad\u7535&#xff1a;\n<ul>\n<li>Broker\u53d1\u5e03LWT\u5230alarm\u4e3b\u9898&#xff08;\u4fdd\u7559&#xff09;&#xff0c;APP\u5f39\u7a97\u544a\u8b66&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u7ef4\u4fee\u4eba\u5458\u4fee\u590d\u540e&#xff0c;\u8bbe\u5907\u91cd\u8fde&#xff1a;\n<ul>\n<li>\u7acb\u5373\u5411state\u53d1\u5e03{&#034;open&#034;: false, &#034;battery&#034;: 100}&#xff0c;\u8986\u76d6\u65e7\u4fdd\u7559\u6d88\u606f&#xff1b;<\/li>\n<li>\u4e3b\u52a8\u5411alarm\u53d1\u5e03\u7a7a\u6d88\u606f&#xff08;payload&#061;None, retain&#061;True&#xff09;&#xff0c;\u6e05\u9664\u79bb\u7ebf\u544a\u8b66\u3002<\/li>\n<\/ul>\n<\/li>\n<h5>4.2 MQTT 5.0\u7684\u7279\u6b8a\u884c\u4e3a\u4e0e\u5e94\u5bf9<\/h5>\n<p>MQTT 5.0\u5bf9LWT\u548c\u4fdd\u7559\u6d88\u606f\u7684\u4ea4\u4e92\u589e\u52a0\u4e86\u65b0\u7279\u6027&#xff0c;\u9700\u7279\u522b\u6ce8\u610f&#xff1a;<\/p>\n<ul>\n<li>\n<p>LWT\u4fdd\u7559\u6d88\u606f\u7684\u5ef6\u8fdf\u53d1\u5e03\u95ee\u9898&#xff1a;<br \/>\n\u82e5\u8bbe\u7f6eWillDelayInterval&#061;300&#xff08;5\u5206\u949f&#xff09;&#xff0c;\u8bbe\u5907\u6389\u7ebf\u540e&#xff0c;Broker\u4f1a\u7b49\u5f855\u5206\u949f\u518d\u53d1\u5e03LWT\u3002\u671f\u95f4\u65b0\u8ba2\u9605\u8005\u8ba2\u9605alarm\u4e3b\u9898\u65f6&#xff0c;\u770b\u5230\u7684\u4ecd\u662f\u65e7\u7684\u201c\u5728\u7ebf\u201d\u4fdd\u7559\u6d88\u606f&#xff08;\u82e5\u672a\u6e05\u7406&#xff09;&#xff0c;\u5bfc\u81f4\u8bef\u5224\u3002<\/p>\n<p>\u5bf9\u7b56&#xff1a;\u5728\u6b63\u5e38\u72b6\u6001\u6d88\u606f\u4e2d\u52a0\u5165\u65f6\u95f4\u6233&#xff0c;\u8ba2\u9605\u8005\u901a\u8fc7\u65f6\u95f4\u6233\u5224\u65ad\u72b6\u6001\u662f\u5426\u8fc7\u671f&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u6b63\u5e38\u72b6\u6001\u6d88\u606fpayload<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token string\">&#034;status&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;online&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token string\">&#034;last_updated&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token number\">1722883265<\/span>  <span class=\"token comment\">\/\/ Unix\u65f6\u95f4\u6233<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u8ba2\u9605\u8005\u6536\u5230\u6d88\u606f\u540e&#xff0c;\u82e5\u5f53\u524d\u65f6\u95f4 &#8211; last_updated &gt; 300\u79d2&#xff08;5\u5206\u949f&#xff09;&#xff0c;\u5219\u5224\u5b9a\u4e3a\u201c\u7591\u4f3c\u79bb\u7ebf\u201d\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>\u4e94\u3001\u771f\u5b9e\u751f\u4ea7\u73af\u5883\u4e2d\u7684\u8840\u6cea\u6559\u8bad<\/h4>\n<p>\u4ee5\u4e0b\u6848\u4f8b\u5747\u6765\u81ea\u771f\u5b9e\u751f\u4ea7\u4e8b\u6545&#xff0c;\u63ed\u793aLWT\u4e0e\u4fdd\u7559\u6d88\u606f\u914d\u7f6e\u4e0d\u5f53\u7684\u4e25\u91cd\u540e\u679c\u3002<\/p>\n<h5>\u6848\u4f8b1&#xff1a;\u5145\u7535\u6869\u201c\u5e7d\u7075\u6307\u4ee4\u201d\u4e8b\u4ef6<\/h5>\n<ul>\n<li>\u73b0\u8c61&#xff1a;\u7528\u6237\u626b\u7801\u5145\u7535\u65f6&#xff0c;\u6307\u4ee4\u5ef6\u8fdf\u8d85\u8fc730\u79d2&#xff0c;\u90e8\u5206\u6307\u4ee4\u88ab\u5176\u4ed6\u5145\u7535\u6869\u6267\u884c\u3002<\/li>\n<li>\u6839\u56e0&#xff1a;<br \/>\n\u6240\u6709\u5145\u7535\u6869\u4f7f\u7528\u76f8\u540cClientID&#xff08;charger_001&#xff09;&#xff0c;\u4e14LWT\u4e3b\u9898\u4e0e\u63a7\u5236\u6307\u4ee4\u4e3b\u9898\u51b2\u7a81&#xff1a;<\/p>\n<li>\u65b0\u5145\u7535\u6869\u4e0a\u7ebf\u65f6&#xff0c;Broker\u8e22\u6389\u65e7\u8fde\u63a5&#xff0c;\u65e7\u5145\u7535\u6869\u7684LWT\u88ab\u53d1\u5e03&#xff1b;<\/li>\n<li>\u63a7\u5236\u6307\u4ee4\u4e3b\u9898\u4e0eLWT\u4e3b\u9898\u76f8\u540c&#xff0c;\u5bfc\u81f4\u6307\u4ee4\u88abLWT\u6d88\u606f\u8986\u76d6\u6216\u6df7\u6dc6&#xff1b;<\/li>\n<li>\u91cd\u590dClientID\u5bfc\u81f4\u6d88\u606f\u8def\u7531\u6df7\u4e71&#xff0c;\u6307\u4ee4\u88ab\u968f\u673a\u8bbe\u5907\u63a5\u6536\u3002<\/li>\n<\/li>\n<li>\u4fee\u590d&#xff1a;<span class=\"token comment\"># \u4e3a\u6bcf\u4e2a\u5145\u7535\u6869\u751f\u6210\u552f\u4e00ClientID<\/span><br \/>\n<span class=\"token keyword\">import<\/span> uuid<br \/>\nmac_address <span class=\"token operator\">&#061;<\/span> get_device_mac<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u83b7\u53d6\u8bbe\u5907\u552f\u4e00MAC\u5730\u5740<\/span><br \/>\nclient_id <span class=\"token operator\">&#061;<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;charger_<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>mac_address<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">_<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>uuid<span class=\"token punctuation\">.<\/span>uuid4<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token builtin\">hex<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">:<\/span><span class=\"token format-spec\">6]<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><br \/>\n<span class=\"token comment\"># \u5206\u79bb\u6307\u4ee4\u4e0eLWT\u4e3b\u9898<\/span><br \/>\ncmd_topic <span class=\"token operator\">&#061;<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;charger\/<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>client_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\/cmd&#034;<\/span><\/span><br \/>\nlwt_topic <span class=\"token operator\">&#061;<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;charger\/<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>client_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\/alarm&#034;<\/span><\/span><\/p>\n<\/li>\n<\/ul>\n<h5>\u6848\u4f8b2&#xff1a;EMQX\u96c6\u7fa4\u5185\u5b58\u6cc4\u6f0f\u4e8b\u6545<\/h5>\n<ul>\n<li>\u73b0\u8c61&#xff1a;Broker\u5185\u5b58\u6bcf\u5468\u589e\u957f20%&#xff0c;1\u4e2a\u6708\u540e\u56e0OOM&#xff08;\u5185\u5b58\u6ea2\u51fa&#xff09;\u5d29\u6e83\u3002<\/li>\n<li>\u6839\u56e0&#xff1a;\n<ul>\n<li>10\u4e07&#043;\u8bbe\u5907\u7684\u4fdd\u7559\u6d88\u606f\u672a\u8bbe\u7f6e\u6e05\u7406\u7b56\u7565&#xff0c;\u9ed8\u8ba4\u6c38\u4e45\u5b58\u50a8&#xff1b;<\/li>\n<li>\u5927\u91cf\u8bbe\u5907\u53d1\u5e03\u9ad8\u9891\u72b6\u6001\u6d88\u606f&#xff08;\u6bcf\u79d21\u6b21&#xff09;&#xff0c;\u4e14\u5747\u8bbe\u7f6eretain&#061;true&#xff0c;\u5bfc\u81f4\u4fdd\u7559\u6d88\u606f\u5b58\u50a8\u9891\u7e41\u66f4\u65b0&#xff0c;\u4ea7\u751f\u5185\u5b58\u788e\u7247\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u89e3\u51b3\u65b9\u6848&#xff1a;\n<li>\u914d\u7f6eBroker\u81ea\u52a8\u6e05\u7406\u7b56\u7565&#xff08;\u4ee5EMQX\u4e3a\u4f8b&#xff09;&#xff1a;<span class=\"token comment\"># emqx.conf \u914d\u7f6e\u4fdd\u7559\u6d88\u606f\u6700\u5927\u6570\u91cf\u548c\u6e05\u7406\u95f4\u9694<\/span><br \/>\nmqtt.retained.max_retained_messages <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">500000<\/span>  <span class=\"token comment\"># \u6700\u591a\u5b58\u50a850\u4e07\u6761<\/span><br \/>\nmqtt.retained.cleanup_interval <span class=\"token operator\">&#061;<\/span> 24h          <span class=\"token comment\"># \u6bcf\u5929\u6e05\u7406\u4e00\u6b21<\/span><br \/>\nmqtt.retained.expiry_interval <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">604800<\/span>        <span class=\"token comment\"># \u4fdd\u7559\u6d88\u606f7\u5929\u8fc7\u671f<\/span><\/p>\n<\/li>\n<li>\u4e1a\u52a1\u5c42\u4f18\u5316&#xff1a;\u975e\u5173\u952e\u72b6\u6001&#xff08;\u5982\u5b9e\u65f6\u6ce2\u5f62&#xff09;\u4e0d\u4f7f\u7528\u4fdd\u7559\u6d88\u606f&#xff0c;\u4ec5\u6838\u5fc3\u72b6\u6001&#xff08;\u5982\u8fd0\u884c\u6a21\u5f0f&#xff09;\u4fdd\u7559\u3002<\/li>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>\u516d\u3001\u6700\u4f73\u5b9e\u8df5\u603b\u7ed3<\/h4>\n<h5>6.1 \u4e3b\u9898\u547d\u540d\u89c4\u8303<\/h5>\n<p>\u4e3a\u907f\u514d\u51b2\u7a81&#xff0c;\u5efa\u8bae\u91c7\u7528\u201c\u8bbe\u5907\u7c7b\u578b\/\u552f\u4e00\u6807\u8bc6\/\u6d88\u606f\u7c7b\u578b\u201d\u7684\u4e09\u7ea7\u7ed3\u6784&#xff1a;<\/p>\n<table>\n<tr>\u6d88\u606f\u7c7b\u578b\u547d\u540d\u6a21\u5f0f\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>\u6b63\u5e38\u72b6\u6001<\/td>\n<td>${type}\/${id}\/state<\/td>\n<td>sensor\/temp_1001\/state<\/td>\n<\/tr>\n<tr>\n<td>LWT\u9057\u5631<\/td>\n<td>${type}\/${id}\/lwt<\/td>\n<td>sensor\/temp_1001\/lwt<\/td>\n<\/tr>\n<tr>\n<td>\u63a7\u5236\u6307\u4ee4<\/td>\n<td>${type}\/${id}\/cmd<\/td>\n<td>actuator\/valve_2001\/cmd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>6.2 \u5173\u952e\u53c2\u6570\u914d\u7f6e\u8868<\/h5>\n<table>\n<tr>\u53c2\u6570\u79fb\u52a8\u8bbe\u5907&#xff08;\u5982\u5171\u4eab\u5355\u8f66&#xff09;\u56fa\u5b9a\u8bbe\u5907&#xff08;\u5982\u5de5\u4e1a\u63a7\u5236\u5668&#xff09;\u4f4e\u529f\u8017\u8bbe\u5907&#xff08;\u5982NB-IoT&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>LWT-QoS<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>1&#xff08;\u786e\u4fdd\u544a\u8b66\u5fc5\u8fbe&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>LWT-Retain<\/td>\n<td>true<\/td>\n<td>true<\/td>\n<td>true<\/td>\n<\/tr>\n<tr>\n<td>WillDelayInterval<\/td>\n<td>30\u79d2&#xff08;\u8fc7\u6ee4\u7f51\u7edc\u95ea\u65ad&#xff09;<\/td>\n<td>0&#xff08;\u7acb\u5373\u544a\u8b66&#xff09;<\/td>\n<td>60\u79d2&#xff08;\u9002\u5e94\u4f11\u7720\u5468\u671f&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u4fdd\u7559\u6d88\u606f\u5b58\u50a8\u5468\u671f<\/td>\n<td>24\u5c0f\u65f6<\/td>\n<td>7\u5929<\/td>\n<td>30\u5929&#xff08;\u51cf\u5c11\u4e0a\u62a5\u6b21\u6570&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>6.3 \u5fc5\u987b\u76d1\u63a7\u7684\u6307\u6807<\/h5>\n<li>\u4fdd\u7559\u6d88\u606f\u5065\u5eb7\u5ea6&#xff1a;\n<ul>\n<li>\u4fdd\u7559\u6d88\u606f\u603b\u6570&#xff08;mqtt_retained_count&#xff09;&#xff1a;\u8d85\u8fc7Broker\u627f\u8f7d\u4e0a\u9650\u65f6\u544a\u8b66&#xff1b;<\/li>\n<li>\u4fdd\u7559\u6d88\u606f\u5e73\u5747\u5927\u5c0f&#xff08;mqtt_retained_avg_size&#xff09;&#xff1a;\u8fc7\u5927\u53ef\u80fd\u5bfc\u81f4\u5b58\u50a8\u538b\u529b\u3002<\/li>\n<\/ul>\n<\/li>\n<li>LWT\u89e6\u53d1\u9891\u7387&#xff1a;\n<ul>\n<li>\u5355\u4f4d\u65f6\u95f4LWT\u53d1\u5e03\u6b21\u6570&#xff08;mqtt_will_publish_rate&#xff09;&#xff1a;\u7a81\u589e\u53ef\u80fd\u9884\u793a\u7f51\u7edc\u6545\u969c\u6216\u8bbe\u5907\u6279\u91cf\u79bb\u7ebf\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u91cd\u70b9\u5fe0\u544a&#xff1a;<\/p>\n<ul>\n<li>\u6c38\u8fdc\u4e3aLWT\u548c\u6b63\u5e38\u72b6\u6001\u6d88\u606f\u8bbe\u7f6e\u72ec\u7acb\u4e3b\u9898&#xff0c;\u7269\u7406\u9694\u79bb\u6bd4\u903b\u8f91\u5224\u65ad\u66f4\u53ef\u9760&#xff1b;<\/li>\n<li>\u8bbe\u5907\u4e0a\u7ebf\u540e\u7684\u7b2c\u4e00\u4ef6\u4e8b&#xff0c;\u662f\u53d1\u5e03\u4e00\u6761\u6b63\u5e38\u72b6\u6001\u6d88\u606f&#xff08;\u8986\u76d6\u53ef\u80fd\u6b8b\u7559\u7684LWT&#xff09;&#xff1b;<\/li>\n<li>\u4fdd\u7559\u6d88\u606f\u5fc5\u987b\u5305\u542b\u65f6\u95f4\u6233&#xff0c;\u5426\u5219\u201c\u6700\u65b0\u72b6\u6001\u201d\u53ef\u80fd\u662f\u8fc7\u671f\u7684\u201c\u5e7d\u7075\u6570\u636e\u201d\u3002<\/li>\n<\/ul>\n<p>LWT\u4e0e\u4fdd\u7559\u6d88\u606f\u662f\u7269\u8054\u7f51\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u201c\u5de6\u53f3\u8111\u201d\u2014\u2014LWT\u786e\u4fdd\u5f02\u5e38\u72b6\u6001\u88ab\u53ca\u65f6\u611f\u77e5&#xff0c;\u4fdd\u7559\u6d88\u606f\u786e\u4fdd\u6b63\u5e38\u72b6\u6001\u53ef\u968f\u65f6\u83b7\u53d6\u3002\u4e8c\u8005\u7684\u6b63\u786e\u534f\u4f5c&#xff0c;\u80fd\u8ba9\u4f60\u7684\u7269\u8054\u7f51\u7cfb\u7edf\u4ece\u201c\u76f2\u76ee\u8fd0\u884c\u201d\u8f6c\u53d8\u4e3a\u201c\u900f\u660e\u53ef\u63a7\u201d\u3002\u4f46\u8bf7\u8bb0\u4f4f&#xff1a;\u6ca1\u6709\u653e\u4e4b\u56db\u6d77\u800c\u7686\u51c6\u7684\u914d\u7f6e&#xff0c;\u5fc5\u987b\u7ed3\u5408\u4e1a\u52a1\u573a\u666f&#xff08;\u5982\u8bbe\u5907\u79fb\u52a8\u6027\u3001\u7f51\u7edc\u7a33\u5b9a\u6027\u3001\u6570\u636e\u91cd\u8981\u6027&#xff09;\u7075\u6d3b\u8c03\u6574&#xff0c;\u624d\u80fd\u53d1\u6325\u5176\u6700\u5927\u4ef7\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb387\u6b21\uff0c\u70b9\u8d5e13\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u7269\u8054\u7f51\u8bbe\u5907\u5f02\u5e38\u6389\u7ebf\u95ee\u9898\u5e38\u56e0MQTT\u534f\u8bae\u914d\u7f6e\u4e0d\u5f53\u5bfc\u81f4\u3002\u672c\u6587\u5256\u6790\u9057\u5631\u6d88\u606f(LWT)\u4e0e\u4fdd\u7559\u6d88\u606f\u7684\u6838\u5fc3\u5dee\u5f02\uff1aLWT\u7528\u4e8e\u8bbe\u5907\u5f02\u5e38\u79bb\u7ebf\u901a\u77e5\uff0c\u4ec5\u5728\u975e\u6b63\u5e38\u65ad\u5f00\u65f6\u89e6\u53d1\uff1b\u4fdd\u7559\u6d88\u606f\u5219\u7f13\u5b58\u4e3b\u9898\u6700\u65b0\u72b6\u6001\uff0c\u89e3\u51b3\u65b0\u8ba2\u9605\u8005\u5ef6\u8fdf\u95ee\u9898\u3002\u901a\u8fc7\u667a\u80fd\u5bb6\u5c45\u6e29\u63a7\u5668\u7b49\u6848\u4f8b\uff0c\u6307\u51fa\u5178\u578b\u914d\u7f6e\u9677\u9631\uff0c\u5982\u4e3b\u9898\u51b2\u7a81\u3001QoS\u7b49\u7ea7\u4e0d\u8db3\u7b49\uff0c\u5e76\u63d0\u51fa\u4e3b\u9898\u5206\u79bb\u3001QoS\u22651\u7b49\u89e3\u51b3\u65b9\u6848\uff0c\u5e2e\u52a9\u6784\u5efa\u53ef\u9760\u7684\u8bbe\u5907\u76d1\u63a7\u4f53\u7cfb\u3002\u6b63\u786e\u914d\u7f6e\u8fd9\u4e24\u79cd\u673a\u5236\u53ef\u663e\u8457\u63d0\u5347\u7269\u8054\u7f51\u7cfb\u7edf\u72b6\u6001\u611f\u77e5\u80fd\u529b\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":[4966,320,5425,5424,5426],"topic":[],"class_list":["post-53440","post","type-post","status-publish","format-standard","hentry","category-server","tag-iot","tag-mqtt","tag-5425","tag-lwt","tag-5426"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MQTT\u534f\u8bae\uff08\u516d\uff09\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u9057\u5631\u6d88\u606f\uff08LWT\uff09\u4e0e\u4fdd\u7559\u6d88\u606f\u6df1\u5ea6\u89e3\u6790 - \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\/53440.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MQTT\u534f\u8bae\uff08\u516d\uff09\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u9057\u5631\u6d88\u606f\uff08LWT\uff09\u4e0e\u4fdd\u7559\u6d88\u606f\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb387\u6b21\uff0c\u70b9\u8d5e13\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u7269\u8054\u7f51\u8bbe\u5907\u5f02\u5e38\u6389\u7ebf\u95ee\u9898\u5e38\u56e0MQTT\u534f\u8bae\u914d\u7f6e\u4e0d\u5f53\u5bfc\u81f4\u3002\u672c\u6587\u5256\u6790\u9057\u5631\u6d88\u606f(LWT)\u4e0e\u4fdd\u7559\u6d88\u606f\u7684\u6838\u5fc3\u5dee\u5f02\uff1aLWT\u7528\u4e8e\u8bbe\u5907\u5f02\u5e38\u79bb\u7ebf\u901a\u77e5\uff0c\u4ec5\u5728\u975e\u6b63\u5e38\u65ad\u5f00\u65f6\u89e6\u53d1\uff1b\u4fdd\u7559\u6d88\u606f\u5219\u7f13\u5b58\u4e3b\u9898\u6700\u65b0\u72b6\u6001\uff0c\u89e3\u51b3\u65b0\u8ba2\u9605\u8005\u5ef6\u8fdf\u95ee\u9898\u3002\u901a\u8fc7\u667a\u80fd\u5bb6\u5c45\u6e29\u63a7\u5668\u7b49\u6848\u4f8b\uff0c\u6307\u51fa\u5178\u578b\u914d\u7f6e\u9677\u9631\uff0c\u5982\u4e3b\u9898\u51b2\u7a81\u3001QoS\u7b49\u7ea7\u4e0d\u8db3\u7b49\uff0c\u5e76\u63d0\u51fa\u4e3b\u9898\u5206\u79bb\u3001QoS\u22651\u7b49\u89e3\u51b3\u65b9\u6848\uff0c\u5e2e\u52a9\u6784\u5efa\u53ef\u9760\u7684\u8bbe\u5907\u76d1\u63a7\u4f53\u7cfb\u3002\u6b63\u786e\u914d\u7f6e\u8fd9\u4e24\u79cd\u673a\u5236\u53ef\u663e\u8457\u63d0\u5347\u7269\u8054\u7f51\u7cfb\u7edf\u72b6\u6001\u611f\u77e5\u80fd\u529b\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/53440.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-12T03:40:07+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/53440.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/53440.html\",\"name\":\"MQTT\u534f\u8bae\uff08\u516d\uff09\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u9057\u5631\u6d88\u606f\uff08LWT\uff09\u4e0e\u4fdd\u7559\u6d88\u606f\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-12T03:40:07+00:00\",\"dateModified\":\"2025-08-12T03:40:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/53440.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/53440.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/53440.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MQTT\u534f\u8bae\uff08\u516d\uff09\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u9057\u5631\u6d88\u606f\uff08LWT\uff09\u4e0e\u4fdd\u7559\u6d88\u606f\u6df1\u5ea6\u89e3\u6790\"}]},{\"@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\u534f\u8bae\uff08\u516d\uff09\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u9057\u5631\u6d88\u606f\uff08LWT\uff09\u4e0e\u4fdd\u7559\u6d88\u606f\u6df1\u5ea6\u89e3\u6790 - \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\/53440.html","og_locale":"zh_CN","og_type":"article","og_title":"MQTT\u534f\u8bae\uff08\u516d\uff09\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u9057\u5631\u6d88\u606f\uff08LWT\uff09\u4e0e\u4fdd\u7559\u6d88\u606f\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb387\u6b21\uff0c\u70b9\u8d5e13\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u7269\u8054\u7f51\u8bbe\u5907\u5f02\u5e38\u6389\u7ebf\u95ee\u9898\u5e38\u56e0MQTT\u534f\u8bae\u914d\u7f6e\u4e0d\u5f53\u5bfc\u81f4\u3002\u672c\u6587\u5256\u6790\u9057\u5631\u6d88\u606f(LWT)\u4e0e\u4fdd\u7559\u6d88\u606f\u7684\u6838\u5fc3\u5dee\u5f02\uff1aLWT\u7528\u4e8e\u8bbe\u5907\u5f02\u5e38\u79bb\u7ebf\u901a\u77e5\uff0c\u4ec5\u5728\u975e\u6b63\u5e38\u65ad\u5f00\u65f6\u89e6\u53d1\uff1b\u4fdd\u7559\u6d88\u606f\u5219\u7f13\u5b58\u4e3b\u9898\u6700\u65b0\u72b6\u6001\uff0c\u89e3\u51b3\u65b0\u8ba2\u9605\u8005\u5ef6\u8fdf\u95ee\u9898\u3002\u901a\u8fc7\u667a\u80fd\u5bb6\u5c45\u6e29\u63a7\u5668\u7b49\u6848\u4f8b\uff0c\u6307\u51fa\u5178\u578b\u914d\u7f6e\u9677\u9631\uff0c\u5982\u4e3b\u9898\u51b2\u7a81\u3001QoS\u7b49\u7ea7\u4e0d\u8db3\u7b49\uff0c\u5e76\u63d0\u51fa\u4e3b\u9898\u5206\u79bb\u3001QoS\u22651\u7b49\u89e3\u51b3\u65b9\u6848\uff0c\u5e2e\u52a9\u6784\u5efa\u53ef\u9760\u7684\u8bbe\u5907\u76d1\u63a7\u4f53\u7cfb\u3002\u6b63\u786e\u914d\u7f6e\u8fd9\u4e24\u79cd\u673a\u5236\u53ef\u663e\u8457\u63d0\u5347\u7269\u8054\u7f51\u7cfb\u7edf\u72b6\u6001\u611f\u77e5\u80fd\u529b\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/53440.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-12T03:40:07+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/53440.html","url":"https:\/\/www.wsisp.com\/helps\/53440.html","name":"MQTT\u534f\u8bae\uff08\u516d\uff09\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u9057\u5631\u6d88\u606f\uff08LWT\uff09\u4e0e\u4fdd\u7559\u6d88\u606f\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-12T03:40:07+00:00","dateModified":"2025-08-12T03:40:07+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/53440.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/53440.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/53440.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"MQTT\u534f\u8bae\uff08\u516d\uff09\u8bbe\u5907\u72b6\u6001\u76d1\u63a7\u7684\u9057\u5631\u6d88\u606f\uff08LWT\uff09\u4e0e\u4fdd\u7559\u6d88\u606f\u6df1\u5ea6\u89e3\u6790"}]},{"@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\/53440","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=53440"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/53440\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=53440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=53440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=53440"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=53440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}