Connecting to WiFi

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Matthew0x
Posts: 4
Joined: Sat Apr 24, 2021 8:49 pm

Connecting to WiFi

Post by Matthew0x » Sat Apr 24, 2021 9:53 pm

Sorry for (if the previous was approved) post, I will remove the old one.

Hello,

I have been trying to connect my ESP8266 to my local networks.
I assumed that in the end I succeeded, but something's wrong in here.

I read the documentation: https://docs.micropython.org/en/latest/ ... networking

Code: Select all

import network
from network_config import SSID, PASSWD
I'm importing the network module, the second one is my lazy solution to easy config:P

Code: Select all

nic = network.WLAN(network.STA_IF) #Creating network interface card object
nic.active(True)
nic.connect(SSID, PASSWD)

print(nic.scan() )
print(nic.isconnected())
print(nic.active)
print(nic.status)

print(SSID,"\n", PASSWD)
Then, the network interface "card" is declared, following the guide in stationary mode
it's then set to active, then it should connect to the network.

It turns out that both .active() and .status() return <bound_method> (what is this?)
and .connect() method gives me "NONE"??
Ignoring these weird values, the code later is as the following:

Code: Select all

if  nic.isconnected(): 
    print("Connected to", SSID)
    oled.fill(0)
    oled.text('Connected to:', 0, 24, 1)
    oled.text('SSID', 32, 48, 1)
    oled.show()
    sleep(2)
else:
    nic = network.WLAN(network.AP_IF)
    nic.active(True)
    print("FAIL, falling to AP mode")
    ...
I am printing nicely my SSID and debug info onto my oled screen, however...
the isconnected() returns FALSE.

Why?

The device appears in my router's management website.
The name is "ESP_1123AC", however it seems to be a wrong name?
I also have it in my WiFi networks, as if it was simultaneously in AP and STA mode, it isn't possible.

I thought that perhaps Thonny was letting me upload code to devices this way (my PC is using WiFi card), but I am not sure in the end.

Am I doing something wrong in here?
I could have missed something.

PS:
I really like MicroPython and want to make use of it in my mini greenhouse project and similar ones.
It seems to work incredibly on ESP chips. It's heaven and earth compared to random scattered libraries from Arduino etc.

The filesystem is absolutely great, a very useful feature.
I have been programming 3D Printers with marlin and MicroPython is awesome.
Please continue supporting it:P

User avatar
Roberthh
Posts: 2707
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Connecting to WiFi

Post by Roberthh » Sun Apr 25, 2021 6:19 am

You have written:

Code: Select all

print(nic.active)
print(nic.status)
Which shows, which type of object e.g. nic.active is.
But you surely wanted to the the return value of the calls, so you have to call the functions by writing.

Code: Select all

print(nic.active())
print(nic.status())
About nic.isconnected() returning False: Connecting takes a while.That nothing related to Micropython. It's just the protocol. If you look for instance at your smartphone: when you attempt connecting to a WiFi AP, it also takes a few seconds until the connection is confirmed. At my set-up it takes a few seconds. So I wrote a code waiting up to 10 seconds for the connection.

kevinkk525
Posts: 939
Joined: Sat Feb 03, 2018 7:02 pm

Re: Connecting to WiFi

Post by kevinkk525 » Sun Apr 25, 2021 7:09 am

Calling nic.scan() might disconnect you wlan again. At least I remember having observed this with either esp8266 or Esp32. Pyboard doesn't do that.
Correct me if I'm wrong..
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Matthew0x
Posts: 4
Joined: Sat Apr 24, 2021 8:49 pm

Re: Connecting to WiFi

Post by Matthew0x » Sun Apr 25, 2021 2:52 pm

I didn't notice the lack of brackets.
Yes, indeed nic.active() returns then "True", and nic.status() returns "1".

It also turns out that it needed more time:D

Code: Select all

nic.connect(SSID, PASSWD)
sleep(10)
#print(nic.scan() )
print(nic.isconnected())
print(nic.active())
print(nic.status())
now the nic.isconnected() returns "True", and then the nic.status() returns "1".

I then uncommented the nic.scan() to test whether it disconnects the active adapter, it seems that it doesn't.

Code: Select all

nic.connect(SSID, PASSWD)
sleep(10)
print(nic.scan() )
print(nic.isconnected())
print(nic.active())
print(nic.status())
>network names
>True
>True
>5

