【问题标题】:Connecting to Wifi in micro python hangs在 micro python 中连接到 Wifi 挂起
【发布时间】: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


【解决方案1】:

所以如果以后有人来寻找这个问题,我已经找到了这个问题。显然,即使我正在重置电路板,电路板仍保持着之前的连接。我从 wifi using this function 断开连接,然后在我意识到我像白痴一样多次调用 input() 后,我的函数开始工作了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2021-09-17
    • 2020-04-16
    相关资源
    最近更新 更多