尝试使用多个AP和密码,直到使用micropython在ESP32上进行连接

问题描述

我希望运行micropython的ESP32继续尝试四个不同的AP,直到其中一个连接为止。以下给出了no AP found错误

import esp32
from machine import Pin,ADC,reset

SSID1 = "firstAP"
PASSWORD1 = "letmein"
SSID2 = "secondAp"
PASSWORD2 = "letmein"
SSID3 = "thirdAP"
PASSWORD3 = "letmein"
SSID4 = "fourthAP"
PASSWORD4 = "letmein"

def do_connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)        
        sta_if.connect(SSID1,PASSWORD1)
        while not sta_if.isconnected():
            pass  
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)        
        sta_if.connect(SSID2,PASSWORD2)
        while not sta_if.isconnected():
            pass  
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)        
        sta_if.connect(SSID3,PASSWORD3)
        while not sta_if.isconnected():
            pass  
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)        
        sta_if.connect(SSID4,PASSWORD4)
        while not sta_if.isconnected():
            pass  
    print('network config:',sta_if.ifconfig())

do_connect()

解决方法

我没有使用Micropython的ESP32端口,但是在pyboard端口中,您可以使用WLAN.scan()命令从WLAN STA实例中扫描可用的无线网络。

扫描将返回一个元组列表,其中包含有关其找到的WiFi接入点的信息(ssid,bssid,通道,RSSI,authmode,隐藏)。

bssid是二进制形式的访问点的硬件地址,作为字节对象返回。您可以使用ubinascii.hexlify()将其转换为ASCII形式。

然后,您应该能够使用该元组列表来查找所需的网络。

我将尝试挖掘一些代码来演示它。

添加了示例:

nets = wlan.scan()
for net in nets:
    if net.ssid == 'mywifi':
        print('Network found!')
        wlan.connect(net.ssid,auth=(net.sec,'mywifikey'),timeout=5000)
        while not wlan.isconnected():
            machine.idle() # save power while waiting
        print('WLAN connection succeeded!')
        break

import os
import machine

uart = machine.UART(0,115200)
os.dupterm(uart)

known_nets = {
    '<net>': {'pwd': '<password>'},'<net>': {'pwd': '<password>','wlan_config':  ('10.0.0.114','255.255.0.0','10.0.0.1','10.0.0.1')},# (ip,subnet_mask,gateway,DNS_server)
}

if machine.reset_cause() != machine.SOFT_RESET:
    from network import WLAN
    wl = WLAN()
    wl.mode(WLAN.STA)
    original_ssid = wl.ssid()
    original_auth = wl.auth()

    print("Scanning for known wifi nets")
    available_nets = wl.scan()
    nets = frozenset([e.ssid for e in available_nets])

    known_nets_names = frozenset([key for key in known_nets])
    net_to_use = list(nets & known_nets_names)
    try:
        net_to_use = net_to_use[0]
        net_properties = known_nets[net_to_use]
        pwd = net_properties['pwd']
        sec = [e.sec for e in available_nets if e.ssid == net_to_use][0]
        if 'wlan_config' in net_properties:
            wl.ifconfig(config=net_properties['wlan_config'])
        wl.connect(net_to_use,(sec,pwd),timeout=10000)
        while not wl.isconnected():
            machine.idle() # save power while waiting
        print("Connected to "+net_to_use+" with IP address:" + wl.ifconfig()[0])

    except Exception as e:
        print("Failed to connect to any known network,going into AP mode")
        wl.init(mode=WLAN.AP,ssid=original_ssid,auth=original_auth,channel=6,antenna=WLAN.INT_ANT)

希望这对您有所帮助,您可能需要对代码进行一些小的更改。

,

您是否考虑过在try:语句中添加每次连接尝试?从我看来,您的代码尝试连接到第一个SSID,找不到它并引发错误。

尝试这样的事情:

if not sta_if.isconnected():
    print('connecting to network...')
    try:
        sta_if.active(True)        
        sta_if.connect(SSID1,PASSWORD1)
    except:
        print("SSID not found.")