Then it turns out that the reported device (by the router) the most likely indeed is the ESP.
It seems that it takes a little bit of time for function nic.isconnected() to get a response/register the status?


Out of curiosity, I tested a few settings
2 seconds - OK
1 second - OK
0.5 seconds - OK
0.25 OK .... and then 0.1 seconds returns False, so it's 100ms. Could be the network lag.

Thank you. I didn't think of the latency/DHCP(?) being an issue.

kevinkk525
Posts: 939
Joined: Sat Feb 03, 2018 7:02 pm

Re: Connecting to WiFi

Post by kevinkk525 » Sun Apr 25, 2021 3:19 pm

Connecting to a wlan network can take a few seconds.
That's why typically this is used:

While not nic.isconnected():
time.sleep(1)
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
Roberthh
Posts: 2707
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Connecting to WiFi

Post by Roberthh » Sun Apr 25, 2021 3:26 pm

The loop I use is:

Code: Select all

        for _ in range(10):
            if nic.isconnected():      # check if the station is connected to an AP
                print('\nnetwork config:', nic.ifconfig())
                break
            print('.', end='')
            time.sleep(1)
        else:
            print(" Connect attempt timed out\n")

Matthew0x
Posts: 4
Joined: Sat Apr 24, 2021 8:49 pm

Re: Connecting to WiFi

Post by Matthew0x » Sun Apr 25, 2021 4:19 pm

kevinkk525 wrote:
Sun Apr 25, 2021 3:19 pm
Connecting to a wlan network can take a few seconds.
That's why typically this is used:

While not nic.isconnected():
time.sleep(1)
I think that the problem in here would be the infinite loop.
I for example want to print on my LCD info about the connected network.

This way I could take my driver into garden, connect it to power supply and let the LCD inform me whether it succeeded at connecting to the local AP. In your solution it would be infinitely stuck in a loop I believe.

I think that Robert's idea is a good one. I am learning python and mostly writing a dirty code for now, but a some sort of "timeout" loop is a good thing. I only don't understand what python understands by _ in the for ... in range (...) loop.

In C the compiler wants a letter as variable, you can use underscore as "anonymous" variable in python or something alike?
Another cool thing is that you can change the duration of one step by specifying seconds in sleep().

Once I manage to setup a simple HTML website I will try out the clock frequency functions.
I wonder if decreasing the clock will lead to any issues in case of simple code.

User avatar
Roberthh
Posts: 2707
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Connecting to WiFi

Post by Roberthh » Sun Apr 25, 2021 5:16 pm

_ is just a legal variable name and often used as temp vatiable name.

kevinkk525
Posts: 939
Joined: Sat Feb 03, 2018 7:02 pm

Re: Connecting to WiFi

Post by kevinkk525 » Sun Apr 25, 2021 6:08 pm


Matthew0x wrote:
kevinkk525 wrote:
Sun Apr 25, 2021 3:19 pm
Connecting to a wlan network can take a few seconds.
That's why typically this is used:

While not nic.isconnected():
time.sleep(1)
I think that the problem in here would be the infinite loop.
I for example want to print on my LCD info about the connected network.
Oh absolutely. This was just the most basic way to get a stable wlan connection so code afterwards will work.

Personally I always program using uasyncio to prevent waiting at any point in my program.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

davef
Posts: 280
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Connecting to WiFi

Post by davef » Mon Jun 07, 2021 8:56 pm

I have been using something similar to another suggestion from Roberthh:

Code: Select all

    if not sta_if.isconnected():
        print('connecting to hotspot...')
        sta_if.active(True)
        sta_if.ifconfig(('192.168.10.99', '255.255.255.0', '192.168.10.1', '8.8.8.8'))
        sta_if.connect('HUAWEI-E8231-c4fd', 'xxx')

        while not sta_if.isconnected():
            print('.', end = '')
            utime.sleep(1)

I had it in a while loop that would bail-out after 5 tries and do a machine.reset(). This always succeeded on the 2nd attempt of 5 tries. It rarely had to go to the 2nd attempt but I wanted to guarantee that I could bring the internet up.

However, I really do not want to do a machine.reset().

It "appears" that doing a machine.reset() after the 1st attempt something somewhere was properly initialised.

Any ideas?

Thanks

Post Reply