One more thing, this unexpected connection is also blocking execution of the main loop. I don’t understand why we can’t ensure that the main loop will be serviced while the Electron goes through the 30+ second process of connecting to Particle.
Make sure you check Particle.connected() before you call Particle.publish(). If you call Particle.publish() while the cloud is enabled but not connected, the function will block until the cloud is actually connected.
I think you need a delay(1000); after Cellular.off(). The problem is that in threaded mode Cellular.off() is non-blocking but there’s no way to tell when it’s actually done. If you go to sleep immediately the cellular state is recorded as on, because it hasn’t been turned off yet, so it wakes, it goes back into on state.
This post is for SLEEP_MODE_DEEP but it shows some of the places you’ll run into the same problems:
I have now identified the issue - it is the way that the Electron is handing sleep. In the code below, if I use System.sleep(int seconds); the code functions as expected. Once disconnected, it will not reconnect until you clear the lowPowerMode flag. However, if you use System.sleep(intPin, RISING, int seconds); you get the unexpected behavior: the Electron will attempt to connect to Particle without any instructions in code to do so.
Based on a suggestion from @ScruffR, I also tried detaching the interrupt before sleep and reattaching after, this had no effect so I took it out of the code to keep it down to a one line change.
I think this may be a flaw in the firmware.
Please take a look. I will open a help desk ticket on this issue Monday.
Thanks to @rickkas7, this issue has been solved. It turns out that, even in MANUAL mode, the Electron will attempt to restore the connection state when it wakes from Sleep. So, the fix is to insert a Particle.disconnect() line before Cellular.off() so the Electron knows it will be in disconnected mode. Please see example code below, commenting out the Particle.disconnect() line will demonstrate the issue.
@rickkas7 is going to ensure that the documentation is updated to better explain this behavior.
Here is the final sample code - no hardware or sensors required:slight_smile:
// Minimal code to display weird connection behaviour
// By weird behaviour, I mean the Electron connecting to the Particle cloud when there is
// no reason to do so. I have isolated this issue to the Sleep command
// Prototypes and System Mode calls
SYSTEM_MODE(MANUAL); // This will enable user code to start executing automatically - OR SO WE ARE TOLD! ;-)
SYSTEM_THREAD(ENABLED); // Means my code will not be held up by Particle processes.
const int blueLED = D7;
void setup() // Note: Disconnected Setup()
Particle.connect(); // Connects to Particle
Particle.process(); // Since we are in Manual mode, we need to process
Particle.disconnect(); // This is the fix - Electron wants to restore connected state after waking
delay(1000); // Delay to rate limit loop and publishes
delay(1000); // Advice from Rickkas - https://community.particle.io/t/electron-sleep-problems-yet-again/42230/11
System.sleep(D3,RISING,30); // If we use this line instead, the Electron will attempt to connect to Particle on every interrupt
@chipmc, I think the other way of looking at it is that the Electron will restore the state of the Cloud connection prior to it going to sleep, regardless of the SYSTEM_MODE. In your case, since the Cloud connection was still active, it restored it upon rebooting. @rickkas7, can confirm this.