{"id":2931,"date":"2024-11-13T16:25:34","date_gmt":"2024-11-13T08:25:34","guid":{"rendered":"https:\/\/saludpcb.com\/zh\/?p=2931"},"modified":"2025-09-26T06:16:07","modified_gmt":"2025-09-25T22:16:07","slug":"unlocking-the-power-of-mqtt-wildcards","status":"publish","type":"post","link":"https:\/\/saludpcb.com\/zh\/unlocking-the-power-of-mqtt-wildcards\/","title":{"rendered":"\u89e3\u9396 MQTT Wildcards ( \u901a\u914d\u7b26 )\u7684\u529b\u91cf | \u6df1\u5165\u89e3\u6790 + \u548c #"},"content":{"rendered":"\n<hr class=\"wp-block-separator alignwide has-text-color has-palette-color-1-color has-alpha-channel-opacity has-palette-color-1-background-color has-background is-style-wide\"\/>\n\n\n\n<p>\u5728\u7269\u806f\u7db2\uff08IoT\uff09\u9818\u57df\u4e2d\uff0cMQTT\uff08\u6d88\u606f\u968a\u5217\u9059\u6e2c\u50b3\u8f38\uff09\u4f5c\u70ba\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u6d88\u606f\u50b3\u905e\u5354\u8b70\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u8a2d\u5099\u4e4b\u9593\u7684\u901a\u4fe1\u3002\u5b83\u57fa\u65bc <strong>\u767c\u5e03\/\u8a02\u95b1\u6a21\u5f0f<\/strong>\uff0c\u5229\u7528\u4e3b\u984c\uff08Topic\uff09\u4f86\u7d44\u7e54\u6d88\u606f\u6d41\u3002\u5c0d\u958b\u767c\u8005\u800c\u8a00\uff0c\u4e3b\u984c\u7684\u8a2d\u8a08\u548c\u8a02\u95b1\u6a21\u5f0f\u81f3\u95dc\u91cd\u8981\uff0c\u9019\u88e1\u7684 <strong>MQTT Wildcards (\u901a\u914d\u7b26)<\/strong> \u8d77\u5230\u4e86\u6975\u5176\u91cd\u8981\u7684\u4f5c\u7528\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u66fe\u7d93\u5728 <a href=\"https:\/\/mqtt.org\" target=\"_blank\" rel=\"noopener\">MQTT<\/a> \u4e2d\u8655\u7406\u904e\u4e3b\u984c\u8a02\u95b1\uff0c\u4f60\u5c31\u6703\u77e5\u9053\uff0c\u8a02\u95b1\u67d0\u4e00\u500b\u7279\u5b9a\u4e3b\u984c\u53ef\u80fd\u6703\u8b8a\u5f97\u7e41\u7463\uff0c\u5c24\u5176\u7576\u4f60\u7684\u7cfb\u7d71\u6709\u5f88\u591a\u8b8a\u91cf\u548c\u5c64\u7d1a\u6642\u3002\u9019\u6642\uff0c<strong>\u901a\u914d\u7b26<\/strong>\u5c31\u6210\u70ba\u4e86\u5f37\u5927\u7684\u5de5\u5177\uff0c\u5e6b\u52a9\u4f60\u7c21\u5316\u4e3b\u984c\u8a02\u95b1\uff0c\u8b93\u4f60\u7684\u7cfb\u7d71\u8b8a\u5f97\u66f4\u52a0\u9748\u6d3b\u4e14\u6613\u65bc\u64f4\u5c55\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2024\/11\/Unlocking-the-Power-of-MQTT-Wildcards-A-Deep-Dive-into-and-.jpg\" alt=\"MQTT Wildcards\" class=\"wp-image-2939\" title=\"\" srcset=\"https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2024\/11\/Unlocking-the-Power-of-MQTT-Wildcards-A-Deep-Dive-into-and-.jpg 1024w, https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2024\/11\/Unlocking-the-Power-of-MQTT-Wildcards-A-Deep-Dive-into-and--768x768.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>\u5167\u5bb9<\/h2><nav><ul><li class=\"\"><a href=\"#\u4ec0\u9ebc\u662f-mqtt-wildcards-\u901a\u914d\u7b26-\uff1f\">\u4ec0\u9ebc\u662f MQTT Wildcards (\u901a\u914d\u7b26)<\/a><\/li><li class=\"\"><a href=\"#mqtt-wildcards-\u901a\u914d\u7b26-\u7684\u8a73\u7d30\u89e3\u6790\">\u901a\u914d\u7b26\u7684\u8a73\u7d30\u89e3\u6790<\/a><\/li><li class=\"\"><a href=\"#3-\u548c-\u7684\u5340\u5225\">\u8868\u683c\u8aaa\u660e + \u548c # \u7684\u5340\u5225<\/a><\/li><li class=\"\"><a href=\"#\u5982\u4f55\u5728-mqtt-\u4e2d\u4f7f\u7528\u9019\u4e9b-mqtt-wildcards-\u901a\u914d\u7b26\">\u5982\u4f55\u5728 MQTT \u4e2d\u4f7f\u7528\u9019\u4e9b\u901a\u914d\u7b26<\/a><\/li><li class=\"\"><a href=\"#\u7a0b\u5f0f\u78bc\u8aaa\u660e\">\u7a0b\u5f0f\u78bc\u8aaa\u660e<\/a><\/li><li class=\"\"><a href=\"#\u8f38\u51fa\">\u8f38\u51fa<\/a><\/li><li class=\"\"><a href=\"#\u70ba\u4ec0\u9ebc\u4f7f\u7528-mqtt-wildcards-\u901a\u914d\u7b26-\uff1f\">\u70ba\u4ec0\u9ebc\u4f7f\u7528\u901a\u914d\u7b26<\/a><\/li><li class=\"\"><a href=\"#\u5be6\u969b\u6848\u4f8b\uff1a\u667a\u80fd\u5bb6\u5c45\u7cfb\u7d71\">\u667a\u80fd\u5bb6\u5c45\u7cfb\u7d71<\/a><\/li><li class=\"\"><a href=\"#\u7d50\u8a9e\">\u7d50\u8ad6<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4ec0\u9ebc\u662f-mqtt-wildcards-\u901a\u914d\u7b26-\uff1f\">\u4ec0\u9ebc\u662f <strong>MQTT Wildcards<\/strong> (\u901a\u914d\u7b26)<\/h2>\n\n\n\n<p>\u5728 MQTT \u4e2d\uff0c<strong>\u4e3b\u984c<\/strong> \u662f\u7528\u4f86\u6a19\u8b58\u8a0a\u606f\u6d41\u7684\u201c\u5730\u5740\u201d\u3002\u6bcf\u500b\u8a2d\u5099\u3001\u611f\u6e2c\u5668\u6216\u61c9\u7528\u7a0b\u5f0f\u90fd\u6703\u6839\u64da\u7279\u5b9a\u7684\u4e3b\u984c\u4f86\u767c\u9001\u6216\u63a5\u6536\u6d88\u606f\u3002\u7136\u800c\uff0c\u6709\u6642\u5019\u4f60\u4e26\u4e0d\u60f3\u8a02\u95b1\u67d0\u4e00\u500b\u5177\u9ad4\u7684\u4e3b\u984c\uff0c\u800c\u662f\u5e0c\u671b\u63a5\u6536\u7b26\u5408\u67d0\u4e9b\u6a21\u5f0f\u7684\u591a\u500b\u4e3b\u984c\u3002\u9019\u6642\u5019\uff0c<strong>\u901a\u914d\u7b26<\/strong>\u5c31\u6d3e\u4e0a\u7528\u5834\u4e86\u3002<\/p>\n\n\n\n<p><strong>MQTT Wildcards (\u901a\u914d\u7b26)<\/strong> \u5141\u8a31\u4f60\u5728\u8a02\u95b1\u6642\u5339\u914d\u591a\u500b\u4e3b\u984c\uff0c\u9019\u4e0d\u50c5\u80fd\u8b93\u4f60\u6e1b\u5c11\u8a02\u95b1\u7684\u6578\u91cf\uff0c\u9084\u80fd\u589e\u5f37\u7cfb\u7d71\u7684\u53ef\u64f4\u5c55\u6027\u548c\u9748\u6d3b\u6027\u3002MQTT \u652f\u63f4\u5169\u7a2e\u5e38\u7528\u7684\u901a\u914d\u7b26\uff1a<strong>\u55ae\u5c64\u901a\u914d\u7b26\uff08<code>+<\/code>\uff09<\/strong> \u548c <strong>\u591a\u5c64\u901a\u914d\u7b26\uff08<code>#<\/code>\uff09<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"mqtt-wildcards-\u901a\u914d\u7b26-\u7684\u8a73\u7d30\u89e3\u6790\">\u901a\u914d\u7b26\u7684\u8a73\u7d30\u89e3\u6790<\/h2>\n\n\n\n<p class=\"has-palette-color-2-color has-text-color has-link-color has-medium-font-size wp-elements-df07e1a0be049fc4a1733419b6637ce7\"><strong>\u55ae\u5c64\u901a\u914d\u7b26 <code>+<\/code><\/strong><\/p>\n\n\n\n<p><strong><code>+<\/code><\/strong> \u662f\u4e00\u500b\u55ae\u5c64 <strong>MQTT Wildcard (\u901a\u914d\u7b26)<\/strong>\uff0c\u5b83\u80fd\u5920\u5339\u914d\u4e3b\u984c\u4e2d\u7684\u4efb\u610f\u4e00\u5c64\uff0c\u4f46\u53ea\u9650\u65bc\u55ae\u5c64\u3002\u5982\u679c\u4e3b\u984c\u7d50\u69cb\u4e2d\u6709\u591a\u5c64\uff0c<code>+<\/code> \u53ea\u80fd\u5339\u914d\u4e00\u5c64\uff0c\u4e0d\u80fd\u8de8\u5c64\u5339\u914d\u3002<\/p>\n\n\n\n<p><em>\u5047\u8a2d\u4f60\u7684\u4e3b\u984c\u7d50\u69cb\u5982\u4e0b\uff1a<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>home\/livingroom\/temperature<\/code><\/li>\n\n\n\n<li><code>home\/kitchen\/temperature<\/code><\/li>\n\n\n\n<li><code>home\/livingroom\/humidity<\/code><\/li>\n<\/ul>\n\n\n\n<p><em>\u5982\u679c\u4f60\u8a02\u95b1 <code>home\/+\/temperature<\/code>\uff0c\u4f60\u6703\u6536\u5230\u4ee5\u4e0b\u6d88\u606f\uff1a<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>home\/livingroom\/temperature<\/code><\/li>\n\n\n\n<li><code>home\/kitchen\/temperature<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u4f46 <strong><code>home\/livingroom\/humidity<\/code><\/strong> \u4e0d\u6703\u5339\u914d\uff0c\u56e0\u70ba <code>+<\/code> \u53ea\u6703\u5339\u914d <code>temperature<\/code> \u5c64\u7d1a\uff0c\u4e26\u4e14\u7121\u6cd5\u8de8\u8d8a\u5b50\u5c64\u3002<\/p>\n\n\n\n<p><em>\u4f7f\u7528\u5834\u666f :<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7576\u4f60\u60f3\u8981\u8a02\u95b1\u67d0\u4e00\u5c64\u7d1a\u7684\u6240\u6709\u5b50\u4e3b\u984c\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>+<\/code> \u4f86\u4ee3\u66ff\u5177\u9ad4\u7684\u5c64\u7d1a\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff0c\u5728\u5bb6\u5ead\u81ea\u52d5\u5316\u7cfb\u7d71\u4e2d\uff0c\u4f60\u53ef\u4ee5\u8a02\u95b1 <code>home\/+\/temperature<\/code>\uff0c\u9019\u6a23\u4f60\u5c31\u80fd\u63a5\u6536\u5230\u6bcf\u500b\u623f\u9593\u7684\u6eab\u5ea6\u6578\u64da\uff0c\u800c\u4e0d\u9700\u8981\u70ba\u6bcf\u500b\u623f\u9593\u5206\u5225\u8a02\u95b1\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"has-palette-color-2-color has-text-color has-link-color has-medium-font-size wp-elements-e91bacda2948466b209d4092ba3555fd\"><strong>\u591a\u5c64\u901a\u914d\u7b26 <code>#<\/code><\/strong><\/p>\n\n\n\n<p><strong><code>#<\/code><\/strong> \u662f\u4e00\u500b\u591a\u5c64 <strong>MQTT Wildcard (\u901a\u914d\u7b26)<\/strong>\uff0c\u80fd\u5920\u5339\u914d\u4efb\u610f\u5c64\u7d1a\u7684\u6240\u6709\u5b50\u5c64\u7d1a\u3002\u9019\u662f MQTT \u4e2d\u6700\u5f37\u5927\u7684\u901a\u914d\u7b26\uff0c\u5c24\u5176\u5728\u6709\u5927\u91cf\u8a2d\u5099\u6216\u591a\u5c64\u7d1a\u4e3b\u984c\u7d50\u69cb\u6642\uff0c\u80fd\u5920\u986f\u8457\u7c21\u5316\u8a02\u95b1\u904e\u7a0b\u3002<strong><code>#<\/code> \u53ea\u80fd\u51fa\u73fe\u5728\u4e3b\u984c\u7684\u7d50\u5c3e<\/strong>\uff0c\u5982\u679c\u51fa\u73fe\u5728\u5176\u4ed6\u4f4d\u7f6e\uff0c\u8a02\u95b1\u6703\u5931\u6557\u3002<\/p>\n\n\n\n<p><em>\u5047\u8a2d\u4f60\u7684\u4e3b\u984c\u7d50\u69cb\u5982\u4e0b\uff1a<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>home\/livingroom\/temperature<\/code><\/li>\n\n\n\n<li><code>home\/kitchen\/temperature<\/code><\/li>\n\n\n\n<li><code>home\/livingroom\/temperature\/room1<\/code><\/li>\n\n\n\n<li><code>home\/livingroom\/humidity<\/code><\/li>\n<\/ul>\n\n\n\n<p><em>\u5982\u679c\u4f60\u8a02\u95b1 <code>home\/#<\/code>\uff0c\u4f60\u5c07\u6703\u6536\u5230\uff1a<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>home\/livingroom\/temperature<\/code><\/li>\n\n\n\n<li><code>home\/kitchen\/temperature<\/code><\/li>\n\n\n\n<li><code>home\/livingroom\/temperature\/room1<\/code><\/li>\n\n\n\n<li><code>home\/livingroom\/humidity<\/code><\/li>\n<\/ul>\n\n\n\n<p><em>\u9019\u662f\u56e0\u70ba <code>#<\/code> \u5339\u914d\u4e86 <code>home\/<\/code> \u5f8c\u7684\u6240\u6709\u5b50\u4e3b\u984c\u3002<\/em><\/p>\n\n\n\n<p><em>\u4f7f\u7528\u5834\u666f :<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7576\u4f60\u5e0c\u671b\u63a5\u6536\u67d0\u500b\u7bc4\u570d\u5167\u7684\u6240\u6709\u6d88\u606f\u6642\uff0c<code>#<\/code> \u662f\u7406\u60f3\u7684\u9078\u64c7\u3002\u6bd4\u5982\u4f60\u6709\u5f88\u591a\u623f\u9593\uff0c\u4e26\u5e0c\u671b\u63a5\u6536\u6240\u6709\u623f\u9593\u7684\u6578\u64da\uff0c\u53ef\u4ee5\u8a02\u95b1 <code>home\/#<\/code>\uff0c\u9019\u6a23\u4e0d\u7ba1\u6709\u591a\u5c11\u623f\u9593\uff0c\u6240\u6709\u4ee5 <code>home\/<\/code> \u958b\u982d\u7684\u6578\u64da\u90fd\u6703\u81ea\u52d5\u9032\u4f86\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-\u548c-\u7684\u5340\u5225\">\u8868\u683c\u8aaa\u660e <code>+<\/code> \u548c <code>#<\/code> \u7684\u5340\u5225<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u55ae\u5c64 <strong>MQTT Wildcard (\u901a\u914d\u7b26)<\/strong> <code>+<\/code><\/th><th>\u591a\u5c64 <strong>MQTT Wildcard (\u901a\u914d\u7b26)<\/strong> <code>#<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u5339\u914d\u7bc4\u570d<\/td><td>\u53ea\u80fd\u5339\u914d\u4e00\u5c64<\/td><td>\u5339\u914d\u5f9e\u6307\u5b9a\u5c64\u7d1a\u958b\u59cb\u7684\u6240\u6709\u5c64\u7d1a<\/td><\/tr><tr><td>\u4f4d\u7f6e\u9650\u5236<\/td><td>\u53ef\u4ee5\u51fa\u73fe\u5728\u4e3b\u984c\u7684\u4efb\u4f55\u5c64\u7d1a<\/td><td>\u53ea\u80fd\u51fa\u73fe\u5728\u4e3b\u984c\u7684\u672b\u5c3e<\/td><\/tr><tr><td>\u5178\u578b\u7528\u6cd5<\/td><td>\u7576\u4f60\u53ea\u95dc\u5fc3\u67d0\u4e00\u5c64\u7d1a\u6642<\/td><td>\u7576\u4f60\u9700\u8981\u8a02\u95b1\u591a\u5c64\u7d1a\u6642<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5982\u4f55\u5728-mqtt-\u4e2d\u4f7f\u7528\u9019\u4e9b-mqtt-wildcards-\u901a\u914d\u7b26\">\u5982\u4f55\u5728 MQTT \u4e2d\u4f7f\u7528\u9019\u4e9b<strong>\u901a\u914d\u7b26<\/strong><\/h2>\n\n\n\n<p>\u8b93\u6211\u5011\u4f86\u770b\u770b\u5982\u4f55\u4f7f\u7528 Python \u5ba2\u6236\u7aef\u4f86\u8a02\u95b1\u5e36\u6709<strong>\u901a\u914d\u7b26<\/strong>\u7684\u4e3b\u984c\u3002\u9019\u88e1\u6211\u5011\u4f7f\u7528 <a href=\"https:\/\/github.com\/eclipse-paho\/paho.mqtt.python\" target=\"_blank\" rel=\"noopener\"><strong>paho-mqtt<\/strong> <\/a>\u5eab\u4f86\u5be6\u73fe\u3002<\/p>\n\n\n\n<p><strong>\u9996\u5148\uff0c<a href=\"https:\/\/saludpcb.com\/zh\/python-tutorial-mqtt\/\">\u5b89\u88dd <code>paho-mqtt<\/code><\/a> \u5ba2\u6236\u7aef\u5eab<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">pip install paho-mqtt<\/code><\/pre>\n\n\n\n<p><strong>\u7bc4\u4f8b\u4ee3\u78bc<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import paho.mqtt.client as mqtt\nimport warnings\n\n# Disable DeprecationWarnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)\n\n# Set the MQTT broker address (using HiveMQ broker)\nbroker = \"broker.hivemq.com\"\n\n# Set the topic to subscribe to\ntopic = \"home\/+\/temperature\"\n\n# Callback function that gets triggered when a message is received\ndef on_message(client, userdata, message):\n    # Decode the payload (message content)\n    payload = message.payload.decode()\n    print(f\"Received message: {payload}\")\n    \n    # If the message contains 'Temperature', extract and print the temperature value\n    if \"Temperature\" in payload:\n        # Extract the numeric part of the temperature (e.g., \"23\u00b0C\")\n        temperature = float(payload.split(\":\")[1].strip().replace(\"\u00b0C\", \"\"))\n        print(f\"Temperature value: {temperature}\u00b0C\")\n    \n    # If the message is a numeric value (e.g., 29.60), process it\n    elif payload.replace(\".\", \"\", 1).isdigit():\n        value = float(payload)  # Convert the string to a float\n        print(f\"Numeric value: {value}\")\n    \n    # If the message does not match any expected pattern\n    else:\n        print(\"Received message in unexpected format\")\n\n# Create an MQTT client instance\nclient = mqtt.Client(client_id=\"pythonClient\", protocol=mqtt.MQTTv5)\nclient.on_message = on_message  # Assign the callback function to handle incoming messages\n\n# Connect to the MQTT broker\nclient.connect(broker)\n\n# Subscribe to the topic with a wildcard to capture all temperature readings\nclient.subscribe(topic)\n\n# Keep the client running to wait for messages\nclient.loop_forever()\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7a0b\u5f0f\u78bc\u8aaa\u660e\">\u7a0b\u5f0f\u78bc\u8aaa\u660e<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>warnings.filterwarnings(\"ignore\", category=DeprecationWarning)<\/code><\/strong>\uff1a\u9019\u4e00\u884c\u7528\u4f86\u5ffd\u7565 DeprecationWarning \u8b66\u544a\u8a0a\u606f\uff0c\u9632\u6b62\u5b83\u5728\u63a7\u5236\u53f0\u4e2d\u5217\u5370\u3002<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>on_message<\/code><\/strong>\uff1a\u9019\u500b\u56de\u547c\u51fd\u6578\u6703\u5728\u63a5\u6536\u5230MQTT \u8a0a\u606f\u6642\u89f8\u767c\uff0c\u5b83\u9996\u5148\u5217\u5370\u63a5\u6536\u5230\u7684\u8a0a\u606f\u5167\u5bb9\uff0c\u5982\u679c\u8a0a\u606f\u5305\u542bTemperature \u5b57\u4e32\uff0c\u5b83\u6703\u63d0\u53d6\u51fa\u6eab\u5ea6\u8cc7\u6599\uff08\u5047\u8a2d\u8a0a\u606f\u683c\u5f0f\u70baTemperature: 23\u00b0C\uff09\uff0c\u5982\u679c\u8a0a\u606f\u662f\u4e00\u500b\u6578\u5b57\uff08\u598229.60\uff09\uff0c\u5247\u5c07\u5176\u4f5c\u70ba\u6d6e\u52d5\u6578\u503c\u8655\u7406\uff0c\u4e26\u53ef\u4ee5\u5728else \u4e2d\u52a0\u5165\u66f4\u591a\u7684\u908f\u8f2f\u4f86\u8655\u7406\u5176\u4ed6\u985e\u578b\u7684\u8a0a\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>client.connect(broker)<\/code><\/strong>\uff1a\u9023\u63a5\u5230\u6307\u5b9a\u7684 MQTT \u4f3a\u670d\u5668\u3002<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>client.subscribe(topic)<\/code><\/strong>\uff1a\u8a02\u95b1\u4e00\u500b\u6216\u591a\u500b\u4e3b\u984c\u3002\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u4f7f\u7528\u4e86\u4e00\u500b\u6709\u842c\u7528\u5b57\u5143\u7684\u4e3b\u984c &#8220;home\/+\/temperature&#8221;\uff0c\u9019\u8868\u793a\u8a02\u95b1\u6240\u6709 &#8220;home\/\u88dd\u7f6e\u985e\u578b\/temperature&#8221; \u683c\u5f0f\u7684\u4e3b\u984c\u3002<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>client.loop_forever()<\/code><\/strong>\uff1a\u555f\u52d5 MQTT \u7528\u6236\u7aef\u7684\u8ff4\u5708\uff0c\u4f7f\u5176\u80fd\u5920\u6301\u7e8c\u63a5\u6536\u4e26\u8655\u7406\u8a0a\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u8f38\u51fa\">\u8f38\u51fa<\/h2>\n\n\n\n<p>\u5047\u8a2d MQTT \u4f3a\u670d\u5668\u767c\u5e03\u77ad\u5982\u4e0b\u8a0a\u606f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"Temperature: 23\u00b0C\"<\/code><\/li>\n\n\n\n<li><code>\"29.60\"<\/code><\/li>\n\n\n\n<li><code>\"2\"<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u904b\u884c\u6642\u7684\u8f38\u51fa\u5c07\u985e\u4f3c\u65bc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">Received message: Temperature: 23\u00b0C\nTemperature value: 23.0\u00b0C\nReceived message: 29.60\nNumeric value: 29.6\nReceived message: 2\nNumeric value: 2.0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u70ba\u4ec0\u9ebc\u4f7f\u7528-mqtt-wildcards-\u901a\u914d\u7b26-\uff1f\">\u70ba\u4ec0\u9ebc\u4f7f\u7528<strong>\u901a\u914d\u7b26<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6e1b\u5c11\u8a02\u95b1\u6578\u91cf<\/strong><br><strong>\u901a\u914d\u7b26<\/strong>\u8b93\u4f60\u53ef\u4ee5\u5728\u4e0d\u9700\u8981\u7cbe\u78ba\u6307\u5b9a\u6bcf\u500b\u8a2d\u5099\u7684\u60c5\u6cc1\u4e0b\uff0c\u7c21\u5316\u8a02\u95b1\u904e\u7a0b\u3002\u4f60\u53ea\u9700\u8981\u8a02\u95b1\u66f4\u9ad8\u5c64\u7d1a\u7684\u4e3b\u984c\uff0c\u5c31\u80fd\u63a5\u6536\u5230\u4f86\u81ea\u4e0d\u540c\u8a2d\u5099\u7684\u6d88\u606f\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u9ad8\u9748\u6d3b\u6027<\/strong><br>\u96a8\u8457\u7cfb\u7d71\u7684\u64f4\u5c55\uff0c\u50b3\u7d71\u7684\u624b\u52d5\u8a02\u95b1\u65b9\u6cd5\u53ef\u80fd\u6703\u8b8a\u5f97\u5197\u9577\u4e14\u96e3\u4ee5\u7ba1\u7406\u3002\u800c\u4f7f\u7528<strong>\u901a\u914d\u7b26<\/strong>\uff0c\u4f60\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u8655\u7406\u4e0d\u540c\u5c64\u7d1a\u7684\u6d88\u606f\u6d41\uff0c\u907f\u514d\u786c\u7de8\u78bc\u6bcf\u500b\u8a2d\u5099\u7684\u4e3b\u984c\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u5347\u53ef\u7dad\u8b77\u6027<\/strong><br><strong>\u901a\u914d\u7b26<\/strong>\u4f7f\u5f97\u5728\u9700\u8981\u6dfb\u52a0\u65b0\u7684\u8a2d\u5099\u6216\u65b0\u7684\u529f\u80fd\u6642\uff0c\u7121\u9700\u66f4\u6539\u8a02\u95b1\u908f\u8f2f\u3002\u53ea\u9700\u78ba\u4fdd\u65b0\u8a2d\u5099\u7684\u4e3b\u984c\u7d50\u69cb\u7b26\u5408\u5df2\u6709\u7684\u898f\u7bc4\u5373\u53ef\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5be6\u969b\u6848\u4f8b\uff1a\u667a\u80fd\u5bb6\u5c45\u7cfb\u7d71\">\u667a\u80fd\u5bb6\u5c45\u7cfb\u7d71<\/h2>\n\n\n\n<p>\u5047\u8a2d\u4f60\u6709\u4e00\u500b\u667a\u80fd\u5bb6\u5c45\u7cfb\u7d71\uff0c\u6d89\u53ca\u591a\u500b\u623f\u9593\uff0c\u6bcf\u500b\u623f\u9593\u6709\u4e0d\u540c\u7684\u611f\u6e2c\u5668\uff0c\u5982\u6eab\u5ea6\u3001\u6fd5\u5ea6\u3001\u5149\u7dda\u7b49\u3002\u50b3\u7d71\u65b9\u6cd5\u6703\u8b93\u4f60\u70ba\u6bcf\u500b\u623f\u9593\u7684\u6bcf\u500b\u611f\u6e2c\u5668\u5206\u5225\u8a02\u95b1\u4e3b\u984c\uff0c\u9019\u6703\u9020\u6210\u8a02\u95b1\u6578\u91cf\u6fc0\u589e\uff0c\u4e26\u96e3\u4ee5\u7dad\u8b77\u3002\u4f7f\u7528 <strong>MQTT Wildcards (\u901a\u914d\u7b26)<\/strong> \u5f8c\uff0c\u4f60\u53ea\u9700\u8981\u8a02\u95b1\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>home\/+\/temperature<\/code>\uff1a\u63a5\u6536\u6240\u6709\u623f\u9593\u7684\u6eab\u5ea6\u6578\u64da\u3002<\/li>\n\n\n\n<li><code>home\/#<\/code>\uff1a\u63a5\u6536\u6240\u6709\u623f\u9593\u7684\u6240\u6709\u6578\u64da\uff08\u6eab\u5ea6\u3001\u6fd5\u5ea6\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u9019\u6a23\uff0c\u4f60\u7684\u8a02\u95b1\u5c31\u8b8a\u5f97\u66f4\u7c21\u55ae\uff0c\u4e14\u80fd\u8f15\u9b06\u61c9\u5c0d\u65b0\u7684\u623f\u9593\u6216\u8a2d\u5099\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7d50\u8a9e\">\u7d50\u8ad6<\/h2>\n\n\n\n<p>MQTT \u7684 **\u901a\u914d\u7b26** \u529f\u80fd\u662f\u958b\u767c\u8005\u5728\u69cb\u5efa\u7269\u806f\u7db2\u7cfb\u7d71\u6642\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u7121\u8ad6\u662f\u55ae\u5c64 <strong>MQTT Wildcard (\u901a\u914d\u7b26)<\/strong> <code>+<\/code> \u9084\u662f\u591a\u5c64 <strong>MQTT Wildcard (\u901a\u914d\u7b26)<\/strong> <code>#<\/code>\uff0c\u5b83\u5011\u90fd\u80fd\u5e6b\u52a9\u4f60\u7c21\u5316\u8a02\u95b1\u908f\u8f2f\uff0c\u63d0\u5347\u7cfb\u7d71\u7684\u9748\u6d3b\u6027\u548c\u53ef\u64f4\u5c55\u6027\u3002\u7406\u89e3\u548c\u5408\u7406\u4f7f\u7528\u9019\u4e9b <strong>MQTT Wildcards (\u901a\u914d\u7b26)<\/strong>\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u4e26\u70ba\u672a\u4f86\u7684\u64f4\u5c55\u548c\u7dad\u8b77\u5960\u5b9a\u5805\u5be6\u57fa\u790e\u3002<\/p>\n\n\n\n<p>\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5920\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528 MQTT \u7684 <strong>MQTT Wildcards (\u901a\u914d\u7b26)<\/strong>\uff0c\u4e26\u63d0\u5347\u4f60\u5728\u7269\u806f\u7db2\u958b\u767c\u4e2d\u7684\u80fd\u529b\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator alignwide has-text-color has-palette-color-1-color has-alpha-channel-opacity has-palette-color-1-background-color has-background is-style-wide\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7269\u806f\u7db2\uff08IoT\uff09\u9818\u57df\u4e2d\uff0cMQTT\uff08\u6d88\u606f\u968a\u5217\u9059\u6e2c\u50b3\u8f38\uff09\u4f5c\u70ba\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u6d88\u606f\u50b3\u905e\u5354\u8b70\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u8a2d\u5099\u4e4b\u9593\u7684\u901a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2940,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[27,21,82,44,9,11],"class_list":["post-2931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-c","tag-ide","tag-mqtt","tag-programming-language","tag-python","tag-tutorial"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/2931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/comments?post=2931"}],"version-history":[{"count":20,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/2931\/revisions"}],"predecessor-version":[{"id":3202,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/2931\/revisions\/3202"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/media\/2940"}],"wp:attachment":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/media?parent=2931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/categories?post=2931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/tags?post=2931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}