Electron power when failing to connect

I have an electron which is taking readings every 10 minutes and publishing an event to the cloud ( and then my database) with the data along with the RSSI and battery level.

the other day there was a few periods where it did not upload any data for 40 minutes or in one case 3 hours. I checked the logs and in this time I have the device came online event twice.

So I am assuming that for one reason or another the particle failed to connect. I’m unsure as to whether this was a power problem or connectivity but I know that there was some cellular maintenance in my area!

In this 40 minutes the battery fell significantly more than the average level

Soooo… my question is this: if the particle fails to connect to a network does it just keep trying and trying until it gets a connection and thus drains the battery?

If so is there a way I can tell it to try a couple of times and if that fails then go back to sleep and try again the next time round?

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

@Moors7

Thank you

so if I add this to the beginning of my loop

if (waitFor(Particle.connected, 10000)) {

do I then close it and add an else statement at the end telling it to sleep?

e.g

void loop() {
  
  if (waitFor(Particle.connected, 10000)) {
    
    // whatever happens in my loop here
    
  }
  else
  System.sleep(WKP, RISING, 10, SLEEP_NETWORK_STANDBY);

Yup, but 10sec will be too little for a reliable connect. A cellular connect can take up to 5min, when a full handshake is required - plus a few sec for the cloud connect.
And you should have SYSTEM_THREAD(ENABLED) for waitFor() to work properly.

Is it best to put the waitFor at the beginning or just to sandwich the particle.publish between it?

I’d put it in a prominent place like directly at the top or bottom of loop() or even “more intuitive” as first statement after sleep() (with the optional Particle.connect() in between, if you can’t be sure what the state was before entering Stop Mode).

Sorry I'm not sure what you mean by this.

As I understand your current code (from hear-say, since unseen ;-)) you are sending your device to sleep after its task and wake 10 minutes later to do the same thing again, so after you wake up, you expect the cloud to get reconnected, so it would be most natural to trigger a Particle.connect(), just in case it was lost before sleep earlier, then waitFor() the connection to be established, but go back to sleep if that fails, just after you woke up.

So something like this

SYSTEM_THREAD(ENABLED)
...
void loop()
  static bool taskCompleted = false;  // gets set true somewhere in your code when done and ready to sleep
  ...
  while(taskCompleted || !Particle.connected())  // just to make sure we got connected otherwise keep sleeping
  {
      System.sleep(WKP, RISING, 600, SLEEP_NETWORK_STANDBY);
      taskCompleted = false;
      Particle.connect();
      waitFor(Particle.connected, 35000);
  }
  ....
}

Ahh I see

so something like this?

SYSTEM_THREAD(ENABLED)

// all my setup stuff

void loop()
  static bool taskCompleted = false;  // gets set true somewhere in your code when done and ready to sleep
  
  // take my sensor readings
  // publish to particle
  // set taskCompleted=true
  
  
  while(taskCompleted || !Particle.connected())  // just to make sure we got connected otherwise keep sleeping
  {
      System.sleep(WKP, RISING, 600, SLEEP_NETWORK_STANDBY);
      taskCompleted = false;
      Particle.connect();
      waitFor(Particle.connected, 35000);
  }
 
 
}

If I’m following correctly then this happens:

  • Sensor readings are taken

  • Readings are published to particle

  • taskCompleted is set to true which initiates the while() statement

  • Device goes to sleep for set time

  • device wakes up and tries to connect to the cloud

  • if it can’t connect then it goes back to sleep immediately

  • if it does connect then the loop begins again

Is that the logic of it?

What happens when the device starts for the first time if it doesn’t have a connection?

Thank you very much for your time, I appreciate I might be asking obvious questions…

The first time case in particular, but it’s generally a good idea, you just need to wrap the publishing part into a if (Particle.connected()) block and will just have take one reading in vain.
Or you put my block of code before your actual task and the device would go to sleep immediately.

But otherwise, the logic is just as you said.