{"id":7653,"date":"2020-11-20T11:49:45","date_gmt":"2020-11-20T03:49:45","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=7653"},"modified":"2022-06-07T10:29:00","modified_gmt":"2022-06-07T02:29:00","slug":"mqtt-2-%e5%85%b6%e4%bb%96%e5%ae%a2%e6%88%b6%e7%ab%af","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=7653","title":{"rendered":"MQTT - 2. \u4f7f\u7528\u7a0b\u5f0f\u4ee5\u53ca\u6307\u4ee4"},"content":{"rendered":"<p>--<\/p>\n<h2>\u53c3\u8003<\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.emqx.net\/broker\/latest\/cn\/development\/client.html\" target=\"_blank\" rel=\"noopener\"> MQTT \u5ba2\u6237\u7aef\u5e93<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/php-mqtt\/client\" target=\"_blank\" rel=\"noopener\">php-mqtt \/ client<\/a><\/li>\n<li><a class=\"question-hyperlink\" href=\"https:\/\/stackoverflow.com\/questions\/26165101\/skip-composer-php-requirement\" target=\"_blank\" rel=\"noopener\">Skip composer PHP requirement<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<h2>PHP Publish<\/h2>\n<p>PHP \u7684\u61c9\u7528\u5834\u666f\u53ef\u80fd\u767c\u751f\u5728\u9700\u8981\u5f9e\u7db2\u9801\u767c\u51fa publish \u63a7\u5236\u9700\u6c42\uff0cHoyo \u5c1a\u672a\u5728 PHP \u4e0a\u64b0\u5beb\u5e38\u99d0\u7a0b\u5f0f<\/p>\n<p>\u5b89\u88dd\uff0c\u8df3\u904e PHP \u7248\u672c\u9700\u6c42<\/p>\n<pre class=\"lang:default decode:true\">&gt; composer require php-mqtt\/client --ignore-platform-reqs<\/pre>\n<p>\u7a0b\u5f0f\u7bc4\u4f8b<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\r\nspl_autoload_register(function ($class_name) {\r\n    $load_FileName = str_replace('\\\\', '\/', $class_name);\r\n    $file_name = __DIR__ .\"\/php\/\". $load_FileName .\".php\";\r\n    if ( is_file($file_name) === true ) {\r\n        require_once( $file_name );\r\n    }\r\n});\r\n\r\n$server   = 'test.mosquitto.org';\r\n$port     = 1883;\r\n$clientId = 'test-publisher';\r\n\r\n$mqtt = new \\PhpMqtt\\Client\\MQTTClient($server, $port, $clientId);\r\n$mqtt-&gt;connect();\r\n$mqtt-&gt;publish('aa\/Get\/DemandRange', 'Hello World!', 0);\r\n$mqtt-&gt;close();<\/pre>\n<p>--<\/p>\n<h2>Python<\/h2>\n<p>\u4f7f\u7528\u8b49\u66f8\u9023\u63a5 SSL<\/p>\n<pre class=\"lang:python decode:true\">client = mqtt.Client()\r\nclient.tls_set(ca_certs=\"my_root_ca.pem\", certfile=\"client.pem\", keyfile=\"client.key\")<\/pre>\n<p>\u4e0d\u4f7f\u7528\u8b49\u66f8\u9023\u63a5<\/p>\n<pre class=\"lang:default decode:true\">client.tls_set(cert_reqs=ssl.CERT_NONE)\r\nclient.tls_insecure_set(True)<\/pre>\n<p>\u5b8c\u6574\u7bc4\u4f8b<\/p>\n<pre class=\"lang:python decode:true \">import paho.mqtt.client as mqtt\r\n\r\n# \u7576\u5730\u7aef\u7a0b\u5f0f\u9023\u7dda\u4f3a\u670d\u5668\u5f97\u5230\u56de\u61c9\u6642\uff0c\u8981\u505a\u7684\u52d5\u4f5c\r\ndef on_connect(client, userdata, flags, rc):\r\n    logging.info(\"Connected with result code %s\", str(rc))\r\n    client.subscribe(\"v1\/devices\/me\/rpc\/request\/+\")\r\n\r\n# \u7576\u63a5\u6536\u5230\u5f9e\u4f3a\u670d\u5668\u767c\u9001\u7684\u8a0a\u606f\u6642\u8981\u9032\u884c\u7684\u52d5\u4f5c\r\ndef on_message(client, userdata, msg):\r\n    # logging.info(userdata)\r\n    # \u8f49\u63db\u7de8\u78bcutf-8\u624d\u770b\u5f97\u61c2\u4e2d\u6587\r\n\r\n# \u8a2d\u5b9a\u9023\u7dda\u7684\u52d5\u4f5c\r\nclient.on_connect = on_connect\r\n\r\n# \u8a2d\u5b9a\u63a5\u6536\u8a0a\u606f\u7684\u52d5\u4f5c\r\nclient.on_message = on_message\r\n\r\n# \u8a2d\u5b9a\u767b\u5165\u5e33\u865f\u5bc6\u78bc\r\nclient.username_pw_set(mqtt_username, mqtt_password)\r\n\r\n# \u8a2d\u5b9a\u9023\u7dda\u8cc7\u8a0a(IP, Port, \u9023\u7dda\u6642\u9593)\r\ntry:\r\n    client.connect(mqtt_broker, int(mqtt_port), 60)\r\nexcept Exception as e:\r\n    logging.info(e)\r\n\r\n#\r\ndef loop_forever():\r\n    client.loop_forever()\r\n\r\nm = threading.Thread(target=loop_forever)\r\nm.start()<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<h2>CLI<\/h2>\n<p>CLI \u57f7\u884c\u65b9\u5f0f\u9664\u4e86\u53ef\u4ee5\u6eff\u8db3\u55ae\u6a5f\u57f7\u884c\u5916\uff0c\u8b93 PHP \u76f4\u63a5\u547c\u53eb\u57f7\u884c\u61c9\u8a72\u662f\u66f4\u4f73\u7684\u65b9\u5f0f\uff0c\u5c24\u5176\u662f\u525b\u525b\u4e0a\u8ff0\u7684 PHP Publish \u6548\u80fd\u4e0d\u5f70\u7684\u60c5\u6cc1<\/p>\n<ul>\n<li><a href=\"https:\/\/mosquitto.org\/man\/mosquitto_sub-1.html\" target=\"_blank\" rel=\"noopener\">mosquitto_sub man page<\/a><\/li>\n<\/ul>\n<p>\u56e0\u70ba\u52a0\u4e0a\u4e86 MySQL \u8a8d\u8b49<\/p>\n<pre class=\"lang:default decode:true\"># mosquitto_pub -h 172.16.1.220 -p 1883 -u emqx -P public -t aa\/Get\/DemandRange -m \"0000\"<\/pre>\n<p>\u5b8c\u6574\u7bc4\u4f8b<\/p>\n<pre class=\"lang:default decode:true\"># mosquitto_pub -h 172.16.1.220 -p 8883 -u emqx -P public --cafile my_root_ca.pem --cert client.pem --key client.key -t aa\/Get\/DemandRange -m \"0000\"<\/pre>\n<ul>\n<li>-h host<\/li>\n<li>-p port<\/li>\n<li>-u \u4f7f\u7528\u8005\u5e33\u865f<\/li>\n<li>-P \u4f7f\u7528\u8005\u5bc6\u78bc<\/li>\n<li>--cafile SSL CA \u8b49\u66f8\u6a94\u6848\u8def\u5f91<\/li>\n<li>--cert SSL \u8b49\u66f8\u6a94\u6848\u8def\u5f91<\/li>\n<li>--key SSL Key \u6a94\u6848\u8def\u5f91<\/li>\n<li>-t Topic<\/li>\n<li>-m Message<\/li>\n<\/ul>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7653\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,479&nbsp;total views, &nbsp;1&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u53c3\u8003 MQTT \u5ba2\u6237\u7aef\u5e93...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7653\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,479&nbsp;total views, &nbsp;1&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[319],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7653"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7653"}],"version-history":[{"count":8,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7653\/revisions"}],"predecessor-version":[{"id":10178,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7653\/revisions\/10178"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}