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 = Time.now();
            bool havesentence = false;
            while(!havesentence && (Time.now() - gps_attempt_startsecs) < 5) {
                
                watchdog_pat();
                if (leddebug) RGB.color(10, 0, 0); //red
                
                long serial_startsecs = Time.now();
                while(Serial1.available()) {
                    char readbyte = (char)Serial1.read();
                    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 = (Time.now() - 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 Time.now() to determine the seconds the gps fix takes and not millis() or Time.local()?

1 Like