waitFor() or waitUnitl(), Automatic Mode, No Threading

For an Electron running 0.8.0.rc-11.
This project is simple, Deep Sleep for 4 hours, wake up and publish a reading.
The Electron will be installed in an area with poor cellular coverage.
It’s battery powered with Solar Recharge, so I would like to limit the Awake time spent trying to Connect. The Publish every 4 hours isn’t critical.
I much rather the Electron go back to Deep Sleep than spend hours trying to connect if something goes wrong.
I’m looking for the simple way to limit the connection attempt to 5 minutes for each wake event.

From searching the forum, it appears Semi_Automatic mode and waitFor(Particle.connected, 300000) is required, but I don’t exactly know how to ensure all the proper housekeeping is completed before Deep Sleep when managing the Connection in code verses Auto Mode.

Currently, I’m using Automatic Mode, No Threading:

void loop()
{  
  waitUntil(Particle.connected);
  Switch = digitalRead(Door);
  if (Switch == 1)   
    {                    
     // Publish here
    }            

  for(uint32_t ms = millis(); millis() - ms < 30000; Particle.process());
  System.sleep(SLEEP_MODE_DEEP, sleepTime);  // 4 hours
}
  

Suggestions appreciated

Actually there is no difference in going to sleep between the system modes.

However, I'd use manual mode together with SYSTEM_THREAD(ENABLED). The latter should take care of the regular calling of Particle.process() and the former is less blocking in case of Particle.connect() (and other functions).

As you said, I'd also use waitFor() and if it times out call Particle.disconnect() and before goint to sleep Cellular.off() plus 200ms delay.

If you are calculating a dynamic sleepTime you may want to constrain its value to be no less than 10seconds and no more than a reasonable maximum for your needs.

Thanks @ScruffR, You’re a gentleman and a scholar !

1 Like

Do you see anything that I should modify ?

SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);

int sleepTime = 14400;  // (seconds) 14,400 = 4 hours

void setup()
{
}


void loop()
{
  Particle.connect();

  if (waitFor(Particle.connected, 300000)) {  // (ms) Limit the Connection attempt to 5 minutes

    // Perform Normal Actions Here, and Publish

    Particle.disconnect();
    for (uint32_t ms = millis(); millis() - ms < 500; Particle.process());
    System.sleep(SLEEP_MODE_DEEP, sleepTime);  // Go to Sleep
  }

  else {    // Electron did not connect within 5 minutes
    Particle.disconnect();
    for (uint32_t ms = millis(); millis() - ms < 500; Particle.process());
    System.sleep(SLEEP_MODE_DEEP, sleepTime);  // Go to Sleep
  }

}
1 Like

Do you see any issues with this running?

It should not be required, but since some device OS versions did require WiFi.on() before Particle.connect() and as I said

That is to properly power down the cellular module.

Since the end of your if() branch and the else branch are doing exactly the same thing, I'd make that unconditional.