从 micropy 获取 OSError -202 where 运行 urequests.get

getting OSError -202 where running urequests.get from micropy

嗨,我在使用此代码时遇到错误,但它在 python shell 中运行,任何人都可以帮助我吗

from machine import Pin
import time
import network
import urequests
p0 = Pin(0,Pin.OUT)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('ssid', 'pass')
response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
while True:
    ans = response.json()['userId']
    p0.value(1)
    time.sleep(1)
    p0.off()
    time.sleep(1)
    print('ok')

这是错误:

Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
  File "urequests.py", line 108, in get
  File "urequests.py", line 53, in request
OSError: -202

您的问题(我的猜测)是您在未连接到 WiFi 的情况下开始 urequest.get()。创建执行 wifi 连接的函数并调用它

def do_connect():
    import network
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('essid', 'password')
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())

说明:wlan.connect()是异步函数,需要等待,连接wifi后才继续urequest.get()