Photon Won't Connect to Cloud after Stop

I’m using the following code to place the Photon into Stop mode after a Timer event has expired:

System.sleep(D2,RISING,60);

The Photon goes into sleep mode then wakes up after 60 seconds.
However, it never connects to the Cloud (breathes green).
Do I have to force it to connect every time it wakes up from Stop?

edit:
I should add that this also happens when the Photon wakes from a button press on D2.
Also, all my timers that are started in the Setup() function stop working.

Hey there @Kokovec

Would you be willing to share the rest of the firmware code that’s running on the device? That sounds like odd behavior.

I feel foolish but I spent so much time looking at the Particle docs for the answer that I neglected to take a thorough look at my code.
My code used to turn Wifi off and I was changing that to Stop mode.
It turns out that I had a Particle.disconnect() still in my Timer callback function that wasn’t properly commented out.

My bad.

I do have a question though.
Is there any way to tell if the Photon was woken either by interrupt or the timer?

The only way currently is to compare the wake time to the set time for wake, and if they differ enough you got woken by button.
An alternative would be to add an external sample’n’hold circuit on the button to keep the button signal long enough for the device to wake and read the pin state.

I decided to use your first suggestion and capture the time on sleep and the time on wake.
But then I ran into a strange problem.
My code simply captures Time.now() just before it goes to sleep.
Then upon wake it does another Time.now() capture.
I subtract sleep time from wake time and it gives me the number of seconds that have passed since the Photon went to sleep.
However, once in a while the Photon wakes prematurely.

The Photon is set to go to sleep for a period of 3600 seconds (or one hour).
So I had my Photon calculate the sleep time (as mentioned above) and published each event to a DynamoDB table.
As you can see from the screen capture below the wake up times are not consistent.

The pattern the Photon follows is that it’s awake for one minute and then asleep for an hour.
If someone hits the button it sounds an alarm.
But if the Photon wakes up prematurely the alarm will sound anyway which would get pretty annoying.

That isn’t expected behaviour :wink:

Are you using an external pull-resistor for your switch?
Or could it be that there are some power spikes that trigger a wake?
Could it be that the sleep time gets calculated wrong from time to time?
Try using fix 3600 instead of a calculated time.

I’m powering my Photon with a Sparkfun 2000mAh Li-ion battery and using the Photon battery shield.
The “wakeup” pin is tied to the 3.3v supplied by the battery.
I didn’t have a pull-down resistor when I originally started running the test.
However I added a 10K pulldown resistor to the D2 pin and so far it’s been working rather well.
I’m also measuring the battery SOC as I want to see if things change as the battery depletes down to the critical mark.
Here are the results thus far accumulated over time…

{
“BatteryLevel”: [
“94”,
“101”,
“100”,
“99”,
“99”,
“99”,
“99”,
“99”,
“98”,
“98”,
“98”,
“97”,
“97”,
“97”,
“96”,
“96”,
“96”,
“95”,
“95”,
“95”,
“95”,
“94”
],
“Name”: “koko”,
“Photon”: “Photon”,
“RunTimeSec”: [
“3603”,
“3601”,
“3601”,
“3601”,
“3601”,
“3601”,
“3601”,
“3607”,
“3601”,
“3601”,
“3603”,
“3603”,
“3601”,
“3607”,
“3601”,
“3601”,
“3601”,
“3601”,
“3602”,
“3601”,
“3603”,
“3601”
]
}

So things were working great until the Particle cloud had those issues a little while back.
Now my Photon will occasionally wake from sleep, fail to connect to the cloud and, pulse green and totally lock up until I hit the reset button.

The Photon is in Automatic mode.
Here’s my code that handles Photon sleep/wake:

 // check if we need to go to sleep
if(goToSleep == 1) {
    Serial.println("Going to sleep");
    goToSleep = 0;
    // save time that we went to sleep
    UTCwentToSleep = Time.now();
    Serial.println(String::format("Time went to sleep: %d",UTCwentToSleep));
    System.sleep(D2,RISING,3600);
}
// check if we came back from sleep
if(UTCwentToSleep != 0) {
    UTCwokeUp = Time.now();
    Serial.println(String::format("Time woke up: %d",UTCwokeUp));
    SleepTimeSec = UTCwokeUp - UTCwentToSleep;
    batSoc = getBatSOC("");
    String mydata = "{\"MessageType\":\"Timestamp\",\"Location\":\"Room 1\",\"RunTime\":\""+ String(SleepTimeSec)+ "\",\"BatterySOC\":\""+String(batSoc)+"\""+"}";
    Serial.println(mydata);
    if(Particle.connected()) {
        // Trigger the webhook
        Particle.publish("ButtonPanic", mydata, PRIVATE);
    }
    UTCwentToSleep = 0;

I’m guessing that my user code is blocked until the Particle connects to the cloud.
Should I be using semi-automatic mode instead?