获取OSError-202在哪里运行urequests.get from micropy



hi我在这段代码中有错误,但它在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()

最新更新