Particle Photon Connecting Timeout

I have a Particle Photon. I would like to be able to tell the device to go into sleep mode for a while if it’s unable to connect to WiFi after trying for some amount of time. I have tried the following:

time1 = millis();   
while(WiFi.connecting())
{
    if((millis() - time1) >= 30000)
    {
        System.sleep(SLEEP_MODE_DEEP, 60); 
        break;
    }
}

That’s at the top of my loop(). However, when I turn off the WiFi that the Photon connects to, it just flashes green until I turn the WiFi back on (at my router). Anyone have any ideas?

I just want it to basically give up and save power to try again later when it can’t connect to WiFi. When it can connect, everything works fine.

Thank you in advance!

Are you using SYSTEM_THREAD(ENABLED)?
If not your code will stall as soon the connection is lost and probably won’t get to that portion of your code.

I don’t call SYSTEM_THREAD(ENABLED) anywhere… what does that do? Should I add it in the setup() function?

No, you should not add it to setup() but outside at the top of your code

To know what it does you can have a read here
https://docs.particle.io/reference/device-os/firmware/photon/#system-thread

In a nutshell it will make sure your application is running independently of the cloud stuff which will be executed on its own “system thread”.

Thank you! I added this line and switched my code around to use some WiFi.ready() if statements and it’s working like a charm. The SYSTEM_THREAD(ENABLED) was key to getting my code to run instead of it halting on blinking green forever if it can’t see WiFi. Thanks!

1 Like