WifI connect trouble

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
lbayo
Posts: 24
Joined: Tue Mar 16, 2021 2:10 pm

WifI connect trouble

Post by lbayo » Mon May 03, 2021 5:21 am

Hi,

I am trying to do a simple function that tells me if certain Wi-Fi networks are "connectable" (the test one is the one I am using at home, therefore: it sure is "connectable") without using scan (), because scan ( ) gives me all the networks (mine and those of my neighbors) and I only want mine, the "connectable" ones. The function is

Code: Select all

import network
from time import sleep

cfg=[('MIWIFI_2G_kYXg', 'mypassword')]

def FilterSSID():
    goodStations=[]
    for ssid,psw in cfg:
        print (ssid,psw)
        station=network.WLAN(network.STA_IF)
        if not station.active():
            station.active(True)
        m=5
        while m>0 and not station.isconnected():
            try:
                station.connect (ss,pw)
            except:
                print ("err",m)
            m-=1
            sleep(1)
            
        if station.isconnected():
            good.append([ssid,psw])
            station.disconnect()
            m=10
            while  station.isconnected() and m>0:
                m-=1
                sleep(1)
        return goodStations
    
        
print (FilterSSID())
and the result is

Code: Select all

>>> %Run -c $EDITOR_CONTENT
myssid  mypassword
err 5
err 4
err 3
err 2
err 1
[]
>>> 
so, it don't connect to my WiFi network.
Instead, if, from the IDE console I do

Code: Select all

>>> import network
>>> st=network.WLAN(network.STA_IF)
>>> st.scan()

the result is

Code: Select all

[(b'MIWIFI_2G_kYXg', b'\x88]\xfb\xca\xf1\x8c', 11, -66, 4, False), (b'MIWIFI_2G_kYXg', b'\x8c\xde\xf9\x0b4\n', 11, -75, 4, False), (b'MiFibra-4526-24G', b'j\xa2"pE*', 6, -84, 3, False), (b'MiFibra-4526', b'\x04\xa2"pE(', 6, -85, 3, False), (b'MiFibra-2AB3', b'H\x8d6\x17*\xb5', 1, -88, 3, False), (b'MOVISTAR_2898', b'4W`f(\x99', 11, -90, 3, False), (b'MOVISTAR_6BD0', b'\x94\x91\x7f\xe8k\xdf', 6, -92, 3, False), (b'MIWIFI_2G_U9CG', b'\xf4\xb5\xaa\x97\x05\x85', 11, -92, 4, False), (b'MiFibra-3CE4', b'L\x1b\x86\xdd<\xe6', 1, -94, 3, False)]


I'm using a ESP-32 dev kit (WROOM-32) and Thonny IDE.

Do you know what can happen?

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: WifI connect trouble

Post by karfas » Mon May 03, 2021 6:30 am

First:
I don't see a useful application in this function. When you already have a ssid/password list, a scan() matched with this list will give you the "connectable" networks in range and will be (for longer ssid lists) much faster than trying to connect to each AP.

Second:
Make your homework.

In the posted code,

Code: Select all

for ssid,psw in cfg:
....
            try:
                station.connect (ss,pw)
            except:
                print ("err",m)
Where do "ss" and "pw" come from ? I assume any undefined variable error is hidden by the overly broad exception handler, where you don't event print the cause of the exception.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

lbayo
Posts: 24
Joined: Tue Mar 16, 2021 2:10 pm

Re: WifI connect trouble

Post by lbayo » Mon May 03, 2021 6:48 am

Sorry,

The reason for this function is that, in my apartment there are many neighbor wifi networks and only 1 or 2 of mine and I just want to know if my network, only my network, are accessible.

I renamed the variables but forgot those two.
Correct code is

Code: Select all

import network
from time import sleep

cfg=[('MIWIFI_2G_kYXg', 'mypassword')]

def FilterSSID():
    goodStations=[]
    for ssid,psw in cfg:
        print (ssid,psw)
        station=network.WLAN(network.STA_IF)
        if not station.active():
            station.active(True)
        m=5
        while m>0 and not station.isconnected():
            try:
                station.connect (ssid,psw)
            except:
                print ("err",m)
            m-=1
            sleep(1)
            
        if station.isconnected():
            good.append([ssid,psw])
            station.disconnect()
            m=10
            while  station.isconnected() and m>0:
                m-=1
                sleep(1)
        return goodStations
    
and result, without any error, but also without any connection to my WiFi. The result is

Code: Select all

>>> %Run -c $EDITOR_CONTENT
MIWIFI_2G_kYXg mypassword
[]
>>> 

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: WifI connect trouble

Post by karfas » Mon May 03, 2021 7:16 am

Again: You still don't know the cause of the exception (if any).

Please read some documentation. A google search for "micropython esp32 wlan connect" gets me directly to https://docs.micropython.org/en/latest/ ... /wlan.html , where the connect() call looks a little different.
The connect() method completes asynchronously. Only WLAN.isconnected() or WLAN.status() will tell you success or failure of the connect().

You call connect() FIVE times in a row, without waiting für completion.
I have no idea what connect() does when a connection is already initiated.

The station.isconnected() will most likely give you the status of the very last connection attempt (which is not completed yet, as you called it immediate before isconnected().
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

lbayo
Posts: 24
Joined: Tue Mar 16, 2021 2:10 pm

Re: WifI connect trouble

Post by lbayo » Mon May 03, 2021 4:49 pm

That's a great idea, thank you for enlightening me!"

Post Reply