Problem connecting after power cycle

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Problem connecting after power cycle

Post by pythoncoder » Fri Apr 05, 2019 9:58 am

My main.py has a single line importing the file below:

Code: Select all

import pyb, network, time
red = pyb.LED(1)
green = pyb.LED(2)

green.on()
time.sleep(4)  # Give things a chance to settle down
green.off()

s = network.WLAN(network.STA_IF)
s.active(True)
s.connect('redacted', 'REDACTED')
red.on()
time.sleep(0.2)  # Brief red flash if already connected
while not s.isconnected():
    time.sleep(1)
red.off()
green.on()
I'm testing on an SF6W with a WBUS-DIP28 and connecting via USB-FS. Firmware is yesterday's (14th April) build and mboot is updated.

Running rshell the script is reliable if I press ctrl-d after it's already connected. However on the first ctrl-d it sometimes hangs with the red LED lit. I can always get it to connect quickly on the second and subsequent soft reset.

Behaviour is poor if I quit rshell and plug the board into my PC (or another known good source of USB power). It occasionally connects, but more commonly does not. If after a failure I subsequently press the reset button it does not usually connect.

In my actual application I've tried various strategies including trying again to connect if isconnected() returns False for too long, but so far I've not identified a reliable way to connect after a power cycle or reset. Am I missing something or is there a firmware issue?

As an aside it would be good if network supported STAT_CONNECTING because we could use the same code for Pyboard D as for ESP8266 and ESP32. The following terminates on success or failure without needing arbitrary timeouts to detect the fail state.

Code: Select all

    while s.status() == network.STAT_CONNECTING:  # Break out on fail or success. Check once per sec.
        await asyncio.sleep(1)
Peter Hinch
Index to my micropython libraries.

chuckbook
Posts: 135
Joined: Fri Oct 30, 2015 11:55 pm

Re: Problem connecting after power cycle

Post by chuckbook » Fri Apr 05, 2019 12:08 pm

@pythoncoder, do you have a battery installed?
Assuming you don't use a battery, why do you use USB_FS instead of USB_HS?

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

Re: Problem connecting after power cycle

Post by pythoncoder » Fri Apr 05, 2019 2:17 pm

Because when I get this working I intend to fit a battery.
[EDIT]
I have now tested removing the USB lead and powering from a switched LiPo battery. In five attempts it connected successfully once.
Peter Hinch
Index to my micropython libraries.

chuckbook
Posts: 135
Joined: Fri Oct 30, 2015 11:55 pm

Re: Problem connecting after power cycle

Post by chuckbook » Fri Apr 05, 2019 4:59 pm

I only wanted to rule out any power faults due to current limit kicking in.

chuckbook
Posts: 135
Joined: Fri Oct 30, 2015 11:55 pm

Re: Problem connecting after power cycle

Post by chuckbook » Fri Apr 05, 2019 5:20 pm

Out of curiosity I tested your script with a SF6W but due to lack of a WBUS-DIP28 I had to use USB-HS.
There wasn't a single failure with ~20 power cycles. It took ~7s to re-connect after a power cycle.

I will try to get hold of a WBUS-DIP28 and repeat the test.

PS:
The AP I used was in the same room.
PS2:
After I strangled Miss Micropython a bit, she gave me one of the rare WBUS-DIP28 boards.
As I had to use an old mboot I had to add:

Code: Select all

EN1 = pyb.Pin('E1', mode=pyb.Pin.IN, pull=pyb.Pin.PULL_UP)
to boot.py to increase ILIM to 500mA.
Not a single failure detected within > 10 trials.
All test done with:

Code: Select all

MicroPython v1.10-284-g4417c03a9 on 2019-04-05; PYBD_SF6W with STM32F767IIK

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

Re: Problem connecting after power cycle

Post by pythoncoder » Sat Apr 06, 2019 5:14 am

I only wanted to rule out any power faults due to current limit kicking in.
Sure. After reading your post I thought it made sense to repeat the test with no USB, not least because it is my intended use case.
Not a single failure detected within > 10 trials.
That is very odd. The room where I did my testing has a good signal (-58dBm) and I have done extensive testing of ESP8266 and ESP32 here without issues of initial connection. I have also run networked applications on the Pyboard D for long periods here. It reconnects after deliberate WiFi outages without issue.

At the moment I'm baffled as to why you haven't seen this. On each power down I waited 10s for decoupling caps to discharge.

I'll try again close to the router, and also with an SF2W.
Peter Hinch
Index to my micropython libraries.

chuckbook
Posts: 135
Joined: Fri Oct 30, 2015 11:55 pm

Re: Problem connecting after power cycle

Post by chuckbook » Sat Apr 06, 2019 7:40 am

I repeated the test with longer timeouts. Same result.
However, when I selected a much weaker AP (-74dB) I got failures to connect after power-cycling.

I modified your code to do a soft reboot after timeout and the second attempt always succeeded.

I have a rough guess what could cause this problem (USB auto detect), but this is something far beyond my skills...

BTW, although USB auto select is quite a handy feature, we always disable it in all of our real applications.
Last edited by chuckbook on Sat Apr 06, 2019 8:25 am, edited 1 time in total.

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

Re: Problem connecting after power cycle

Post by pythoncoder » Sat Apr 06, 2019 7:55 am

I've now tested SF6W and SF2W in the same room as the router, distance ~2.5m, RSSI -39dBm.
SF6W 8 successes, 2 fails
SF2W 9 successes, 1 fail
For completeness I did five attempts with SF2W in my usual -58dBm location: 1 success, 4 fails.

I have concluded that SF6W and SF2W behave identically; unsurprising as they use the same WiFi chip.

To make the initial connection after a power cycle with even moderate reliability you need a signal >=20dB (100x!) more than that required to reconnect after a WiFi outage.
Peter Hinch
Index to my micropython libraries.

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: Problem connecting after power cycle

Post by kamikaze » Thu Apr 25, 2019 4:22 pm

so does it mean that it is not the right time to buy PyBoard D ?

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

Re: Problem connecting after power cycle

Post by pythoncoder » Fri Apr 26, 2019 5:55 am

This has been much improved by firmware updates. This issue may be tracked here.
tl;dr Most users have no problem. I still get occasional connect failures which are related to the security mode of my access point.

This is problematic if your code repeatedly turns off the WiFi chip and turns it on again to reconnect. However if you don't deliberately do this (with .deinit()) the code will recover from WiFi outages and the problem is reduced to a fairly uncommon failure to connect after a power outage.

I have total confidence that Damien will fix these problems soon in firmware. The Pyboard D is a great piece of kit.
Peter Hinch
Index to my micropython libraries.

Post Reply