A small edit to .connect() method

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

A small edit to .connect() method

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

Hello,

preceding the questions - https://docs.micropython.org/en/latest/ ... networking

I wanted to ask whether my code was wrong and if I was doing something wrong :mrgreen:
The ESP8266 "wasn't" connecting to my home networks.
It turns out I missed one of my own "debug" prints, print(nic.connect(SSID, PASSWD))

The function actually returns "None"

Perhaps it could be a good idea to let it return a control value (true, false, 1, 0)

in my case, the code

Code: Select all

if  nic.connect(SSID, PASSWD): 
    print("Connected to", SSID)
would not proceed further, because the function returns None (what is none?).

I know, there is the isConnected() method :P

PS: The project is absolutely awesome and Micropython is something I found out about just recently.
100% happy with the discovery. The programming is ultra fast and simple.

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

Re: A small edit to .connect() method

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

About Returning None: The function connect() does not return a value by intention. According to Python rules, the return value is then None.
About a possible return value: Since the connection does not happen immediately and connect() is non-blocking, the return value would almost always be False (or 0), unless connect() would be changed to be blocking. But then, a timeout value has to be specified, because connection may also fail completely. That means, the logic of waiting for a connection would be inside the connect() method and not in the python code. The latter has however the advantage that the code could do other reasonale startup tasks and just check & wait after that whether the connection succeeded. So you have more freedom the way it is now.

Post Reply