在我的 esp32 设备上运行这段代码时:
import utime
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Dream Net R-632", "07132711")
max_wait = 10
while max_wait > 0:
"""
0 STAT_IDLE -- no connection and no activity,
1 STAT_CONNECTING -- connecting in progress,
-3 STAT_WRONG_PASSWORD -- failed due to incorrect password,
-2 STAT_NO_AP_FOUND -- failed because no access point replied,
-1 STAT_CONNECT_FAIL -- failed due to other problems,
3 STAT_GOT_IP -- connection successful.
"""
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection... ' + str(max_wait))
utime.sleep(1)
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('wlan connected')
status = wlan.ifconfig()
print('IP address = ' + status[0])
print('subnet mask = ' + status[1])
print('gateway = ' + status[2])
print('DNS server = ' + status[3])
我已经通过 Thonny ide 在我的 esp32 设备上运行了这段代码,但总是收到这样的错误:“RuntimeError: network connection failed”。 试运行:
wlan.isconnected()
它返回 True 意味着 wifi 已连接。 但是当我检查 wlan.status() 而不是获取 1-5 之间的值时,它会给我 1010 值,这会导致 RunTimeError。
我已经检查了我的 ssid 和 pin。一切都很好,除了这个 .status() 返回到 1010 的东西。
>>> wlan.active(True)
True
>>> wlan.status()
1010
>>>
根据this documentation状态是:
WLAN.status()
返回无线连接的当前状态。
STAT_IDLE – no connection, no activities-1000 STAT_CONNECTING – Connecting-1001 STAT_WRONG_PASSWORD – Failed due to password error-202 STAT_NO_AP_FOUND – Failed, because there is no access point reply,201 STAT_GOT_IP – Connected-1010 STAT_ASSOC_FAIL – 203 STAT_BEACON_TIMEOUT – Timeout-200 STAT_HANDSHAKE_TIMEOUT – Handshake timeout-204
所以看起来你的结果是预期的。