How can I turn on an output when WiFi is not connected (blinking green)?

Pretty simple question but I couldn’t find the answer in the docs or on the forum: if the Photon is in blinking green mode (no WiFi connection), I would like to turn on one of my outputs.

I tried the following in both loop() and setup() but it didn’t work:
if(Particle.connected() == false)
{
Serial.print(“not connected”);
Serial.println();
digitalWrite(relayOut, HIGH);
}

Interestingly, when placed in loop(), it does print “not connected” a few times, but the LED I have connected to relayOut (D0) doesn’t turn on. I would think that there’s a proper way to do this, I just haven’t found it yet.

Thanks!

We would need a bigger code snippet ideally. However, if you have it in the standard non-threaded mode with the cloud connection in AUTOMATIC then when you turn the Photon on it won’t run your user code until it has made a connection.

1 Like

Thanks. To be clear, the code snippet I pasted is simply the first thing under void loop() right now. There is other code that is cloud-dependent that can turn relayOut on or off, but obviously it doesn’t do anything until the WiFi is up.

I saw some mention of modes other than AUTOMATIC, but they typically required some sort of user interaction (like a button press) to invoke WiFi. I’m open to using something like SEMI-AUTOMATIC mode but I’m not sure how I would use it in this case.

You could just go for SEMI_AUTOMATIC as suggested and add an unconditional Particle.connect() in setup().
As @Viscacha said, with AUTOMATIC mode and in absence of SYSTEM_THREAD(ENABLED) your code will just stall as soon your device loses cloud connection.

1 Like