{"id":1024532,"date":"2024-12-30T14:14:40","date_gmt":"2024-12-30T06:14:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1024532.html"},"modified":"2024-12-30T14:14:43","modified_gmt":"2024-12-30T06:14:43","slug":"python%e5%a6%82%e4%bd%95%e5%b5%8c%e5%85%a5%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1024532.html","title":{"rendered":"python\u5982\u4f55\u5d4c\u5165\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4713284f-dad1-4287-a3a8-010418a84d68.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5d4c\u5165\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u5d4c\u5165\u5f0f\u5f00\u53d1\u53ef\u4ee5\u901a\u8fc7MicroPython\u3001CircuitPython\u3001\u4e0e\u786c\u4ef6\u5e73\u53f0\u517c\u5bb9\u3001\u4f7f\u7528\u5408\u9002\u7684\u5f00\u53d1\u73af\u5883\u6765\u5b9e\u73b0\u3002<\/strong>\u5176\u4e2d\uff0cMicroPython\u548cCircuitPython\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u5d4c\u5165\u5f0fPython\u5b9e\u73b0\uff0c\u9002\u7528\u4e8e\u8bb8\u591a\u5fae\u63a7\u5236\u5668\u3002\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u5e73\u53f0\u548c\u5f00\u53d1\u73af\u5883\u5bf9\u4e8e\u6210\u529f\u7684\u5d4c\u5165\u5f0f\u5f00\u53d1\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684MicroPython\uff0c\u4ee5\u5e2e\u52a9\u7406\u89e3Python\u5d4c\u5165\u5f0f\u5f00\u53d1\u7684\u6838\u5fc3\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><p><strong>MicroPython<\/strong>\u662f\u4e00\u79cd\u4e13\u95e8\u4e3a\u5fae\u63a7\u5236\u5668\u8bbe\u8ba1\u7684\u7cbe\u7b80\u7248Python\u3002\u5b83\u4fdd\u7559\u4e86Python\u7684\u7cbe\u9ad3\uff0c\u540c\u65f6\u51cf\u5c11\u4e86\u5bf9\u7cfb\u7edf\u8d44\u6e90\u7684\u9700\u6c42\uff0c\u4f7f\u5176\u80fd\u591f\u5728\u8d44\u6e90\u6709\u9650\u7684\u5d4c\u5165\u5f0f\u8bbe\u5907\u4e0a\u8fd0\u884c\u3002MicroPython\u652f\u6301\u8bb8\u591a\u5e38\u89c1\u7684\u5fae\u63a7\u5236\u5668\u5e73\u53f0\uff0c\u5982ESP8266\u3001ESP32\u3001STM32\u7b49\uff0c\u5177\u6709\u4e30\u5bcc\u7684\u5e93\u652f\u6301\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u63a7\u5236\u786c\u4ef6\u5916\u8bbe\uff0c\u5982GPIO\u3001PWM\u3001I2C\u3001SPI\u7b49\u3002\u901a\u8fc7MicroPython\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528Python\u7f16\u5199\u5d4c\u5165\u5f0f\u5e94\u7528\u7a0b\u5e8f\uff0c\u7b80\u5316\u5f00\u53d1\u8fc7\u7a0b\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001MicroPython\u6982\u8ff0<\/h3>\n<\/p>\n<p><p><strong>MicroPython<\/strong>\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Python\u7f16\u7a0b\u8bed\u8a00\u5b9e\u73b0\uff0c\u4e13\u95e8\u4e3a\u5fae\u63a7\u5236\u5668\u548c\u53d7\u9650\u73af\u5883\u8bbe\u8ba1\u3002\u5b83\u4fdd\u7559\u4e86Python\u7684\u5927\u90e8\u5206\u7279\u6027\u548c\u6807\u51c6\u5e93\uff0c\u4f46\u4e3a\u4e86\u9002\u5e94\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u8d44\u6e90\u9650\u5236\uff0c\u8fdb\u884c\u4e86\u9002\u5f53\u7684\u88c1\u526a\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h4>1. MicroPython\u7684\u7279\u70b9<\/h4>\n<\/p>\n<p><p>MicroPython\u5177\u6709\u4ee5\u4e0b\u51e0\u4e2a\u663e\u8457\u7279\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8f7b\u91cf\u5316<\/strong>\uff1aMicroPython\u7684\u89e3\u91ca\u5668\u975e\u5e38\u5c0f\u5de7\uff0c\u53ef\u4ee5\u5728\u8d44\u6e90\u6709\u9650\u7684\u5fae\u63a7\u5236\u5668\u4e0a\u8fd0\u884c\u3002<\/li>\n<li><strong>\u9ad8\u6548\u6027<\/strong>\uff1a\u901a\u8fc7\u4f18\u5316\uff0cMicroPython\u5728\u8fd0\u884c\u65f6\u80fd\u591f\u9ad8\u6548\u5730\u5229\u7528\u7cfb\u7edf\u8d44\u6e90\u3002<\/li>\n<li><strong>\u5e7f\u6cdb\u7684\u786c\u4ef6\u652f\u6301<\/strong>\uff1aMicroPython\u652f\u6301\u591a\u79cd\u5fae\u63a7\u5236\u5668\u5e73\u53f0\uff0c\u5982ESP8266\u3001ESP32\u3001STM32\u3001RP2040\u7b49\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u3002<\/li>\n<li><strong>\u4e30\u5bcc\u7684\u5e93\u652f\u6301<\/strong>\uff1aMicroPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\uff0c\u652f\u6301\u5e38\u89c1\u7684\u786c\u4ef6\u63a5\u53e3\u548c\u534f\u8bae\uff0c\u5982GPIO\u3001PWM\u3001I2C\u3001SPI\u3001UART\u7b49\u3002<\/li>\n<li><strong>\u6613\u4e8e\u6269\u5c55<\/strong>\uff1a\u5f00\u53d1\u8005\u53ef\u4ee5\u57fa\u4e8eMicroPython\u7f16\u5199\u81ea\u5b9a\u4e49\u6a21\u5757\uff0c\u6269\u5c55\u5176\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><h4>2. MicroPython\u7684\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>MicroPython\u9002\u7528\u4e8e\u591a\u79cd\u5d4c\u5165\u5f0f\u5e94\u7528\u573a\u666f\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7269\u8054\u7f51\uff08IoT\uff09\u8bbe\u5907<\/strong>\uff1a\u5229\u7528MicroPython\u5f00\u53d1\u667a\u80fd\u5bb6\u5c45\u8bbe\u5907\u3001\u4f20\u611f\u5668\u7f51\u7edc\u7b49\u7269\u8054\u7f51\u5e94\u7528\u3002<\/li>\n<li><strong>\u673a\u5668\u4eba\u63a7\u5236<\/strong>\uff1a\u7f16\u5199MicroPython\u7a0b\u5e8f\u63a7\u5236\u673a\u5668\u4eba\u8fd0\u52a8\u3001\u4f20\u611f\u5668\u6570\u636e\u5904\u7406\u7b49\u3002<\/li>\n<li><strong>\u6559\u80b2\u548c\u539f\u578b\u5f00\u53d1<\/strong>\uff1aMicroPython\u7b80\u5355\u6613\u5b66\uff0c\u9002\u5408\u7528\u4e8e\u6559\u80b2\u548c\u5feb\u901f\u539f\u578b\u5f00\u53d1\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001MicroPython\u5f00\u53d1\u73af\u5883\u642d\u5efa<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528MicroPython\u8fdb\u884c\u5d4c\u5165\u5f0f\u5f00\u53d1\u4e4b\u524d\uff0c\u9700\u8981\u642d\u5efa\u5408\u9002\u7684\u5f00\u53d1\u73af\u5883\u3002\u4ee5\u4e0b\u662f\u642d\u5efaMicroPython\u5f00\u53d1\u73af\u5883\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5MicroPython\u56fa\u4ef6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5c06MicroPython\u56fa\u4ef6\u70e7\u5f55\u5230\u76ee\u6807\u5fae\u63a7\u5236\u5668\u4e0a\u3002\u4ee5ESP32\u4e3a\u4f8b\uff0c\u70e7\u5f55MicroPython\u56fa\u4ef6\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4e0b\u8f7d\u6700\u65b0\u7684MicroPython\u56fa\u4ef6\u3002\u53ef\u4ee5\u4eceMicroPython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u9884\u7f16\u8bd1\u7684\u56fa\u4ef6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u8fde\u63a5ESP32\u5f00\u53d1\u677f\u5230\u7535\u8111\uff0c\u5e76\u5b89\u88c5\u4e32\u53e3\u9a71\u52a8\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u5de5\u5177\uff08\u5982esptool.py\uff09\u5c06\u56fa\u4ef6\u70e7\u5f55\u5230ESP32\u5f00\u53d1\u677f\u4e0a\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">esptool.py --chip esp32 --port \/dev\/ttyUSB0 erase_flash<\/p>\n<p>esptool.py --chip esp32 --port \/dev\/ttyUSB0 write_flash -z 0x1000 esp32-20210902-v1.17.bin<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>2. \u8bbe\u7f6e\u5f00\u53d1\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728\u70e7\u5f55\u56fa\u4ef6\u540e\uff0c\u9700\u8981\u8bbe\u7f6e\u5f00\u53d1\u73af\u5883\u4ee5\u7f16\u5199\u548c\u4e0a\u4f20MicroPython\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u5e38\u7528\u7684\u5f00\u53d1\u73af\u5883\u9009\u62e9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Thonny<\/strong>\uff1aThonny\u662f\u4e00\u6b3e\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\uff0c\u652f\u6301MicroPython\u7f16\u7a0b\u3002\u5b83\u5177\u6709\u7b80\u5355\u6613\u7528\u7684\u56fe\u5f62\u754c\u9762\uff0c\u9002\u5408\u521d\u5b66\u8005\u4f7f\u7528\u3002<\/li>\n<li><strong>Visual Studio Code<\/strong>\uff1aVisual Studio Code\u662f\u4e00\u6b3e\u6d41\u884c\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u901a\u8fc7\u5b89\u88c5MicroPython\u63d2\u4ef6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cMicroPython\u5f00\u53d1\u3002<\/li>\n<li><strong>uPyCraft<\/strong>\uff1auPyCraft\u662f\u4e13\u95e8\u4e3aMicroPython\u5f00\u53d1\u8bbe\u8ba1\u7684IDE\uff0c\u5177\u6709\u4e32\u53e3\u901a\u4fe1\u3001\u6587\u4ef6\u7ba1\u7406\u7b49\u529f\u80fd\uff0c\u9002\u5408\u7528\u4e8eMicroPython\u5f00\u53d1\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001MicroPython\u7f16\u7a0b\u57fa\u7840<\/h3>\n<\/p>\n<p><p>\u638c\u63e1\u4e86\u5f00\u53d1\u73af\u5883\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199MicroPython\u4ee3\u7801\u4e86\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9bMicroPython\u7f16\u7a0b\u7684\u57fa\u7840\u77e5\u8bc6\uff1a<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u8bed\u6cd5<\/h4>\n<\/p>\n<p><p>MicroPython\u7684\u8bed\u6cd5\u4e0e\u6807\u51c6Python\u57fa\u672c\u4e00\u81f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u8bed\u6cd5\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>y = 3.14<\/p>\n<p>name = &quot;MicroPython&quot;<\/p>\n<p>is_active = True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6761\u4ef6\u8bed\u53e5<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if x &gt; 5:<\/p>\n<p>    print(&quot;x is greater than 5&quot;)<\/p>\n<p>elif x == 5:<\/p>\n<p>    print(&quot;x is equal to 5&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;x is less than 5&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5faa\u73af\u8bed\u53e5<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p>while x &gt; 0:<\/p>\n<p>    print(x)<\/p>\n<p>    x -= 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u51fd\u6570\u5b9a\u4e49<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(3, 5)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2. \u786c\u4ef6\u63a5\u53e3<\/h4>\n<\/p>\n<p><p>MicroPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u786c\u4ef6\u63a5\u53e3\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u786c\u4ef6\u5916\u8bbe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u786c\u4ef6\u63a5\u53e3\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>GPIO<\/strong>\uff1a\u63a7\u5236\u901a\u7528\u8f93\u5165\u8f93\u51fa\u5f15\u811a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from machine import Pin<\/p>\n<p>led = Pin(2, Pin.OUT)<\/p>\n<p>led.on()<\/p>\n<p>led.off()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>PWM<\/strong>\uff1a\u751f\u6210\u8109\u5bbd\u8c03\u5236\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from machine import Pin, PWM<\/p>\n<p>pwm = PWM(Pin(2))<\/p>\n<p>pwm.freq(1000)<\/p>\n<p>pwm.duty(512)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>I2C<\/strong>\uff1a\u4e0eI2C\u8bbe\u5907\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from machine import I2C, Pin<\/p>\n<p>i2c = I2C(scl=Pin(22), sda=Pin(21))<\/p>\n<p>i2c.writeto(0x3C, b&#39;\\x00\\xAF&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>SPI<\/strong>\uff1a\u4e0eSPI\u8bbe\u5907\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from machine import SPI, Pin<\/p>\n<p>spi = SPI(1, baudrate=1000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))<\/p>\n<p>spi.write(b&#39;\\x9F&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001MicroPython\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u7f16\u7a0b\u548c\u786c\u4ef6\u63a5\u53e3\u64cd\u4f5c\uff0cMicroPython\u8fd8\u652f\u6301\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\uff0c\u5982\u7f51\u7edc\u901a\u4fe1\u3001\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f51\u7edc\u901a\u4fe1<\/h4>\n<\/p>\n<p><p>MicroPython\u652f\u6301Wi-Fi\u3001\u84dd\u7259\u7b49\u7f51\u7edc\u901a\u4fe1\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u5f00\u53d1\u7269\u8054\u7f51\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7f51\u7edc\u901a\u4fe1\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>Wi-Fi\u8fde\u63a5<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import network<\/p>\n<p>wlan = network.WLAN(network.STA_IF)<\/p>\n<p>wlan.active(True)<\/p>\n<p>wlan.connect(&#39;SSID&#39;, &#39;password&#39;)<\/p>\n<p>while not wlan.isconnected():<\/p>\n<p>    pass<\/p>\n<p>print(&#39;network config:&#39;, wlan.ifconfig())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>HTTP\u8bf7\u6c42<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urequests<\/p>\n<p>response = urequests.get(&#39;http:\/\/api.example.com\/data&#39;)<\/p>\n<p>print(response.text)<\/p>\n<p>response.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2. \u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>MicroPython\u652f\u6301\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5728\u8bbe\u5907\u7684\u5b58\u50a8\u4e2d\u8bfb\u5199\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u6587\u4ef6\u8bfb\u5199<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.txt&#39;, &#39;w&#39;) as f:<\/p>\n<p>    f.write(&#39;Hello, MicroPython!&#39;)<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as f:<\/p>\n<p>    content = f.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u4ef6\u7ba1\u7406<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.listdir()<\/p>\n<p>os.mkdir(&#39;new_dir&#39;)<\/p>\n<p>os.remove(&#39;data.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001MicroPython\u9879\u76ee\u5b9e\u6218<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3MicroPython\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684MicroPython\u9879\u76ee\u793a\u4f8b\uff1a\u5f00\u53d1\u4e00\u4e2a\u57fa\u4e8eESP32\u7684\u6e29\u6e7f\u5ea6\u76d1\u6d4b\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><h4>1. \u786c\u4ef6\u51c6\u5907<\/h4>\n<\/p>\n<ul>\n<li>ESP32\u5f00\u53d1\u677f<\/li>\n<li>DHT11\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668<\/li>\n<li>\u9762\u5305\u677f\u548c\u675c\u90a6\u7ebf<\/li>\n<\/ul>\n<p><h4>2. \u7535\u8def\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u6309\u7167\u4ee5\u4e0b\u8fde\u63a5\u65b9\u5f0f\uff0c\u5c06DHT11\u4f20\u611f\u5668\u8fde\u63a5\u5230ESP32\u5f00\u53d1\u677f\uff1a<\/p>\n<\/p>\n<ul>\n<li>DHT11 VCC -&gt; ESP32 3.3V<\/li>\n<li>DHT11 GND -&gt; ESP32 GND<\/li>\n<li>DHT11 DATA -&gt; ESP32 GPIO 4<\/li>\n<\/ul>\n<p><h4>3. \u7f16\u5199MicroPython\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b9e\u73b0\u6e29\u6e7f\u5ea6\u76d1\u6d4b\u7cfb\u7edf\u7684MicroPython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from machine import Pin<\/p>\n<p>import dht<\/p>\n<h2><strong>\u521d\u59cb\u5316DHT11\u4f20\u611f\u5668<\/strong><\/h2>\n<p>sensor = dht.DHT11(Pin(4))<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        # \u8bfb\u53d6\u6e29\u6e7f\u5ea6\u6570\u636e<\/p>\n<p>        sensor.measure()<\/p>\n<p>        temp = sensor.temperature()<\/p>\n<p>        hum = sensor.humidity()<\/p>\n<p>        print(&#39;Temperature: {}\u00b0C, Humidity: {}%&#39;.format(temp, hum))<\/p>\n<p>        # \u5c06\u6570\u636e\u4e0a\u4f20\u5230\u670d\u52a1\u5668\uff08\u793a\u4f8b\u4ee3\u7801\uff09<\/p>\n<p>        # import urequests<\/p>\n<p>        # response = urequests.post(&#39;http:\/\/api.example.com\/upload&#39;, json={&#39;temp&#39;: temp, &#39;hum&#39;: hum})<\/p>\n<p>        # response.close()<\/p>\n<p>    except OSError as e:<\/p>\n<p>        print(&#39;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to read sensor.&#39;)<\/p>\n<p>    # \u6bcf10\u79d2\u8bfb\u53d6\u4e00\u6b21<\/p>\n<p>    time.sleep(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u5728\u5d4c\u5165\u5f0f\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\uff0c\u91cd\u70b9\u4ecb\u7ecd\u4e86MicroPython\u7684\u57fa\u672c\u6982\u5ff5\u3001\u5f00\u53d1\u73af\u5883\u642d\u5efa\u3001\u7f16\u7a0b\u57fa\u7840\u4ee5\u53ca\u9ad8\u7ea7\u5e94\u7528\u3002\u901a\u8fc7MicroPython\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528Python\u7f16\u5199\u5d4c\u5165\u5f0f\u5e94\u7528\u7a0b\u5e8f\uff0c\u7b80\u5316\u5f00\u53d1\u8fc7\u7a0b\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u5e76\u638c\u63e1MicroPython\uff0c\u4e3a\u5d4c\u5165\u5f0f\u5f00\u53d1\u5e26\u6765\u65b0\u7684\u601d\u8def\u548c\u7075\u611f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9664\u4e86MicroPython\uff0cCircuitPython\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5d4c\u5165\u5f0fPython\u5b9e\u73b0\uff0c\u8bfb\u8005\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u5e73\u53f0\u3002\u65e0\u8bba\u662f\u7269\u8054\u7f51\u8bbe\u5907\u3001\u673a\u5668\u4eba\u63a7\u5236\u8fd8\u662f\u6559\u80b2\u548c\u539f\u578b\u5f00\u53d1\uff0cPython\u5d4c\u5165\u5f0f\u5f00\u53d1\u90fd\u5177\u6709\u5e7f\u9614\u7684\u5e94\u7528\u524d\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f7f\u7528Python\u8fdb\u884c\u5f00\u53d1\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u8fdb\u884c\u5f00\u53d1\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528MicroPython\u6216CircuitPython\u7b49\u8f7b\u91cf\u7ea7Python\u5b9e\u73b0\u3002\u8fd9\u4e9b\u5b9e\u73b0\u4e13\u4e3a\u8d44\u6e90\u6709\u9650\u7684\u8bbe\u5907\u8bbe\u8ba1\uff0c\u652f\u6301\u57fa\u672c\u7684Python\u8bed\u6cd5\u548c\u6807\u51c6\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u5f00\u53d1\u7b80\u5355\u7684\u5d4c\u5165\u5f0f\u5e94\u7528\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u5d4c\u5165\u5f0f\u7f16\u7a0b\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Python\u5728\u5d4c\u5165\u5f0f\u7f16\u7a0b\u4e2d\u5177\u6709\u8bb8\u591a\u4f18\u52bf\uff0c\u5305\u62ec\u7b80\u6d01\u6613\u61c2\u7684\u8bed\u6cd5\u3001\u4e30\u5bcc\u7684\u5e93\u548c\u6846\u67b6\u652f\u6301\uff0c\u4ee5\u53ca\u5f3a\u5927\u7684\u793e\u533a\u8d44\u6e90\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u5229\u7528Python\u5feb\u901f\u539f\u578b\u5f00\u53d1\uff0c\u52a0\u5feb\u9879\u76ee\u8fdb\u5ea6\u3002\u540c\u65f6\uff0cPython\u7684\u53ef\u8bfb\u6027\u4f7f\u5f97\u4ee3\u7801\u7ef4\u62a4\u548c\u534f\u4f5c\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p><strong>\u54ea\u4e9b\u5d4c\u5165\u5f0f\u8bbe\u5907\u9002\u5408\u4f7f\u7528Python\u8fdb\u884c\u5f00\u53d1\uff1f<\/strong><br \/>\u8bb8\u591a\u5d4c\u5165\u5f0f\u8bbe\u5907\u90fd\u9002\u5408\u4f7f\u7528Python\u8fdb\u884c\u5f00\u53d1\uff0c\u4f8b\u5982\u6811\u8393\u6d3e\uff08Raspberry Pi\uff09\u3001Arduino\uff08\u4f7f\u7528MicroPython\uff09\u4ee5\u53caESP32\u7b49\u3002\u8fd9\u4e9b\u8bbe\u5907\u901a\u5e38\u5177\u6709\u8db3\u591f\u7684\u8ba1\u7b97\u80fd\u529b\u548c\u5185\u5b58\uff0c\u53ef\u4ee5\u8fd0\u884cPython\u89e3\u91ca\u5668\uff0c\u9002\u5408\u8fdb\u884c\u5404\u79cdIoT\u548c\u667a\u80fd\u5bb6\u5c45\u9879\u76ee\u7684\u5f00\u53d1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5d4c\u5165\u5f0f\u5f00\u53d1\u53ef\u4ee5\u901a\u8fc7MicroPython\u3001CircuitPython\u3001\u4e0e\u786c\u4ef6\u5e73\u53f0\u517c\u5bb9\u3001\u4f7f\u7528\u5408\u9002\u7684\u5f00 [&hellip;]","protected":false},"author":3,"featured_media":1024540,"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\/1024532"}],"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=1024532"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024532\/revisions"}],"predecessor-version":[{"id":1024542,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024532\/revisions\/1024542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1024540"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1024532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1024532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1024532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}