Putting electron to sleep if it can't successfully connect to cloud

Hi all,

I noticed this morning that my Electron was stuck for almost 2 hours blinking cyan quickly but not actually connecting to the cloud. That was a huge battery drain and I’m trying to write a code to put it to sleep for 10 minutes if it can’t establish a connection after 5 minutes (it’s currently in AUTOMATIC mode):

if (!waitFor(Cellular.ready, 300000)) {
System.sleep(600000, SLEEP_NETWORK_STANDBY);
}

Here’s my question: Where exactly should I put this code? If I put it in void setup(), will it actually execute this code as it’s flashing cyan and trying to connect to the cloud? At what point does the Electron actually start to execute code in void setup()? Is it only after it establishes a connection to the cloud? If so, how can I get the Electron to sleep if it’s stuck unable to connect to the cloud?

Thanks!

For your use case I think SEMI_AUTOMATIC and SYSTEM_THREAD(ENABLED) would be what I’d opt for.

Thanks Scruff!

I'll give it a try...so my understanding is that in semi_automatic mode, the electron will start executing code right away without waiting to connect to the cloud, correct?

Yes it should :slight_smile: Only if you call Particle.connect though will it start establishing a connection.

I have something like this in my codes.

case CONNECT: {
  if (millis() - lastConnect >= 8000) {
     lastConnect = millis();
     Particle.connect();
  }
  if (Particle.connected()) {
      Serial.println("connection established!");
      state = PUBLISH;
      break;
      }
  if (millis() - connectTime >= 30000) {
  	Serial.println("failed to connect"); 
  	state = SLEEP;
  	break;
        }
  }
    break;
1 Like