Electron sleep strategy for variable sleep times?

I’ve been working on a “simple” Electron locator using the Google Maps integration. I wanted to work sleep modes into it, because I want it to run on battery, so that it would publish a location, then sleep for a while. At one point I had that working as expected. But then I wanted to get fancy and have it determined whether it was stationary or in motion, and sleep longer or shorter, accordingly. In the process of making those changes, I managed to screw up my state tracking, so now it’s not sleeping/waking/tracking correctly. I think part of my problem is that I started with a particular sleep mode example which just wasn’t the most appropriate for my use-case. It’s not the example’s fault, it’s mine.

So, anyhow, I’m going to pretty much re-write it from scratch, based on what I’ve learned. so far. My question isn’t about making the code work, I can do that. My question is:

I’m basically going to save the previously known location (as retained variables), and compare it to the current location and accuracy level to see if we’ve moved or not. If we are moving, I want to track location probably about every 1-3 minutes (maybe sleeping in between?). If we’ve stopped, I plan to increase the sleep time by one minute after each wake, up to a maximum of, say, 15 minutes.

Based on those criteria, what are the best strategies for me to use regarding the sleep modes vs maybe just doing cellular_off(NULL) between readings?

Based on @rickkas7’s article on choosing sleep modes, it looks like for sleeping 12 minutes or less, I should use SLEEP_NETWORK_STANDBY, and for more than 12 minutes, use SLEEP_MODE_DEEP, yes?

But what about for the shallow end of the pool? If I want to get location once a minute, is it worth sleeping at all? Or turning the modem off and back on again? What about for 2 minutes, or 3?

Stop mode sleep (pin + time) with SLEEP_NETWORK_STANDBY typically has no data overhead when waking up, so it’s useful to sleep for periods longer than maybe 15 seconds or so. The lower bound is only because sometimes it takes a little while to go to sleep, so if you make it very short (say 5 seconds), you’ll end up sleeping much longer than that sometimes.

For 15 minute sleep I’d still go with stop mode sleep (pin + time) with SLEEP_NETWORK_STANDBY. If you’re in an area with poor cellular coverage the time to reconnect to cellular (blinking green) will be much longer and you’ll use more power than the calculations in the sleep mode guide.


Okay, thanks for the input! So, what is the best situation for using SLEEP_MODE_DEEP? When the sleep time will be longer than the cellular keepalive time? Which is something like 23 minutes, IIRC?

Yes, if you need to sleep more than 20 minutes or so that’s where SLEEP_MODE_DEEP is almost always going to use less power.

1 Like

Excellent, this was exactly the info I needed.