Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4804

SDK • Re: Odd behaviour in wifi connection

$
0
0
When I've tested wifi-enabled microcontrollers in my home network, it's not uncommon at all that the first attempts to connect to the router fail, nor it is uncommon for the initial failures to be reported as bad credentials, even though subsequent attempts with the same credentials succeed. This has happened with the PicoW, ESP32 and ESP8266, which makes me doubt that the specific MCU hardware is at fault.

So either I'm systematically doing something wrong in the software with at least two different SDKs, or it's just a fact of life (at least with my router) that connection attempts may fail until they succeed. Since this sounds almost exactly like your situation, I wonder if that's just the way it is with wifi in general. (My experience is different in that sometimes I do see the connection succeeding on the first attempt, I'd say about as often as two attempts are needed.)

So I've made it a matter of course to run the connection attempt in a loop, checking the link status along the way, sleeping for about 100ms if it's not up, and repeating the connection attempt if a error status is determined. That keeps going until the link is up or a timeout expires. With the Pico SDK, you can call cyw43_tcpip_link_status() and check for the return value CYW43_LINK_UP. If the return value is negative, it indicates an error, and the connection should be attempted again. An example of the loop in one of my projects can be seen here.

This has made me suspect that all of my devices that connect to wifi also run such a loop, I had just never noticed it before.

Unfortunately these observations don't explain much. Why you'd have to ask a router twice to log in is beyond my ken. If transporting data over the airwaves were flaky for some reason, you'd think that there would be similar kind of trouble after the connection is established, but that hasn't been my experience. Once the link is up, the connections have been very reliable. I guess the router isn't convinced that you really mean it when you ask to log in, until you ask it again.

Statistics: Posted by slimhazard — Thu Mar 21, 2024 5:22 pm



Viewing all articles
Browse latest Browse all 4804

Trending Articles