Sleep won't wake up

I can get the Photon to do most things I want, but it absolutely refuses to wake up after a timed deep sleep period. This is very simple. What am I doing wrong please?
I am using the Photon as a Central Heating controller powered from a solar charged battery. To save power I want to switch it off during the later hours of darkness.
Device goes to sleep fine - power consumption drops to almost zero (important). After 40 minutes the LEDs stay off and it needs a press of the reset button to get things going again.

Setup and loop code is thus:

void setup()
{
    Particle.function("digitalread", tinkerDigitalRead);
    Particle.function("digitalwrite", tinkerDigitalWrite);
    Particle.function("analogread", tinkerAnalogRead);
    Particle.function("analogwrite", tinkerAnalogWrite);

    tinkerDigitalWrite("D0 LOW");                   // CH control set safe
    tinkerDigitalWrite("D1 LOW");                   // HW control set safe
    pinMode(2, INPUT_PULLDOWN);                     // CH status input
    pinMode(3, INPUT_PULLDOWN);                     // HW status input
    pinMode(4, INPUT_PULLDOWN);                     // pump status input
    pinMode(7, OUTPUT);                             // LED driver is output
    pinMode(10, INPUT);                             // Top HW temp
    pinMode(11, INPUT);                             // Bottom HW temp
    pinMode(14, INPUT);                             // Supply voltage (12V)
}

void loop()
{
    delay(200);          // 200ms loop delay
    if (Time.hour() > 0 && Time.hour() < 10) {
        tinkerDigitalWrite("D7 HIGH"); 
        delay(600);
        tinkerDigitalWrite("D7 LOW");
        delay(1600);
        if (sleeping == false) { 
            System.sleep(SLEEP_MODE_DEEP, 2400);  // sleep for 40 minutes
            sleeping = true;
        }     
    }
    else {
        tinkerDigitalWrite("D7 LOW");
        sleeping = false;
    }
}

I did a little searching around, and if I understand correctly, waking from deep sleep is like powering it up after it is off, so doesn't the code stop executing after System.sleep(), never executing the rest of the code - sleeping will never be true.

** Update - check here..

Also, try declaring your pins with their proper names, e.g. A1/D1. Currently, it could be either. I’m not sure how your tinkerDigitalWrite function looks like, but it might be calling pins before declaring them.

Please try adding this in your source code (outside of any function)

STARTUP(Time.setTime(0));

For testing, it’s probably a good idea to reduce the sleep time to a few seconds! :slight_smile:

Cheers,
mat.

PS: The 0.4.5 firmware will do this automatically so no need for that in later versions.

Without changing any other code, I reduced the deep_sleep period to 40 seconds. It worked - the Photon sprang into life again without the help of the reset button. I then extended the sleep period to 80 seconds - that worked OK too. Tonight’s experiment is to try a 180 second sleep period. I shall post results tomorrow.

Thanks for your suggestions guys!

180 second deep sleep period worked and woke up OK. I will not push the period further as the sleep to wake ratio is now about 50:1 so I could only gain about another 1-2% battery life extension. Not worth the hassle.
Could there be a limit as to how long one is allowed to “deep sleep” and still wake up? Maybe some counter eventually overflows? If I ever find out what the limit is, I’ll certainly post it here!

Did you try the STARTUP() change I mentioned above?

I haven’t tried that yet. I’ll see how tonight’s sleep goes then try Startup() tomorrow.
Fighting with Windows 10 at the moment!

Yes I did add the STARTUP line and that seems to have sorted the problem. A 9 minute deep sleep went off OK so last attempt is for a 20 minute deep sleep.
Many thanks for your suggestion.