Best practice for Wifi.scan

Trying to figure out what the best practice is for calling Wifi.scan in order to a) have it return as quickly as possible b) deal with it in case it doesn’t return quickly (in terms of not crashing). It seems to me when I run “particle setup” from the CLI that scanning takes about 3-4 seconds, however depending on what state the WiFi is actively in, it could take anywhere from 1/2 a second to 20 seconds to return when I call it for my application code. A couple notes and observations:




  • If I call WiFi.scan() when WiFI is on but before it connects to Particle then it returns quickly (< 1 second?)
  • If I call WiFi.scan() when Particle is connected to the cloud it can take me 20+ seconds to return an my app crashes
  • If I call WiFi.scan() when Particle was connected and I call Particle.disconnect() right before scanning it behaves the same as if connected, long return time, crashes my app (error code Hard fault)

I understand I may be doing somethings wrong here but that’s why I’m asking


  • Is there a state I should make sure that my system is in before I call WiFi.scan in order to get it to return the quickest?
  • Is there any particular reason that if it doesn’t return after 10-20 seconds my app would crash?
  • What can I do to improve my situation here?

There are mulitple possible causes to that, most of them directly related to your code, so it would be best to see what your code is doing.
Especially in multi threading scenarios thread interactions can cause this but are hard to pinpoint.

I’m pretty sure that the scanning for networks is best performed when the module hasn’t established a WiFi connection at all. While it should be possible in other cases too, I’m not entirely sure whether the module is actually supposed to maintain and service the current connection while scanning and depending on the ouline of your own code that could be causing troubles.

Thanks for chiming in, the slow scanning turned out to be a hardware issues actually. Once I got that figured out, I now have consistently quick WiFi scanning.

1 Like