【发布时间】:2020-02-05 08:56:45
【问题描述】:
我正在尝试设置一个以交互方式获取用户输入并连接到 WiFi 的功能。我可以扫描附近的网络并获取 SSID 列表,但是一旦我去实际连接程序总是挂起。我似乎无法弄清楚为什么。
import machine
import urequests
import network
from network import WLAN
def wifi_con():
station = network.WLAN(network.STA_IF)
station.active(True)
wlan = WLAN()
nets = wlan.scan()
for i in range(len(nets)):
print(str(i) + '\t' + str(nets[i][0])[2:-1])
print('Please enter the number corresponding to the SSID you wich to connect to:')
sel = -1
sel = int(input())
while sel not in range(len(nets)):
print('Please enter the number corresponding to the SSID you wich to connect to:')
print("please enter the wifi password: ")
connect_options = {
'ssid':str(nets[sel][0])[2:-1],
'password':input()
}
print(nets[sel][0], str(input()))
wlan.connect(str(nets[sel][0])[2:-1], input())
# test that we actually connected
print('getting the paste')
r = urequests.get('https://pastebin.com/raw/CZ6Mkdeg')
print(r.content)
硬件:LOLIN D32(基于 esp32 的板)
这是板上唯一的代码,所以我认为没有其他任何东西会干扰它。
【问题讨论】:
-
AFAIR
connect()不是同步的,这意味着连接是在后台建立的,不能保证在返回时启动。您应该在发出请求之前检查您是否已连接。立即执行请求应该会失败。 -
打印语句也永远不会运行。但这是一个很好的点,我会在那里添加一个检查。
标签: python networking python-requests micropython