{"id":760,"date":"2024-11-07T12:10:49","date_gmt":"2024-11-07T04:10:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/760\/"},"modified":"2024-11-07T12:10:49","modified_gmt":"2024-11-07T04:10:49","slug":"php-%e5%87%bd%e6%95%b0%e5%9c%a8%e7%89%a9%e8%81%94%e7%bd%91%e9%a2%86%e5%9f%9f%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8%e6%8e%a2%e7%b4%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/760\/","title":{"rendered":"PHP \u51fd\u6570\u5728\u7269\u8054\u7f51\u9886\u57df\u4e2d\u7684\u5e94\u7528\u63a2\u7d22"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1153\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172827756218373.jpg\" width=\"720\" height=\"288\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172827756218373.jpg 720w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172827756218373-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172827756218373-670x268.jpg 670w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" title=\"PHP \u51fd\u6570\u5728\u7269\u8054\u7f51\u9886\u57df\u4e2d\u7684\u5e94\u7528\u63a2\u7d22\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5728\u7269\u8054\u7f51\u9886\u57df\u4e2d\u7684\u5e94\u7528\u63a2\u7d22\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5728\u7269\u8054\u7f51\u9886\u57df\u4e2d\u7684\u5e94\u7528\u63a2\u7d22<\/strong><\/p>\n<p>\u7269\u8054\u7f51\uff08IoT\uff09\u5df2\u6210\u4e3a\u6211\u4eec\u65e5\u5e38\u751f\u6d3b\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u8fde\u63a5\u548c\u63a7\u5236\u8bbe\u5907\u7684\u5f3a\u5927\u5de5\u5177\u3002PHP \u4f5c\u4e3a\u4e00\u79cd\u901a\u7528\u7f16\u7a0b\u8bed\u8a00\uff0c\u5728\u7269\u8054\u7f51\u9886\u57df\u4e5f\u53d1\u6325\u7740\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n<p><strong>\u4e00\u3001\u4f20\u611f\u5668\u6570\u636e\u83b7\u53d6<\/strong><\/p>\n<p>file_get_contents() \u51fd\u6570\u53ef\u7528\u4e8e\u4ece\u4f20\u611f\u5668\u8bfb\u53d6\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6\u6e29\u5ea6\u4f20\u611f\u5668 \/sys\/bus\/w1\/devices\/28-011580a82549\/w1_slave \u7684\u6570\u636e\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cPHP\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<pre>$temperature = file_get_contents(\"\/sys\/bus\/w1\/devices\/28-011580a82549\/w1_slave\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8c\u3001\u6570\u636e\u89e3\u6790<\/strong><\/p>\n<p>\u8bfb\u53d6\u7684\u6570\u636e\u901a\u5e38\u662f\u539f\u59cb\u7684\u6216\u4e0d\u53ef\u8bfb\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6570\u6765\u89e3\u6790\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u89e3\u6790\u4ee5\u4e0a\u8bfb\u53d6\u7684\u6e29\u5ea6\u6570\u636e\uff1a<\/p>\n<pre>preg_match('\/t=(d+)\/', $temperature, $matches);\n$temperature = $matches[1] \/ 1000;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e09\u3001\u8bbe\u5907\u63a7\u5236<\/strong><\/p>\n<p>shell_exec() \u51fd\u6570\u53ef\u7528\u4e8e\u5411\u5916\u90e8\u5e94\u7528\u7a0b\u5e8f\u53d1\u9001\u547d\u4ee4\uff0c\u4f8b\u5982\u63a7\u5236\u8bbe\u5907\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528 GPIO \u5e93\u63a7\u5236 LED\uff1a<\/p>\n<pre>shell_exec(\"\/usr\/bin\/gpio write 7 1\"); \/\/ \u6253\u5f00 LED\nshell_exec(\"\/usr\/bin\/gpio write 7 0\"); \/\/ \u5173\u95ed LED<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u56db\u3001\u6570\u636e\u53d1\u9001<\/strong><\/p>\n<p>MQTT \u662f IoT \u4e2d\u5e38\u7528\u7684\u534f\u8bae\uff0c\u7528\u4e8e\u8bbe\u5907\u95f4\u7684\u6570\u636e\u4ea4\u6362\u3002PHP \u4e2d\u6709\u8bb8\u591a MQTT \u5e93\uff0c\u4f8b\u5982 php-mqtt\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6570\u636e\u53d1\u9001\uff1a<\/p>\n<pre>use PhpMqttClientMqttClient;\n\n$client = new MqttClient('127.0.0.1', 1883);\n$client-&amp;gt;connect();\n$client-&amp;gt;publish('topic\/test', 'Hello, IoT');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ed3\u8bed<\/strong><\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u5e94\u7528\uff0cPHP \u51fd\u6570\u5728\u7269\u8054\u7f51\u9886\u57df\u53d1\u6325\u4e86\u4e0d\u53ef\u66ff\u4ee3\u7684\u4f5c\u7528\uff0c\u5b83\u7b80\u5316\u4e86\u8bbe\u5907\u8fde\u63a5\u3001\u6570\u636e\u89e3\u6790\u3001\u8bbe\u5907\u63a7\u5236\u548c\u6570\u636e\u53d1\u9001\u7b49\u4efb\u52a1\uff0c\u4e3a\u7269\u8054\u7f51\u5f00\u53d1\u63d0\u4f9b\u4e86\u5f3a\u5927\u652f\u6301\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fPHP \u51fd\u6570\u5728\u7269\u8054\u7f51\u9886\u57df\u4e2d\u7684\u5e94\u7528\u63a2\u7d22\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5728\u7269\u8054\u7f51\u9886\u57df\u4e2d\u7684\u5e94\u7528\u63a2\u7d22 \u7269\u8054\u7f51\uff08IoT\uff09\u5df2\u6210\u4e3a\u6211\u4eec\u65e5\u5e38\u751f\u6d3b\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u8fde\u63a5\u548c\u63a7\u5236\u8bbe\u5907\u7684\u5f3a\u5927\u5de5\u5177\u3002PHP \u4f5c\u4e3a\u4e00\u79cd\u901a\u7528\u7f16\u7a0b\u8bed\u8a00\uff0c\u5728\u7269\u8054\u7f51\u9886\u57df\u4e5f\u53d1\u6325\u7740\u91cd\u8981\u4f5c\u7528\u3002 \u4e00\u3001\u4f20\u611f\u5668\u6570\u636e\u83b7\u53d6 file_get_contents() \u51fd\u6570\u53ef\u7528\u4e8e\u4ece\u4f20\u611f\u5668\u8bfb\u53d6\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6\u6e29\u5ea6\u4f20\u611f\u5668 \/sys\/bus\/w1\/devices\/28-011580a82549\/w1_slave \u7684\u6570\u636e\uff1a \u7acb\u5373\u5b66\u4e60\u201cPHP\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b $temperature = file_get_contents(&#8220;\/sys\/bus\/w1\/devices\/28-011580a82549\/w1_slave&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u6570\u636e\u89e3\u6790 \u8bfb\u53d6\u7684\u6570\u636e\u901a\u5e38\u662f\u539f\u59cb\u7684\u6216\u4e0d\u53ef\u8bfb\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6570\u6765\u89e3\u6790\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u89e3\u6790\u4ee5\u4e0a\u8bfb\u53d6\u7684\u6e29\u5ea6\u6570\u636e\uff1a preg_match(&#8216;\/t=(d+)\/&#8217;, $temperature, $matches); $temperature = $matches[1] \/ 1000; \u767b\u5f55\u540e\u590d\u5236 \u4e09\u3001\u8bbe\u5907\u63a7\u5236 shell_exec() \u51fd\u6570\u53ef\u7528\u4e8e\u5411\u5916\u90e8\u5e94\u7528\u7a0b\u5e8f\u53d1\u9001\u547d\u4ee4\uff0c\u4f8b\u5982\u63a7\u5236\u8bbe\u5907\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528 GPIO \u5e93\u63a7\u5236 LED\uff1a shell_exec(&#8220;\/usr\/bin\/gpio write 7 1&#8221;); \/\/ \u6253\u5f00 LED shell_exec(&#8220;\/usr\/bin\/gpio write 7 0&#8221;); \/\/ \u5173\u95ed LED \u767b\u5f55\u540e\u590d\u5236 \u56db\u3001\u6570\u636e\u53d1\u9001 MQTT \u662f IoT \u4e2d\u5e38\u7528\u7684\u534f\u8bae\uff0c\u7528\u4e8e\u8bbe\u5907\u95f4\u7684\u6570\u636e\u4ea4\u6362\u3002PHP \u4e2d\u6709\u8bb8\u591a MQTT \u5e93\uff0c\u4f8b\u5982 php-mqtt\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6570\u636e\u53d1\u9001\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-760","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=760"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/760\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}