Turning off the photon if WiFi is not available

@rickkas7, that seems overly complicated when waifFor() could be used instead as @Moors7 pointed out :

const unsigned long MAX_TIME_TO_CONNECT_MS = 30000;


void loop() {
  if (!waitFor(Particle.connnected, MAX_TIME_TO_CONNECT_MS) {
    // timeout occured go back to deep sleep
    System.sleep(SLEEP_MODE_DEEP, 30);
  }

  // Connection established 
  // ...
}

I use SYSTEM_MODE(SEMI_AUTOMATIC) so I control the connection. In some cases, if Particle.connect() fails, I run Particle.disconnect(), wait a small delay then run Particle.connect() and test for the connection again. If that retry fails, then I go back to sleep.

I do all data collection BEFORE trying to establish the connection, storing data on an SD or in retained RAM. That way if a timeout occurs, I can send it on the next successful connection. :wink:

1 Like