Electron taking to long to wake up from sleep

I’m working on a project that needs to go to sleep when not in use, to conserve battery, and wake up quickly to take measurements, but the electron is taking too long to wake up.

if (quietCount >= QUIETAMOUNT)  //sleep count
        {
            #ifdef DEBUG
                Serial.println("Going to sleep!");
                Serial.print(Time.hour()); Serial.print(":");
                Serial.print(Time.minute()); Serial.print(":");
                Serial.print(Time.second()); Serial.println();
                delay(10);
            #endif
            quietCount = 0;   //already going to sleep
            //how long until the device needs to wake up at the next measurement period?
            
            accel_standby();
            read_register(ACCELADDRESS, 0x16);    //reset interrupt pin
            accel_active();
            
            int secToNextMin = 60 - Time.second();
            int minToNextHour = 59 - Time.minute();
            int currentHour = Time.hour();
            if(currentHour % 2 == 0)    //odd or even hour
                seconds = secToNextMin + ((minToNextHour)*60) + 3600;
            else
                seconds = secToNextMin + ((minToNextHour)*60);
            
            delay(10);
                
            System.sleep(WKP, FALLING, seconds);   //sleep until time is up, or pin WKP goes high
            
            delay(1000);
            
            #ifdef DEBUG
                Serial.println("Pressing snooze");
            #endif
            
            accel_standby();
            read_register(ACCELADDRESS, 0x16);    //reset interrupt pin
            accel_active();
            
            #ifdef DEBUG
                Serial.println("Waking up!"); Serial.println();
            #endif
        }

This is the sleep portion of the code (the entire code is very long)

I’m counting the up and down movements of an accelerometer, and the device goes to sleep after a couple of seconds without movement and wakes up when it moves again.

When the device wakes up the embedded led flashes green for about 50 seconds before breathing green and moving on with the program. I need this cut down to no longer than 1 or 2 seconds, preferably less. If I had to guess, the electron is getting caught up in trying to connect to cellular. I’m using SYSTEM_MODE(SEMI_AUTOMATIC); and keeping cellular disconnected because I don’t need it and I’m working in an area with pretty bad reception.

Is there a way to minimize it looking for a signal it doesn’t need, can’t have, and I don’t want? If that’s not the problem, does anyone have any idea how to cut down the time to fully wake up? Thanks in advance

@MackD, you need to consider using SYSTEM_THREAD(ENABLED) which will run you application on its own thread.

1 Like

Your device is never connecting to the cloud?
In that case you may need to make sure the Time object is initialised and signals Time.isValid() otherwise that could block you next time you are requesting the time from the RTC.

1 Like

@peekay123
I think your suggestion worked, the board is waking up almost immediately. Occasionally it takes a while for it to get to sleep, which is a new problem and could mess up the interrupt that I’m using. Any time it takes to get to sleep is time that the accelerometer could move and fire its interrupt before it’s needed, but it isn’t a consistent problem, so I don’t know what to do about it.

@ScruffR
The device does connect to the cloud when it’s setting up, and once per day when I use Particle.publish() in a different part of the code. I am syncing the time while the particle is connected once a day.

Are you then actively disconnecting from the cloud again?
If not, the connection will persist and after waking from Stop Mode sleep (which you are using) the system will try to reestablish the previous state (i.e. reconnect) - unlinke Deep Sleep where the device will start the way as set by SYSTEM_MODE().
That's also why @peekay123's solution helped - however, IMO it only mitigates the effect of the unwarranted reconnect and disguises the underlying problem.

2 Likes

@ScruffR
Yes, I am disconnecting from the cloud after every reconnect. After a few tests, it consistently works, I’m not having any more trouble with the sleep/wake-up feature. Thanks for the help!

1 Like