{"id":1152973,"date":"2025-01-13T17:33:10","date_gmt":"2025-01-13T09:33:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152973.html"},"modified":"2025-01-13T17:33:15","modified_gmt":"2025-01-13T09:33:15","slug":"python%e5%a6%82%e4%bd%95%e6%90%ad%e5%bb%ba%e7%89%a9%e8%81%94%e7%bd%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1152973.html","title":{"rendered":"python\u5982\u4f55\u642d\u5efa\u7269\u8054\u7f51"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182837\/502cc4d8-eaf4-4add-b6bd-d77165e54941.webp\" alt=\"python\u5982\u4f55\u642d\u5efa\u7269\u8054\u7f51\" \/><\/p>\n<p><p> <strong>Python\u642d\u5efa\u7269\u8054\u7f51\u7684\u6b65\u9aa4\u5305\u62ec\u9009\u62e9\u786c\u4ef6\u3001\u914d\u7f6e\u7f51\u7edc\u3001\u9009\u62e9\u534f\u8bae\u3001\u7f16\u5199\u4ee3\u7801\u3001\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u3001\u5b9e\u73b0\u5b89\u5168\u6027<\/strong>\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u9009\u62e9\u534f\u8bae\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u534f\u8bae\u662f\u642d\u5efa\u7269\u8054\u7f51\u7684\u5173\u952e\u6b65\u9aa4\u3002\u7269\u8054\u7f51\u8bbe\u5907\u901a\u5e38\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\uff0c\u56e0\u6b64\u9009\u62e9\u5408\u9002\u7684\u901a\u4fe1\u534f\u8bae\u81f3\u5173\u91cd\u8981\u3002\u5e38\u89c1\u7684\u7269\u8054\u7f51\u901a\u4fe1\u534f\u8bae\u5305\u62ecMQTT\u3001CoAP\u3001HTTP\u7b49\u3002MQTT\uff08Message Queuing Telemetry Transport\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6d88\u606f\u4f20\u8f93\u534f\u8bae\uff0c\u975e\u5e38\u9002\u5408\u8d44\u6e90\u53d7\u9650\u7684\u8bbe\u5907\u3002\u5b83\u5177\u6709\u4f4e\u5e26\u5bbd\u5360\u7528\u3001\u53ef\u9760\u4f20\u8f93\u7b49\u7279\u70b9\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7269\u8054\u7f51\u9886\u57df\u3002\u9009\u62e9\u9002\u5f53\u7684\u534f\u8bae\u80fd\u591f\u786e\u4fdd\u6570\u636e\u7684\u9ad8\u6548\u4f20\u8f93\u548c\u8bbe\u5907\u7684\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u786c\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u642d\u5efa\u7269\u8054\u7f51\u7cfb\u7edf\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u8bbe\u5907\u662f\u7b2c\u4e00\u6b65\u3002\u786c\u4ef6\u8bbe\u5907\u5305\u62ec\u4f20\u611f\u5668\u3001\u5fae\u63a7\u5236\u5668\u3001\u7f51\u5173\u7b49\u3002\u4f20\u611f\u5668\u7528\u4e8e\u91c7\u96c6\u6570\u636e\uff0c\u5982\u6e29\u5ea6\u3001\u6e7f\u5ea6\u3001\u538b\u529b\u7b49\u3002\u5fae\u63a7\u5236\u5668\u7528\u4e8e\u5904\u7406\u4f20\u611f\u5668\u91c7\u96c6\u7684\u6570\u636e\uff0c\u5e76\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\u3002\u5e38\u89c1\u7684\u5fae\u63a7\u5236\u5668\u6709Arduino\u3001Raspberry Pi\u7b49\u3002\u7f51\u5173\u7528\u4e8e\u8fde\u63a5\u5fae\u63a7\u5236\u5668\u548c\u4e91\u7aef\u670d\u52a1\u5668\uff0c\u786e\u4fdd\u6570\u636e\u7684\u4f20\u8f93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f20\u611f\u5668\u9009\u62e9<\/li>\n<\/ol>\n<p><p>\u4f20\u611f\u5668\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u7528\u4e8e\u91c7\u96c6\u73af\u5883\u4e2d\u7684\u5404\u79cd\u6570\u636e\u3002\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u9002\u5408\u7684\u4f20\u611f\u5668\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668\u9002\u7528\u4e8e\u73af\u5883\u76d1\u6d4b\uff0c\u6c14\u4f53\u4f20\u611f\u5668\u9002\u7528\u4e8e\u7a7a\u6c14\u8d28\u91cf\u68c0\u6d4b\uff0c\u538b\u529b\u4f20\u611f\u5668\u9002\u7528\u4e8e\u5de5\u4e1a\u81ea\u52a8\u5316\u3002\u9009\u62e9\u4f20\u611f\u5668\u65f6\uff0c\u9700\u8981\u8003\u8651\u5176\u7cbe\u5ea6\u3001\u54cd\u5e94\u65f6\u95f4\u3001\u529f\u8017\u7b49\u53c2\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5fae\u63a7\u5236\u5668\u9009\u62e9<\/li>\n<\/ol>\n<p><p>\u5fae\u63a7\u5236\u5668\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u6838\u5fc3\u5904\u7406\u5355\u5143\uff0c\u8d1f\u8d23\u5904\u7406\u4f20\u611f\u5668\u91c7\u96c6\u7684\u6570\u636e\uff0c\u5e76\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\u3002\u5e38\u89c1\u7684\u5fae\u63a7\u5236\u5668\u6709Arduino\u3001Raspberry Pi\u3001ESP8266\u7b49\u3002\u9009\u62e9\u5fae\u63a7\u5236\u5668\u65f6\uff0c\u9700\u8981\u8003\u8651\u5176\u5904\u7406\u80fd\u529b\u3001\u5b58\u50a8\u5bb9\u91cf\u3001\u529f\u8017\u7b49\u53c2\u6570\u3002\u4f8b\u5982\uff0cRaspberry Pi\u5177\u6709\u5f3a\u5927\u7684\u5904\u7406\u80fd\u529b\u548c\u4e30\u5bcc\u7684\u63a5\u53e3\uff0c\u9002\u5408\u590d\u6742\u7684\u7269\u8054\u7f51\u5e94\u7528\uff1b\u800cESP8266\u5177\u6709\u4f4e\u529f\u8017\u548c\u4f4e\u6210\u672c\u7684\u7279\u70b9\uff0c\u9002\u5408\u7b80\u5355\u7684\u7269\u8054\u7f51\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u914d\u7f6e\u7f51\u7edc<\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u7f51\u7edc\u662f\u642d\u5efa\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u5173\u952e\u6b65\u9aa4\u3002\u7269\u8054\u7f51\u8bbe\u5907\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\uff0c\u56e0\u6b64\u9700\u8981\u914d\u7f6e\u7f51\u7edc\u8fde\u63a5\u3002\u5e38\u89c1\u7684\u7f51\u7edc\u8fde\u63a5\u65b9\u5f0f\u6709Wi-Fi\u3001\u84dd\u7259\u3001ZigBee\u7b49\u3002Wi-Fi\u9002\u7528\u4e8e\u77ed\u8ddd\u79bb\u3001\u5927\u5e26\u5bbd\u7684\u573a\u666f\uff1b\u84dd\u7259\u9002\u7528\u4e8e\u77ed\u8ddd\u79bb\u3001\u4f4e\u529f\u8017\u7684\u573a\u666f\uff1bZigBee\u9002\u7528\u4e8e\u4f4e\u529f\u8017\u3001\u4f4e\u5e26\u5bbd\u7684\u573a\u666f\u3002\u9009\u62e9\u9002\u5408\u7684\u7f51\u7edc\u8fde\u63a5\u65b9\u5f0f\uff0c\u80fd\u591f\u786e\u4fdd\u6570\u636e\u7684\u9ad8\u6548\u4f20\u8f93\u548c\u8bbe\u5907\u7684\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>Wi-Fi\u7f51\u7edc\u914d\u7f6e<\/li>\n<\/ol>\n<p><p>Wi-Fi\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65e0\u7ebf\u7f51\u7edc\u8fde\u63a5\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u77ed\u8ddd\u79bb\u3001\u5927\u5e26\u5bbd\u7684\u573a\u666f\u3002\u914d\u7f6eWi-Fi\u7f51\u7edc\u65f6\uff0c\u9700\u8981\u5c06\u5fae\u63a7\u5236\u5668\u8fde\u63a5\u5230Wi-Fi\u7f51\u7edc\uff0c\u5e76\u914d\u7f6e\u76f8\u5173\u7684\u7f51\u7edc\u53c2\u6570\u3002\u4ee5ESP8266\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\uff0c\u5c06ESP8266\u8fde\u63a5\u5230Wi-Fi\u7f51\u7edc\uff0c\u5e76\u5b9e\u73b0\u6570\u636e\u7684\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import network<\/p>\n<h2><strong>\u914d\u7f6eWi-Fi\u7f51\u7edc<\/strong><\/h2>\n<p>ssid = &quot;your_ssid&quot;<\/p>\n<p>password = &quot;your_password&quot;<\/p>\n<h2><strong>\u8fde\u63a5\u5230Wi-Fi\u7f51\u7edc<\/strong><\/h2>\n<p>station = network.WLAN(network.STA_IF)<\/p>\n<p>station.active(True)<\/p>\n<p>station.connect(ssid, password)<\/p>\n<h2><strong>\u7b49\u5f85\u8fde\u63a5\u6210\u529f<\/strong><\/h2>\n<p>while not station.isconnected():<\/p>\n<p>    pass<\/p>\n<p>print(&quot;Wi-Fi connected&quot;)<\/p>\n<p>print(station.ifconfig())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u84dd\u7259\u7f51\u7edc\u914d\u7f6e<\/li>\n<\/ol>\n<p><p>\u84dd\u7259\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u77ed\u8ddd\u79bb\u3001\u4f4e\u529f\u8017\u7684\u65e0\u7ebf\u7f51\u7edc\u8fde\u63a5\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u4f4e\u529f\u8017\u7684\u7269\u8054\u7f51\u8bbe\u5907\u3002\u914d\u7f6e\u84dd\u7259\u7f51\u7edc\u65f6\uff0c\u9700\u8981\u5c06\u5fae\u63a7\u5236\u5668\u914d\u7f6e\u4e3a\u84dd\u7259\u8bbe\u5907\uff0c\u5e76\u5b9e\u73b0\u6570\u636e\u7684\u4f20\u8f93\u3002\u4ee5Raspberry Pi\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u84dd\u7259\u6a21\u5757\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\uff0c\u5b9e\u73b0\u84dd\u7259\u6570\u636e\u7684\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bluetooth<\/p>\n<h2><strong>\u914d\u7f6e\u84dd\u7259\u8bbe\u5907<\/strong><\/h2>\n<p>bt = bluetooth.Bluetooth()<\/p>\n<p>bt.active(True)<\/p>\n<h2><strong>\u626b\u63cf\u9644\u8fd1\u7684\u84dd\u7259\u8bbe\u5907<\/strong><\/h2>\n<p>devices = bt.discover_devices()<\/p>\n<p>print(&quot;Found devices:&quot;, devices)<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u84dd\u7259\u8bbe\u5907<\/strong><\/h2>\n<p>device_address = &quot;xx:xx:xx:xx:xx:xx&quot;<\/p>\n<p>bt.connect(device_address)<\/p>\n<h2><strong>\u53d1\u9001\u6570\u636e<\/strong><\/h2>\n<p>bt.send(&quot;Hello, Bluetooth!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u9009\u62e9\u534f\u8bae<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u901a\u4fe1\u534f\u8bae\u662f\u642d\u5efa\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u5173\u952e\u6b65\u9aa4\u3002\u7269\u8054\u7f51\u8bbe\u5907\u901a\u5e38\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\uff0c\u56e0\u6b64\u9009\u62e9\u5408\u9002\u7684\u901a\u4fe1\u534f\u8bae\u81f3\u5173\u91cd\u8981\u3002\u5e38\u89c1\u7684\u7269\u8054\u7f51\u901a\u4fe1\u534f\u8bae\u5305\u62ecMQTT\u3001CoAP\u3001HTTP\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>MQTT\u534f\u8bae<\/li>\n<\/ol>\n<p><p>MQTT\uff08Message Queuing Telemetry Transport\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6d88\u606f\u4f20\u8f93\u534f\u8bae\uff0c\u975e\u5e38\u9002\u5408\u8d44\u6e90\u53d7\u9650\u7684\u8bbe\u5907\u3002\u5b83\u5177\u6709\u4f4e\u5e26\u5bbd\u5360\u7528\u3001\u53ef\u9760\u4f20\u8f93\u7b49\u7279\u70b9\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7269\u8054\u7f51\u9886\u57df\u3002MQTT\u534f\u8bae\u57fa\u4e8e\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u53d1\u5e03\u6d88\u606f\u5230\u7279\u5b9a\u4e3b\u9898\uff0c\u4e5f\u53ef\u4ee5\u8ba2\u9605\u7279\u5b9a\u4e3b\u9898\u7684\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import paho.mqtt.client as mqtt<\/p>\n<h2><strong>\u914d\u7f6eMQTT\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>client = mqtt.Client()<\/p>\n<p>client.connect(&quot;mqtt_broker_address&quot;, 1883, 60)<\/p>\n<h2><strong>\u53d1\u5e03\u6d88\u606f<\/strong><\/h2>\n<p>client.publish(&quot;test\/topic&quot;, &quot;Hello, MQTT!&quot;)<\/p>\n<h2><strong>\u8ba2\u9605\u6d88\u606f<\/strong><\/h2>\n<p>def on_message(client, userdata, msg):<\/p>\n<p>    print(&quot;Received message:&quot;, msg.payload.decode())<\/p>\n<p>client.on_message = on_message<\/p>\n<p>client.subscribe(&quot;test\/topic&quot;)<\/p>\n<p>client.loop_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>CoAP\u534f\u8bae<\/li>\n<\/ol>\n<p><p>CoAP\uff08Constr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ned Application Protocol\uff09\u662f\u4e00\u79cd\u4e13\u4e3a\u8d44\u6e90\u53d7\u9650\u8bbe\u5907\u8bbe\u8ba1\u7684\u5e94\u7528\u5c42\u534f\u8bae\uff0c\u7c7b\u4f3c\u4e8eHTTP\u534f\u8bae\uff0c\u4f46\u66f4\u52a0\u8f7b\u91cf\u7ea7\u3002CoAP\u534f\u8bae\u57fa\u4e8e\u8bf7\u6c42\/\u54cd\u5e94\u6a21\u5f0f\uff0c\u8bbe\u5907\u53ef\u4ee5\u53d1\u9001\u8bf7\u6c42\u5e76\u63a5\u6536\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from coapthon.client.helperclient import HelperClient<\/p>\n<h2><strong>\u914d\u7f6eCoAP\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>client = HelperClient(server=(&quot;coap_server_address&quot;, 5683))<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = client.get(&quot;test\/resource&quot;)<\/p>\n<p>print(&quot;Received response:&quot;, response.payload)<\/p>\n<p>client.stop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7f16\u5199\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u7f16\u5199\u4ee3\u7801\u662f\u642d\u5efa\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\uff0c\u5b9e\u73b0\u4f20\u611f\u5668\u6570\u636e\u7684\u91c7\u96c6\u3001\u5904\u7406\u548c\u4f20\u8f93\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u8003\u8651\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u6027\u80fd\u7b49\u56e0\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6570\u636e\u91c7\u96c6<\/li>\n<\/ol>\n<p><p>\u6570\u636e\u91c7\u96c6\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u57fa\u7840\uff0c\u901a\u8fc7\u91c7\u96c6\u4f20\u611f\u5668\u7684\u6570\u636e\uff0c\u5b9e\u73b0\u5bf9\u73af\u5883\u7684\u76d1\u6d4b\u3002\u5728\u7f16\u5199\u6570\u636e\u91c7\u96c6\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u6839\u636e\u4f20\u611f\u5668\u7684\u7c7b\u578b\u548c\u63a5\u53e3\uff0c\u7f16\u5199\u76f8\u5e94\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u4f7f\u7528DHT11\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668\u91c7\u96c6\u6e29\u6e7f\u5ea6\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import Adafruit_DHT<\/p>\n<h2><strong>\u914d\u7f6e\u4f20\u611f\u5668<\/strong><\/h2>\n<p>sensor = Adafruit_DHT.DHT11<\/p>\n<p>pin = 4<\/p>\n<h2><strong>\u91c7\u96c6\u6570\u636e<\/strong><\/h2>\n<p>humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)<\/p>\n<p>print(&quot;Temperature:&quot;, temperature, &quot;C&quot;)<\/p>\n<p>print(&quot;Humidity:&quot;, humidity, &quot;%&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6570\u636e\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u6570\u636e\u5904\u7406\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u901a\u8fc7\u5bf9\u91c7\u96c6\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u5206\u6790\u548c\u51b3\u7b56\u3002\u5728\u7f16\u5199\u6570\u636e\u5904\u7406\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5904\u7406\u7b97\u6cd5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u79fb\u52a8\u5e73\u5747\u7b97\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5e73\u6ed1\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def moving_average(data, window_size):<\/p>\n<p>    result = []<\/p>\n<p>    for i in range(len(data)):<\/p>\n<p>        if i &lt; window_size:<\/p>\n<p>            result.append(sum(data[:i+1]) \/ (i+1))<\/p>\n<p>        else:<\/p>\n<p>            result.append(sum(data[i-window_size+1:i+1]) \/ window_size)<\/p>\n<p>    return result<\/p>\n<p>data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>window_size = 3<\/p>\n<p>smoothed_data = moving_average(data, window_size)<\/p>\n<p>print(&quot;Smoothed data:&quot;, smoothed_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u6570\u636e\u4f20\u8f93<\/li>\n<\/ol>\n<p><p>\u6570\u636e\u4f20\u8f93\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u5173\u952e\u6b65\u9aa4\uff0c\u901a\u8fc7\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u4f20\u8f93\u5230\u4e91\u7aef\u670d\u52a1\u5668\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u5b58\u50a8\u548c\u5206\u6790\u3002\u5728\u7f16\u5199\u6570\u636e\u4f20\u8f93\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u901a\u4fe1\u534f\u8bae\u548c\u4f20\u8f93\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528MQTT\u534f\u8bae\u5c06\u6570\u636e\u4f20\u8f93\u5230\u4e91\u7aef\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import paho.mqtt.client as mqtt<\/p>\n<h2><strong>\u914d\u7f6eMQTT\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>client = mqtt.Client()<\/p>\n<p>client.connect(&quot;mqtt_broker_address&quot;, 1883, 60)<\/p>\n<h2><strong>\u4f20\u8f93\u6570\u636e<\/strong><\/h2>\n<p>data = {&quot;temperature&quot;: 25.0, &quot;humidity&quot;: 60.0}<\/p>\n<p>client.publish(&quot;test\/topic&quot;, str(data))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8<\/p>\n<\/p>\n<p><p>\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u901a\u8fc7\u5bf9\u4f20\u8f93\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5b58\u50a8\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u5206\u6790\u548c\u51b3\u7b56\u3002\u5728\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5904\u7406\u7b97\u6cd5\u548c\u5b58\u50a8\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6570\u636e\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u6570\u636e\u5904\u7406\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u901a\u8fc7\u5bf9\u4f20\u8f93\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u5206\u6790\u548c\u51b3\u7b56\u3002\u5728\u7f16\u5199\u6570\u636e\u5904\u7406\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5904\u7406\u7b97\u6cd5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u7ebf\u6027\u56de\u5f52\u7b97\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u6d4b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<h2><strong>\u914d\u7f6e\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[1], [2], [3], [4], [5]])<\/p>\n<p>y = np.array([2, 3, 5, 7, 11])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u9884\u6d4b\u6570\u636e<\/strong><\/h2>\n<p>X_test = np.array([[6], [7]])<\/p>\n<p>y_pred = model.predict(X_test)<\/p>\n<p>print(&quot;Predicted data:&quot;, y_pred)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6570\u636e\u5b58\u50a8<\/li>\n<\/ol>\n<p><p>\u6570\u636e\u5b58\u50a8\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u5173\u952e\u6b65\u9aa4\uff0c\u901a\u8fc7\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u6301\u4e45\u5316\u548c\u67e5\u8be2\u3002\u5728\u7f16\u5199\u6570\u636e\u5b58\u50a8\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u548c\u5b58\u50a8\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528SQLite\u6570\u636e\u5e93\u5b58\u50a8\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u914d\u7f6e\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;iot_data.db&#39;)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS data<\/p>\n<p>             (timestamp DATETIME, temperature REAL, humidity REAL)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>data = [(datetime.now(), 25.0, 60.0)]<\/p>\n<p>c.executemany(&#39;INSERT INTO data VALUES (?, ?, ?)&#39;, data)<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>c.execute(&#39;SELECT * FROM data&#39;)<\/p>\n<p>rows = c.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5b9e\u73b0\u5b89\u5168\u6027<\/p>\n<\/p>\n<p><p>\u5b9e\u73b0\u5b89\u5168\u6027\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u548c\u8ba4\u8bc1\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u4f20\u8f93\u548c\u5b58\u50a8\u3002\u5728\u5b9e\u73b0\u5b89\u5168\u6027\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u52a0\u5bc6\u7b97\u6cd5\u548c\u8ba4\u8bc1\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6570\u636e\u52a0\u5bc6<\/li>\n<\/ol>\n<p><p>\u6570\u636e\u52a0\u5bc6\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u901a\u8fc7\u5bf9\u4f20\u8f93\u7684\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u4f20\u8f93\u3002\u5728\u7f16\u5199\u6570\u636e\u52a0\u5bc6\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u52a0\u5bc6\u7b97\u6cd5\u548c\u52a0\u5bc6\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528AES\u7b97\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Crypto.Cipher import AES<\/p>\n<p>import base64<\/p>\n<h2><strong>\u914d\u7f6e\u52a0\u5bc6\u5bc6\u94a5<\/strong><\/h2>\n<p>key = b&#39;Sixteen byte key&#39;<\/p>\n<h2><strong>\u52a0\u5bc6\u6570\u636e<\/strong><\/h2>\n<p>def encrypt(data):<\/p>\n<p>    cipher = AES.new(key, AES.MODE_EAX)<\/p>\n<p>    nonce = cipher.nonce<\/p>\n<p>    ciphertext, tag = cipher.encrypt_and_digest(data.encode(&#39;utf-8&#39;))<\/p>\n<p>    return base64.b64encode(nonce + ciphertext).decode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u89e3\u5bc6\u6570\u636e<\/strong><\/h2>\n<p>def decrypt(data):<\/p>\n<p>    data = base64.b64decode(data)<\/p>\n<p>    nonce = data[:16]<\/p>\n<p>    ciphertext = data[16:]<\/p>\n<p>    cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)<\/p>\n<p>    return cipher.decrypt(ciphertext).decode(&#39;utf-8&#39;)<\/p>\n<p>data = &quot;Hello, World!&quot;<\/p>\n<p>encrypted_data = encrypt(data)<\/p>\n<p>decrypted_data = decrypt(encrypted_data)<\/p>\n<p>print(&quot;Encrypted data:&quot;, encrypted_data)<\/p>\n<p>print(&quot;Decrypted data:&quot;, decrypted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6570\u636e\u8ba4\u8bc1<\/li>\n<\/ol>\n<p><p>\u6570\u636e\u8ba4\u8bc1\u662f\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u901a\u8fc7\u5bf9\u4f20\u8f93\u7684\u6570\u636e\u8fdb\u884c\u8ba4\u8bc1\uff0c\u786e\u4fdd\u6570\u636e\u7684\u771f\u5b9e\u6027\u548c\u5b8c\u6574\u6027\u3002\u5728\u7f16\u5199\u6570\u636e\u8ba4\u8bc1\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u8ba4\u8bc1\u7b97\u6cd5\u548c\u8ba4\u8bc1\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528HMAC\u7b97\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u8ba4\u8bc1\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import hmac<\/p>\n<p>import hashlib<\/p>\n<h2><strong>\u914d\u7f6e\u8ba4\u8bc1\u5bc6\u94a5<\/strong><\/h2>\n<p>key = b&#39;Secret key&#39;<\/p>\n<h2><strong>\u751f\u6210\u8ba4\u8bc1\u7801<\/strong><\/h2>\n<p>def generate_hmac(data):<\/p>\n<p>    return hmac.new(key, data.encode(&#39;utf-8&#39;), hashlib.sha256).hexdigest()<\/p>\n<h2><strong>\u9a8c\u8bc1\u8ba4\u8bc1\u7801<\/strong><\/h2>\n<p>def verify_hmac(data, hmac_code):<\/p>\n<p>    return hmac.compare_digest(generate_hmac(data), hmac_code)<\/p>\n<p>data = &quot;Hello, World!&quot;<\/p>\n<p>hmac_code = generate_hmac(data)<\/p>\n<p>is_valid = verify_hmac(data, hmac_code)<\/p>\n<p>print(&quot;HMAC code:&quot;, hmac_code)<\/p>\n<p>print(&quot;Is valid:&quot;, is_valid)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u642d\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u7269\u8054\u7f51\u7cfb\u7edf\u3002\u4ece\u9009\u62e9\u786c\u4ef6\u3001\u914d\u7f6e\u7f51\u7edc\u3001\u9009\u62e9\u534f\u8bae\u3001\u7f16\u5199\u4ee3\u7801\u3001\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\uff0c\u5230\u5b9e\u73b0\u5b89\u5168\u6027\uff0c\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u81f3\u5173\u91cd\u8981\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u7075\u6d3b\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u3001\u7f51\u7edc\u3001\u534f\u8bae\u548c\u7b97\u6cd5\uff0c\u786e\u4fdd\u7269\u8054\u7f51\u7cfb\u7edf\u7684\u9ad8\u6548\u3001\u53ef\u9760\u548c\u5b89\u5168\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fde\u63a5\u7269\u8054\u7f51\u8bbe\u5907\uff1f<\/strong><br \/>\u5728\u7269\u8054\u7f51\u9879\u76ee\u4e2d\uff0cPython\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u548c\u534f\u8bae\u4e0e\u8bbe\u5907\u8fdb\u884c\u8fde\u63a5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>paho-mqtt<\/code>\u5e93\u5b9e\u73b0MQTT\u534f\u8bae\u7684\u901a\u4fe1\uff0c\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u9700\u8981\u4f4e\u5e26\u5bbd\u548c\u4f4e\u529f\u8017\u7684\u5e94\u7528\u3002\u6b64\u5916\uff0cPython\u7684<code>requests<\/code>\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8eHTTP\u8bf7\u6c42\uff0c\u4f7f\u5f97\u4e0eRESTful API\u7684\u4ea4\u4e92\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u8fde\u63a5\u548c\u7ba1\u7406\u5404\u79cd\u7269\u8054\u7f51\u8bbe\u5907\u3002<\/p>\n<p><strong>\u5728\u7269\u8054\u7f51\u9879\u76ee\u4e2d\uff0cPython\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Python\u56e0\u5176\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u5f3a\u5927\u7684\u5e93\u652f\u6301\uff0c\u6210\u4e3a\u7269\u8054\u7f51\u5f00\u53d1\u7684\u70ed\u95e8\u9009\u62e9\u3002\u5b83\u62e5\u6709\u4e30\u5bcc\u7684\u793e\u533a\u8d44\u6e90\u548c\u6587\u6863\uff0c\u4f7f\u5f97\u5b66\u4e60\u548c\u5f00\u53d1\u66f4\u52a0\u9ad8\u6548\u3002\u6b64\u5916\uff0cPython\u7684\u8de8\u5e73\u53f0\u7279\u6027\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fdb\u884c\u5f00\u53d1\u548c\u90e8\u7f72\uff0c\u63d0\u5347\u4e86\u9879\u76ee\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u7269\u8054\u7f51\u6570\u636e\uff1f<\/strong><br \/>\u5728\u7269\u8054\u7f51\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5904\u7406\u81f3\u5173\u91cd\u8981\u3002Python\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5bf9\u4f20\u611f\u5668\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u3001\u8f6c\u6362\u548c\u53ef\u89c6\u5316\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>matplotlib<\/code>\u548c<code>seaborn<\/code>\u7b49\u53ef\u89c6\u5316\u5e93\uff0c\u53ef\u4ee5\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u56fe\u5f62\u5316\uff0c\u4fbf\u4e8e\u7406\u89e3\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u642d\u5efa\u7269\u8054\u7f51\u7684\u6b65\u9aa4\u5305\u62ec\u9009\u62e9\u786c\u4ef6\u3001\u914d\u7f6e\u7f51\u7edc\u3001\u9009\u62e9\u534f\u8bae\u3001\u7f16\u5199\u4ee3\u7801\u3001\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u3001\u5b9e\u73b0\u5b89\u5168\u6027\u3002\u4e0b\u9762\uff0c\u6211\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":1152985,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152973"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1152973"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152973\/revisions"}],"predecessor-version":[{"id":1152993,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152973\/revisions\/1152993"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152985"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}