"# Python\u901a\u8fc7MQTT\u63a5\u5165\u793a\u4f8b\n\n> 2024-04-07 | 2024-04-07\n> https:\/\/www.modbus.cn\/en\/docs\/python\n\n---\n\n### \u6982\u8ff0\n\n\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\n\n**\u8bf4\u660e\uff1a**\n\n\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[\u8d44\u6e90\u83b7\u53d6](https:\/\/support.huaweicloud.com\/devg-iothub\/iot_02_1004.html)\u83b7\u53d6\u5bf9\u5e94\u8bed\u8a00\u7684IoT Device SDK\u8fdb\u884c\u96c6\u6210\u3002\n\n#### \u524d\u63d0\u6761\u4ef6\n\n- \u5df2\u5b89\u88c5python\uff0c\u82e5\u672a\u5b89\u88c5\u8bf7\u53c2\u8003[\u5b89\u88c5python](https:\/\/support.huaweicloud.com\/devg-iothub\/iot_02_2129.html#ZH-CN_TOPIC_0240834886__li1563192812342)\u3002\n\n- \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\n\n- \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\n\n### \u51c6\u5907\u5de5\u4f5c\n\n- \u5b89\u88c5python\n\n- \u8bbf\u95ee[python\u5b98\u7f51](https:\/\/www.python.org\/downloads\/windows\/)\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\n\n- \u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u8fd0\u884cexe\u6587\u4ef6\u8fdb\u884c\u5b89\u88c5\u3002\n\n- \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\n\n- \u68c0\u67e5python\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002Win\u952e + r -->\u8f93\u5165 cmd-->\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\n\n### \u5b8c\u6574\u4ee3\u7801\n\n```\n# -*- 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()\n```\n\n\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\n\nMQTT\u57fa\u672c\u4fe1\u606f\n\n**Topic****\u6743\u9650****\u8bf4\u660e**Broker Addressiot.modbus.cnMQTT\u5730\u5740Broker Port1883MQTT\u7aef\u53e3\u53f7Client ID4QR8TZ9ThuL4G\u8bbe\u5907\u53f7SN(\u8bf7\u66ff\u6362\u4e3a\u81ea\u5df1\u7684\uff09\n\nMQTT\u8d26\u53f7\u5bc6\u7801\n\n**Topic****\u6743\u9650****\u8bf4\u660e**User Nameceshi\u7528\u6237\u8d26\u6237Password123456\u7528\u6237\u5bc6\u7801\n\n### MQTT \u4e3b\u9898\u4e00\u89c8\n\nModbus\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\n\n\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\n\n**Topic****\u6743\u9650****\u8bf4\u660e**\/dev\/coo\/4QR8TZ9ThuL4G**\u53d1\u5e03**\u4e0a\u884c\u901a\u4fe1\uff1a\u8bbe\u5907\u901a\u8fc7\u8be5Topic\u5411\u7269\u8054\u7f51\u5e73\u53f0\u53d1\u9001\u6d88\u606f\u3002\/server\/coo\/4QR8TZ9ThuL4G**\u8ba2\u9605**\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\n\n4QR8TZ9ThuL4G\u4e3a\u793a\u4f8b\u8bbe\u5907\u53f7SN\uff0c\u8bf7\u66ff\u6362\u4e3a\u81ea\u5df1\u7684\uff01\n\n#### \u8be6\u89e3\u4ecb\u7ecd\n\n- \u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u548c\u6a21\u5757\uff1a\n\n```\npythonCopy code\nimport paho.mqtt.client as mqtt\nimport json\nimport threading\n```\n\n\u6b64\u5916\uff0c\u8bf7\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86**paho-mqtt**\u5e93\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5b83\uff1a\n\n```\npip install paho-mqtt\n```\n\n- \u5b9a\u4e49 MQTT \u76f8\u5173\u7684\u53c2\u6570\uff1a\n\n```\npythonCopy 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;\n```\n\n- \u521b\u5efa MQTT \u5ba2\u6237\u7aef\uff1a\n\n```\npythonCopy code\nclient = mqtt.Client(client_id=MQTT_CLIENT_ID)\nclient.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)\n```\n\n- \u5b9a\u4e49\u8fde\u63a5\u6210\u529f\u7684\u56de\u8c03\u51fd\u6570\uff1a\n\n```\npythonCopy code\ndef on_connect(client, userdata, flags, rc):\n    print(&quot;Connected with result code &quot; + str(rc))\n    client.subscribe(MQTT_TOPIC_SUBSCRIBE)\n```\n\n- \u5b9a\u4e49\u63a5\u6536\u6d88\u606f\u7684\u56de\u8c03\u51fd\u6570\uff1a\n\n```\npythonCopy code\ndef on_message(client, userdata, msg):\n    print(&quot;Received message: &quot; + msg.topic + &quot; &quot; + msg.payload.decode())\n```\n\n- \u5b9a\u4e49\u53d1\u5e03\u6d88\u606f\u7684\u51fd\u6570\uff1a\n\n```\npythonCopy code\ndef publish_message(client, topic, payload):\n    client.publish(topic, payload)\n```\n\n- \u5b9a\u4e49\u5fc3\u8df3\u51fd\u6570\uff1a\n\n```\npythonCopy 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()\n```\n\n- \u5b9a\u4e49\u5b9a\u65f6\u4e0a\u62a5\u51fd\u6570\uff1a\n\n```\npythonCopy 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()\n```\n\n- \u8bbe\u7f6e\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u6210\u529f\u548c\u6d88\u606f\u63a5\u6536\u7684\u56de\u8c03\u51fd\u6570\uff1a\n\n```\npythonCopy code\nclient.on_connect = on_connect\nclient.on_message = on_message\n```\n\n- \u8fde\u63a5 MQTT \u670d\u52a1\u5668\uff1a\n\n```\npythonCopy code\nclient.connect(MQTT_BROKER, MQTT_PORT, 60)\n```\n\n- \u542f\u52a8\u5fc3\u8df3\u5b9a\u65f6\u5668\uff1a\n\n```\npythonCopy code\nsend_heartbeat()\n```\n\n- \u542f\u52a8\u5b9a\u65f6\u4e0a\u62a5\u5b9a\u65f6\u5668\uff1a\n\n```\npythonCopy code\nreport_data()\n```\n\n- \u8fdb\u5165\u6d88\u606f\u76d1\u542c\u5faa\u73af\uff1a\n\n```\npythonCopy code\nclient.loop_forever()\n```\n\n---\n*modbus.cn*\n"