Wake from Sleep when battery charging changes state


I was wondering if there is a way to detect when there is a power input change (ie external power connected) and use that to wake from sleep? I am powering the trackers via the M8 connector. My other option is to take the power input to a digital input to wake from, but it would definitely be easier if there was a way to do this in software as I will need to add a voltage divider in order to keep that IO input below 3.3V.

Yes, it is not only possible, it’s surprisingly easy:

void setup()
    TrackerSleep::instance().wakeFor(PMIC_INT, FALLING);

Just add the wakeFor line to your setup() function and that’s it. It will wake USB or VIN power added or removed. It will also wake for charge complete, so you might need to filter that out.

Is there a similar function that works with the eseries?

On the Electron and E Series, there is of course no TrackerSleep class. However, when you go into sleep mode, add a gpio(LOW_BAT_UC, FALLING) to your other sleep configuration settings to have the same effect.

Thank you Rick!

I have been playing with that addition as well as some other code, because I want the tracker to stay awake if power is being supplied to it, and stay in normal sleep mode when power is not connected.

So far, what I have tried is not working reliably. I was trying to use if statements in the main loop to check the parameter System.batteryState() and execute either a pauseSleep() or resumeSleep() function. I am not sure what all of the battery state integers relate to. So far I think 2 is charing, 3 is charged, and 4 is discharging. Is there further documentation on this? Sometimes when power is cycled, it works correctly. Other times, it gets stuck flashing the green LED forever and never publishes or connects.

Also, is there an easier way to do this? If not, I will post what I have so far to see if you can spot the error.

The battery state codes are here however they can occasionally be indeterminate.

In order to check whether external power is available, this is a better test:

PMIC pmic;
if (pmic.isPowerGood()) {
    // Is powered externally (USB, VIN, or M8)
else {
    // Is powered by battery