Errorhandling when WiFi.connect() fails

Hi,

currently I run my Photons in Manual Mode and do the following to connect to my WiFi:

WiFi.on();
WiFi.connect();

And it seems that one Photon, the one which is two rooms away from my Router, can’t connect to the WiFi every time the device wakes up from deep sleep. But what should I do when WiFi.connect() fails and how long does the Photon try to connect to the WiFi? Is there a best practice for that?

And another question, is there a way to set a limit of seconds (let’s say 10 seconds) to try to connect to the WiFi and after 10 seconds the Photon goes into deep sleep if the connection fails?

Thank you in advance!
David

This?
https://docs.particle.io/reference/firmware/photon/#waiting-for-the-system

1 Like

Ah okay. Am I get this right?

WiFi.on();
WiFi.connect();
if (waitFor(WiFi.ready, 10000)) {
   //do stuff when wifi is ready
}
//do stuff when wifi not ready within 10 seconds

@larusso, you may want to reverse the logic on your waitFor() to make things easier:

if (!waitFor(WiFi.ready, 10000)) {
  //do stuff when wifi not ready within 10 seconds
}
//do stuff when wifi is ready

This is a scenario I use for many of my apps. If wifi (or Cloud) doesn’t connect within the delay, I force a deep sleep for a period of time. Or, I set a flag that tells my app not to use TCP/UDP/Cloud commands and simply buffer data. Or I reset the Photon with a System.reset(). What you do all depends on your application. :wink:

1 Like

Thank you very much! I’ll implement that :grinning:

Edit: @peekay123
I’m not sure if this is working like expected. If I’m right, WiFi.connect(); tries to connect to the WiFi until an internal timer interrupts this process beacuse it’s taking to long. Is this right? If so,

if (waitFor(WiFi.ready, 10000)) {
   //do stuff when wifi is ready
}

make no sense, because WiFi can’t be ready because WiFi.connect(); failed.

1 Like