network connect issue

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
Mark.Calaway
Posts: 7
Joined: Sun Oct 29, 2017 4:22 pm

network connect issue

Post by Mark.Calaway » Thu Aug 16, 2018 9:21 am

Hi everyone,
i notified that strange issue in my similar codes, both load the information from the same json file:

Example one:

Code: Select all

with open('document.json') as data_file:
    # pay attention, load NOT loads
    data = ujson.load(data_file)
data_file.close()
yourWifiSSID = data["wifi"]["SSD"]
yourWifiPassword = data["wifi"]["pass"]
print('SSD: {} & pass: {}'.format(yourWifiSSID,yourWifiPassword))

sta_if = network.WLAN(network.STA_IF) # create station interface
sta_if.active(True) # activate the interface
sta_if.connect(yourWifiSSID, yourWifiPassword) # connect to an AP
while not sta_if.isconnected():
  pass
That example works correctly, it's connect.

Example two, i spilt json load and the connection in two functions:

Code: Select all

global data

def json_load():
    global data
    # read standard json configuration
    with open('document.json') as data_file:
        # pay attention, load NOT loads
        data = ujson.load(data_file)
    data_file.close()
    print("json loaded.")

def do_connection():
    global data
    yourWifiSSID = data["wifi"]["SSD"]
    yourWifiPassword = data["wifi"]["pass"]
    print('SSD: {} & pass: {}'.format(yourWifiSSID,yourWifiPassword))
    # connect the ESP8266 to local wifi network
    sta_if = network.WLAN(network.STA_IF) # create station interface
    sta_if.active(True) # activate the interface
    sta_if.connect(yourWifiSSID, yourWifiPassword)  # connect to an AP
    while not sta_if.isconnected():
        pass

# ------ MAIN
json_load()
do_connection()
That example print correctly the "yourWifiSSID" and "yourWifiPassword" but it never exit to the while loop.

What i'm doing wrong?

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

Re: network connect issue

Post by kevinkk525 » Thu Aug 16, 2018 9:44 am

try putting a sleep in the loop like time.sleep_ms(100).
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
Mark.Calaway
Posts: 7
Joined: Sun Oct 29, 2017 4:22 pm

Re: network connect issue

Post by Mark.Calaway » Thu Aug 16, 2018 10:19 am

kevinkk525 wrote:
Thu Aug 16, 2018 9:44 am
try putting a sleep in the loop like time.sleep_ms(100).
# ------ MAIN
json_load()
time.sleep_ms(100)
do_connection()

Still not working...

User avatar
Mark.Calaway
Posts: 7
Joined: Sun Oct 29, 2017 4:22 pm

Re: network connect issue

Post by Mark.Calaway » Thu Aug 16, 2018 1:51 pm

If i reboot stop the board by REPL with CTRL+C, the board reset and the code run correctly.

Is it a sort of bug? :shock:

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

Re: network connect issue

Post by kevinkk525 » Thu Aug 16, 2018 3:36 pm

I meant try this:

Code: Select all

while not sta_if.isconnected():
        time.sleep_ms(100)
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
Mark.Calaway
Posts: 7
Joined: Sun Oct 29, 2017 4:22 pm

Re: network connect issue

Post by Mark.Calaway » Thu Aug 16, 2018 5:43 pm

kevinkk525 wrote:
Thu Aug 16, 2018 3:36 pm
I meant try this:

Code: Select all

while not sta_if.isconnected():
        time.sleep_ms(100)
oh thx, it works!
Why in the doc is not mentioned?

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

Re: network connect issue

Post by kevinkk525 » Thu Aug 16, 2018 7:09 pm

Probably because they thought, that everyone knows, that a "busy loop" is always a bad idea and should never be used.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: network connect issue

Post by pythoncoder » Fri Aug 17, 2018 4:58 am

@Mark.Calaway You don't mention what platform you're using but it's likely that sleep() enables the underlying vendor code to run. You might like to look at this issue especially if you're using ESP32 - notably the comments by @Damien.

@kevinkk525 Putting sleep_ms(100) is fine in synchronous code. However it's not acceptable in asynchronous (uasyncio) solutions because it hogs the processor. When I last looked at ESP32 these sleeps were indeed necessary which in my view is a bug.
Peter Hinch
Index to my micropython libraries.

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

Re: network connect issue

Post by kevinkk525 » Fri Aug 17, 2018 7:49 am

@pythoncoder: You are right about asyncio. As I'm trying to connect to WIFI before I even start the asyncio loop, I use it that way. If the wifi connection breaks down later on, then your mqtt_as library takes care of that asynchronously.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply