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
}
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.
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
}
}