{"id":27363,"date":"2024-04-07T17:11:13","date_gmt":"2024-04-07T09:11:13","guid":{"rendered":"https:\/\/www.modbus.cn\/?post_type=docs&#038;p=27363"},"modified":"2024-04-07T17:12:12","modified_gmt":"2024-04-07T09:12:12","password":"","slug":"python","status":"publish","type":"docs","link":"https:\/\/www.modbus.cn\/en\/docs\/python","title":{"rendered":"Python\u901a\u8fc7MQTT\u63a5\u5165\u793a\u4f8b"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\" id=\"VGawg\">\u6982\u8ff0<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"u65f7b3af\">\u672c\u6587\u4ee5Python\u8bed\u8a00\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u901a\u8fc7MQTTS\/MQTT\u534f\u8bae\u63a5\u5165\u5e73\u53f0\uff0c\u57fa\u4e8e\u5e73\u53f0\u63a5\u53e3\u5b9e\u73b0\u201c\u5c5e\u6027\u4e0a\u62a5\u201d\u3001\u201c\u8ba2\u9605\u63a5\u6536\u547d\u4ee4\u201d\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"ubbb441dc\"><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"u75bac27d\">\u672c\u6587\u4e2d\u4f7f\u7528\u7684\u4ee3\u7801\u4e3a\u6837\u4f8b\u4ee3\u7801\uff0c\u4ec5\u7528\u4e8e\u4f53\u9a8c\u5e73\u53f0\u901a\u4fe1\u529f\u80fd\uff0c\u5982\u9700\u8fdb\u884c\u5546\u7528\uff0c\u53ef\u4ee5\u53c2\u8003<a href=\"https:\/\/support.huaweicloud.com\/devg-iothub\/iot_02_1004.html\" target=\"_blank\" rel=\"noopener noreferrer nofollow noreferrer noopener\">\u8d44\u6e90\u83b7\u53d6<\/a>\u83b7\u53d6\u5bf9\u5e94\u8bed\u8a00\u7684IoT Device SDK\u8fdb\u884c\u96c6\u6210\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"uARCo\">\u524d\u63d0\u6761\u4ef6<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5df2\u5b89\u88c5python\uff0c\u82e5\u672a\u5b89\u88c5\u8bf7\u53c2\u8003<a href=\"https:\/\/support.huaweicloud.com\/devg-iothub\/iot_02_2129.html#ZH-CN_TOPIC_0240834886__li1563192812342\" target=\"_blank\" rel=\"noopener noreferrer nofollow noreferrer noopener\">\u5b89\u88c5python<\/a>\u3002<\/li>\n\n\n\n<li>\u5df2\u5728\u7ba1\u7406\u63a7\u5236\u53f0\u83b7\u53d6\u8bbe\u5907\u63a5\u5165\u5730\u5740\u3002\u83b7\u53d6\u5730\u5740\u7684\u64cd\u4f5c\u6b65\u9aa4\uff0c\u8bf7\u53c2\u8003\u5e73\u53f0\u5bf9\u63a5\u4fe1\u606f\u3002<\/li>\n\n\n\n<li>\u5df2\u5728\u7ba1\u7406\u63a7\u5236\u53f0\u521b\u5efa\u4ea7\u54c1\u548c\u8bbe\u5907\u3002\u521b\u5efa\u4ea7\u54c1\u548c\u8bbe\u5907\u7684\u5177\u4f53\u64cd\u4f5c\u7ec6\u8282\uff0c\u8bf7\u53c2\u8003\u521b\u5efa\u4ea7\u54c1\u3001\u6ce8\u518c\u5355\u4e2a\u8bbe\u5907\u6216\u6279\u91cf\u6ce8\u518c\u8bbe\u5907\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"gCXCH\">\u51c6\u5907\u5de5\u4f5c<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b89\u88c5python<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8bbf\u95ee<a href=\"https:\/\/www.python.org\/downloads\/windows\/\" target=\"_blank\" rel=\"noopener noreferrer nofollow noreferrer noopener\">python\u5b98\u7f51<\/a>\uff0c\u9009\u62e9\u5408\u9002\u7cfb\u7edf\u7684\u7248\u672c\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002\uff08\u672c\u6587\u4ee5windows\u7cfb\u7edf\u4e3a\u4f8b\uff0c\u5b89\u88c5python3.8.2\uff09\u3002<img decoding=\"async\" src=\"https:\/\/image.modbus.cn\/wp-content\/uploads\/2024\/04\/20240407171114715.png\" style=\"width: 750px;\" loading=\"lazy\" title=\"Python\u901a\u8fc7MQTT\u63a5\u5165\u793a\u4f8b\u63d2\u56fe\" alt=\"Python\u901a\u8fc7MQTT\u63a5\u5165\u793a\u4f8b\u63d2\u56fe\" \/><\/li>\n\n\n\n<li>\u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u8fd0\u884cexe\u6587\u4ef6\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n\n\n\n<li>\u52fe\u9009\u201cAdd python 3.8 to PAYTH\u201d\uff08\u5982\u65e0\u52fe\u9009\uff0c\u9700\u624b\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff09\uff0c\u5355\u51fb\u201cCustomize installation\u201d\uff0c\u6309\u7167\u754c\u9762\u63d0\u793a\u5b89\u88c5\u3002<img decoding=\"async\" src=\"https:\/\/image.modbus.cn\/wp-content\/uploads\/2024\/04\/20240407171115668.png\" style=\"width: 750px;\" loading=\"lazy\" title=\"Python\u901a\u8fc7MQTT\u63a5\u5165\u793a\u4f8b\u63d2\u56fe1\" alt=\"Python\u901a\u8fc7MQTT\u63a5\u5165\u793a\u4f8b\u63d2\u56fe1\" \/><\/li>\n\n\n\n<li>\u68c0\u67e5python\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002Win\u952e + r &#8211;>\u8f93\u5165 cmd&#8211;>\u56de\u8f66\uff0c\u8fdb\u5165\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u8f93\u5165python \u2013V\uff0c\u56de\u8f66\u540e\u663e\u793apython\u7248\u672c\u5373\u8868\u793a\u5b89\u88c5\u6210\u529f\u3002<img decoding=\"async\" src=\"https:\/\/image.modbus.cn\/wp-content\/uploads\/2024\/04\/20240407171116818.png\" style=\"width: 750px;\" loading=\"lazy\" title=\"Python\u901a\u8fc7MQTT\u63a5\u5165\u793a\u4f8b\u63d2\u56fe2\" alt=\"Python\u901a\u8fc7MQTT\u63a5\u5165\u793a\u4f8b\u63d2\u56fe2\" \/><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"N7sem\">\u5b8c\u6574\u4ee3\u7801<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># -*- coding: utf-8 -*-\n\nimport paho.mqtt.client as mqtt\nimport json\nimport threading\n\n# MQTT \u670d\u52a1\u5668\u5730\u5740\nMQTT_BROKER = &quot;iot.modbus.cn&quot;\n# MQTT \u7aef\u53e3\u53f7\nMQTT_PORT = 1883\n# MQTT \u5ba2\u6237\u7aef ID\nMQTT_CLIENT_ID = &quot;4QR8TZ9ThuL4G&quot;\n# MQTT \u7528\u6237\u540d\nMQTT_USERNAME = &quot;ceshi&quot;\n# MQTT \u5bc6\u7801\nMQTT_PASSWORD = &quot;Abc123456&quot;\n# \u8ba2\u9605\u7684\u4e3b\u9898\nMQTT_TOPIC_SUBSCRIBE = &quot;\/server\/coo\/4QR8TZ9ThuL4G&quot;\n# \u53d1\u5e03\u7684\u4e3b\u9898\nMQTT_TOPIC_PUBLISH = &quot;\/dev\/coo\/4QR8TZ9ThuL4G&quot;\n\n# \u521b\u5efa MQTT \u5ba2\u6237\u7aef\nclient = mqtt.Client(client_id=MQTT_CLIENT_ID)\nclient.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)\n\n# \u8fde\u63a5\u6210\u529f\u56de\u8c03\u51fd\u6570\ndef on_connect(client, userdata, flags, rc):\n    print(&quot;Connected with result code &quot; + str(rc))\n    # \u8ba2\u9605\u4e3b\u9898\n    client.subscribe(MQTT_TOPIC_SUBSCRIBE)\n\n# \u63a5\u6536\u6d88\u606f\u56de\u8c03\u51fd\u6570\ndef on_message(client, userdata, msg):\n    print(&quot;Received message: &quot; + msg.topic + &quot; &quot; + msg.payload.decode())\n\n# \u53d1\u5e03\u6d88\u606f\ndef publish_message(client, topic, payload):\n    client.publish(topic, payload)\n\n# \u5fc3\u8df3\u51fd\u6570\ndef send_heartbeat():\n    print(&quot;Sending heartbeat&quot;)\n    # \u53d1\u9001\u5fc3\u8df3\u6d88\u606f\n    publish_message(client, MQTT_TOPIC_PUBLISH, &quot;heartbeat&quot;)\n    # \u5b9a\u65f6\u8c03\u7528\u81ea\u8eab\n    threading.Timer(60, send_heartbeat).start()\n\n# \u5b9a\u65f6\u4e0a\u62a5\u51fd\u6570\ndef report_data():\n    print(&quot;Reporting data&quot;)\n    # \u5f85\u4e0a\u62a5\u7684\u6570\u636e\n    message = [\n        {&quot;sensor_device_id&quot;: 1, &quot;port_id&quot;: 1, &quot;sdata&quot;: 33},\n        {&quot;sensor_device_id&quot;: 1, &quot;port_id&quot;: 2, &quot;sdata&quot;: 60}\n    ]\n    payload = json.dumps(message)\n    # \u53d1\u5e03\u6570\u636e\n    publish_message(client, MQTT_TOPIC_PUBLISH, payload)\n    # \u5b9a\u65f6\u8c03\u7528\u81ea\u8eab\n    threading.Timer(10, report_data).start()\n\n# \u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\nclient.on_connect = on_connect\nclient.on_message = on_message\n\n# \u8fde\u63a5 MQTT \u670d\u52a1\u5668\nclient.connect(MQTT_BROKER, MQTT_PORT, 60)\n\n# \u542f\u52a8\u5fc3\u8df3\u5b9a\u65f6\u5668\nsend_heartbeat()\n\n# \u542f\u52a8\u5b9a\u65f6\u4e0a\u62a5\u5b9a\u65f6\u5668\nreport_data()\n\n# \u5faa\u73af\u76d1\u542c\u6d88\u606f\nclient.loop_forever()<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"u27cbd9ad\">\u5907\u6ce8\uff1a\u4ece\u8bbe\u5907\u5730\u5740\u662fsensor_device_id \uff0c\u5bc4\u5b58\u5668\u662fport_id\uff0c\u6570\u636e\u6570\u503c\u662fSdata \uff0c\u5177\u4f53\u7a0d\u540e\u6709\u5177\u4f53\u8bf4\u660e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"u1f59e98b\">MQTT\u57fa\u672c\u4fe1\u606f<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Topic<\/strong><\/td><td><strong>\u6743\u9650<\/strong><\/td><td><strong>\u8bf4\u660e<\/strong><\/td><\/tr><tr><td>Broker Address<\/td><td>iot.modbus.cn<\/td><td>MQTT\u5730\u5740<\/td><\/tr><tr><td>Broker Port<\/td><td>1883<\/td><td>MQTT\u7aef\u53e3\u53f7<\/td><\/tr><tr><td>Client ID<\/td><td>4QR8TZ9ThuL4G<\/td><td>\u8bbe\u5907\u53f7SN(\u8bf7\u66ff\u6362\u4e3a\u81ea\u5df1\u7684\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"u6a080b81\">MQTT\u8d26\u53f7\u5bc6\u7801<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Topic<\/strong><\/td><td><strong>\u6743\u9650<\/strong><\/td><td><strong>\u8bf4\u660e<\/strong><\/td><\/tr><tr><td>User Name<\/td><td>ceshi<\/td><td>\u7528\u6237\u8d26\u6237<\/td><\/tr><tr><td>Password<\/td><td>123456<\/td><td>\u7528\u6237\u5bc6\u7801<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"hsgEk\">MQTT \u4e3b\u9898\u4e00\u89c8<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"ua6098b00\">Modbus\u7269\u8054\u7f51\u5e73\u53f0 \u4f5c\u4e3a\u7269\u8054\u7f51 PaaS \u4e91\u5e73\u53f0\uff0c\u5bf9\u8bbe\u5907 MQTT \u63a5\u5165\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u8bbf\u95ee\u534f\u8bae\u89c4\u8303\uff0c\u8ba9\u8bbe\u5907\u548c\u4e91\u5e73\u53f0\u7684\u6d88\u606f\u901a\u4fe1\u66f4\u52a0\u6709\u7ae0\u53ef\u5faa\uff0c\u5927\u5927\u7b80\u5316\u4e86\u7269\u8054\u7f51\u9879\u76ee\u7684\u5f00\u53d1\u96be\u5ea6\uff0c\u7f29\u77ed\u4e86\u4ea7\u54c1\u7684\u5f00\u53d1\u5468\u671f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"ub3df03a0\">\u4e0d\u540c\u4e8e\u666e\u901a\u7684 MQTT \u4f7f\u7528\u65b9\u5f0f\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u6807\u51c6\u7684\u5185\u7f6e\u4e3b\u9898\uff0c\u8fd9\u8db3\u4ee5\u5b9e\u73b0\u7edd\u5927\u591a\u6570\u7684\u7269\u8054\u7f51\u5e94\u7528\u573a\u666f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Topic<\/strong><\/td><td><strong>\u6743\u9650<\/strong><\/td><td><strong>\u8bf4\u660e<\/strong><\/td><\/tr><tr><td>\/dev\/coo\/4QR8TZ9ThuL4G<\/td><td><strong>\u53d1\u5e03<\/strong><\/td><td>\u4e0a\u884c\u901a\u4fe1\uff1a\u8bbe\u5907\u901a\u8fc7\u8be5Topic\u5411\u7269\u8054\u7f51\u5e73\u53f0\u53d1\u9001\u6d88\u606f\u3002<\/td><\/tr><tr><td>\/server\/coo\/4QR8TZ9ThuL4G<\/td><td><strong>\u8ba2\u9605<\/strong><\/td><td>\u4e0b\u884c\u901a\u4fe1\uff1a\u8bbe\u5907\u901a\u8fc7\u8ba2\u9605\u8be5Topic\uff0c\u83b7\u53d6\u4ece\u7269\u8054\u7f51\u5e73\u53f0\u4e0b\u53d1\u7684\u6d88\u606f\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"uf23209aa\">4QR8TZ9ThuL4G\u4e3a\u793a\u4f8b\u8bbe\u5907\u53f7SN\uff0c\u8bf7\u66ff\u6362\u4e3a\u81ea\u5df1\u7684\uff01<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"JcWjD\">\u8be6\u89e3\u4ecb\u7ecd<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u548c\u6a21\u5757\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\nimport paho.mqtt.client as mqtt\nimport json\nimport threading<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"u07ae9575\">\u6b64\u5916\uff0c\u8bf7\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86<strong>paho-mqtt<\/strong>\u5e93\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5b83\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pip install paho-mqtt<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u5b9a\u4e49 MQTT \u76f8\u5173\u7684\u53c2\u6570\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\nMQTT_BROKER = &quot;cloud.modbus.cn&quot;\nMQTT_PORT = 1883\nMQTT_CLIENT_ID = &quot;4QR8TZ9ThuL4G&quot;\nMQTT_USERNAME = &quot;ceshi&quot;\nMQTT_PASSWORD = &quot;Abc123456&quot;\nMQTT_TOPIC_SUBSCRIBE = &quot;\/server\/coo\/4QR8TZ9ThuL4G&quot;\nMQTT_TOPIC_PUBLISH = &quot;\/dev\/coo\/4QR8TZ9ThuL4G&quot;<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u521b\u5efa MQTT \u5ba2\u6237\u7aef\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\nclient = mqtt.Client(client_id=MQTT_CLIENT_ID)\nclient.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u5b9a\u4e49\u8fde\u63a5\u6210\u529f\u7684\u56de\u8c03\u51fd\u6570\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\ndef on_connect(client, userdata, flags, rc):\n    print(&quot;Connected with result code &quot; + str(rc))\n    client.subscribe(MQTT_TOPIC_SUBSCRIBE)<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li>\u5b9a\u4e49\u63a5\u6536\u6d88\u606f\u7684\u56de\u8c03\u51fd\u6570\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\ndef on_message(client, userdata, msg):\n    print(&quot;Received message: &quot; + msg.topic + &quot; &quot; + msg.payload.decode())<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li>\u5b9a\u4e49\u53d1\u5e03\u6d88\u606f\u7684\u51fd\u6570\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\ndef publish_message(client, topic, payload):\n    client.publish(topic, payload)<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"7\">\n<li>\u5b9a\u4e49\u5fc3\u8df3\u51fd\u6570\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\ndef send_heartbeat():\n    print(&quot;Sending heartbeat&quot;)\n    publish_message(client, MQTT_TOPIC_PUBLISH, &quot;heartbeat&quot;)\n    threading.Timer(60, send_heartbeat).start()<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"8\">\n<li>\u5b9a\u4e49\u5b9a\u65f6\u4e0a\u62a5\u51fd\u6570\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\ndef report_data():\n    print(&quot;Reporting data&quot;)\n    message = [\n        {&quot;sensor_device_id&quot;: 1, &quot;port_id&quot;: 1, &quot;sdata&quot;: 33},\n        {&quot;sensor_device_id&quot;: 1, &quot;port_id&quot;: 2, &quot;sdata&quot;: 60}\n    ]\n    payload = json.dumps(message)\n    publish_message(client, MQTT_TOPIC_PUBLISH, payload)\n    threading.Timer(10, report_data).start()<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"9\">\n<li>\u8bbe\u7f6e\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u6210\u529f\u548c\u6d88\u606f\u63a5\u6536\u7684\u56de\u8c03\u51fd\u6570\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\nclient.on_connect = on_connect\nclient.on_message = on_message<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"10\">\n<li>\u8fde\u63a5 MQTT \u670d\u52a1\u5668\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\nclient.connect(MQTT_BROKER, MQTT_PORT, 60)<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"11\">\n<li>\u542f\u52a8\u5fc3\u8df3\u5b9a\u65f6\u5668\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\nsend_heartbeat()<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"12\">\n<li>\u542f\u52a8\u5b9a\u65f6\u4e0a\u62a5\u5b9a\u65f6\u5668\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\nreport_data()<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"13\">\n<li>\u8fdb\u5165\u6d88\u606f\u76d1\u542c\u5faa\u73af\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">pythonCopy code\nclient.loop_forever()<\/pre><\/div>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 \u672c\u6587\u4ee5Python\u8bed\u8a00\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u901a\u8fc7MQTTS\/MQT&#8230;<\/p>","protected":false},"author":1,"featured_media":27364,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"doc_category":[1921],"doc_tag":[],"class_list":["post-27363","docs","type-docs","status-publish","has-post-thumbnail","hentry","doc_category-mqtt-access"],"year_month":"2026-07","word_count":564,"total_views":0,"reactions":{"happy":0,"normal":0,"sad":0},"author_info":{"name":"modbus\u6280\u672f\u56e2\u961f","author_nicename":"ckhr","author_url":"https:\/\/www.modbus.cn\/en\/profile\/1"},"doc_category_info":[{"term_name":"\u8bbe\u5907 MQTT \u63a5\u5165","term_url":"https:\/\/www.modbus.cn\/en\/docs-category\/mqtt-access"}],"doc_tag_info":[],"_links":{"self":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/docs\/27363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/comments?post=27363"}],"version-history":[{"count":0,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/docs\/27363\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media\/27364"}],"wp:attachment":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=27363"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/doc_category?post=27363"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/doc_tag?post=27363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}