Wifi Kit 32 detect switch to battery power

With the Heltec Wifi Kit 32, can the power source be obtained programmatically from any API calls?

ie Most of the time the device would be connected to power, but if the power fails, then as it auto switches to run on battery, I would like to change to power saving strategy to enable sleep between measurements, power down WiFi, etc.

I figure I can wire from the incoming power to a digital pin to determine if the mains power is still on, but wanted to know if there was a built in way that would not require sacrificing one of the GPIO pins for the monitoring.

Thanks in advance

You can read the battery voltage directly through the voltage divider circuit, that is, remove the MOS tube on the circuit for measuring the battery voltage. The way to increase MOS tube and GPIO control is to further reduce power consumption.

Yes but how do I know if the Battery is powering or the unit is being powered by USB? If the voltage on the battery goes down after 5 or 10 minutes that could be an indication of running on Battery, but wondered if there was a better method. Thoughts?

You can judge by the voltage of VBAT. If the voltage of VBAT is around 4.2V, it is powered by USB.

Looks like it will take more than 1 minute perhaps even more than 2 minutes on Battery to get the trend of the battery going down. When I had it running on battery with the demo Battery Program, the mV changed from 4212 to 4207 back to 4212 then 4205, 4207, 4212 repeating similar pattern through several loops and about a minute before dropping to 4192 and then back to 4205.

Also if would lose USB power before the battery is fully charged it could still take a few minutes of comparisons to know if on battery.

Hence why I was wondering if there was a programmatic call that I can check if on batter or USB. If not I will need to monitor the incoming power using a GPIO pin, but would like to not lose it.

ie Is there another way to tell other than looking at the battery voltage?

I decided to go ahead and build a simple voltage divider circuit from the 5V pin to give 2.5V to a GPIO pin to indicate power from USB. Then I should be able to programmatically react very quickly to a power outage.

Thanks Shaffer.