System.sleep taking ages to enter

Hi all

Having an intermittent issue where my application is hanging on System.sleep(pin_intwkp,RISING,30);

Sometimes it enters sleep fine, and other times it just hangs on this sleep call.


Could you possibly share more of your code? At least the context of where you are calling System.sleep()?

Sure, so I have a loop that checks for gps fix. I need to sleep the electron during this for 2 reasons, one it’s noisy as affects the gps fix time, and two, to save power.

This is the code block…

while(gps_healthy && fixstage < 2 && gps_secstaken < gps_timeout) { 
            if (leddebug) RGB.color(20, 0, 20); //purple, set led to purple so i can see if sleep command is hanging
            System.sleep(pin_intwkp,RISING,30); //800ua from mcu
            long gps_attempt_startsecs =;
            bool havesentence = false;
            while(!havesentence && ( - gps_attempt_startsecs) < 5) {
                if (leddebug) RGB.color(10, 0, 0); //red
                long serial_startsecs =;
                while(Serial1.available()) {
                    char readbyte = (char);
                    if (gps.encode(readbyte)) {
                        //check for sentence
                        if (gps.satellites.isUpdated()) {
                            havesentence = true;
                            sentencescount ++;
                        //Check for fix
                        if (gps.location.isValid()) {
                            fixstage = 1;
                            if (leddebug) RGB.color(0, 0, 10); //blue
                        //check for good fix
                        if (gps.location.isValid() && (int)gps.hdop.value() > 0.0 && (int)gps.hdop.value() <= 100) {
                            fixstage = 2;
                            if (leddebug) RGB.color(10, 0, 10); //purple
            gps_secstaken = ( - gps_startsecs);

Are you sure that pin_intwkp is not changing at the point System.sleep() is called? Could you put some output to Serial or log traces, also include the time millis() /micros() and similarly after the System.sleep() with the time in millis()/micros(). What is the Device OS?

Curious to know why you are using to determine the seconds the gps fix takes and not millis() or Time.local()?

1 Like