How to read battery voltage before connecting to wifi

Hi I have IMP002 and a boost converter like in Nora reference design. I would like to go to sleep for 5 seconds, wake up, read battery voltage, connect to wifi (that enables boost) and then do server.log(batteryvoltage). However the first time the code returns the boost voltage (3.3V) and not the battery voltage (2.7V). How do make sure device is disconnected and boost enable is off? I’m still learning to write better code. `local batteryvoltage

function OnIdleFuntion() {
server.disconnect();
imp.setpoweren(false)
while(server.isconnected()) {
//wait untill disconnected
}
batteryvoltage = hardware.voltage()
server.connect()
server.log("batteryvoltage: "+ batteryvoltage)
server.sleepfor(5)
}

imp.onidle(OnIdleFuntion)`

Hi Jasper,
When you disable the boost the voltage won’t come down to VBAT immediately since the caps are still charged to 3V3. You may just need to need to wait longer after you disable the boost before reading the voltage.

Hi Brandon,
I think you’re right. Below code works.
`local batteryvoltage

function dosomething(){
batteryvoltage = hardware.voltage()
server.connect()
server.log("batteryvoltage: "+ batteryvoltage)
server.sleepfor(61)
}

function main(){
server.log(“going to disconnect”)
server.disconnect();
imp.wakeup(0.1, dosomething);//0.1s works, but higher is better
}

main();`

You are reading the hardware imp voltage (3.3V), not the voltage of the battery powering the device, for that you will need to set a pin to an analog input. You must use a voltage divider if your battery is more than 3.3V.

No, we use 2xAA battery (vbat is max 3.6V) and a Torex XC9128 boost converter. When enabled the voltage is 3.3V, when disabled the voltage is vbat. All I need to do is to disable the boost converter, wait a couple of seconds and then read the voltage.

If you sample the voltage at wake time before you do a connection, that might be a more efficient time to do it? Wifi is off at wake time.

Right now in above test code the IMP wakes up, connects to wifi, disconnects from wifi, read battery voltage, connect to wifi and then sleeps for a minute. Can you suggest some code to: wake up, read battery voltage, and then connect to wifi?

Just read at the top of your code. Wifi does not come up at wake until you do something that requires the network.

It will connect on a cold boot, but any other wake the imp will not be connected as your code starts to run.

`local batteryvoltage

function dosomething(){
batteryvoltage = hardware.voltage()
//there is no need to to server.connect
// because server.log automatically connects
server.log("batteryvoltage: "+ batteryvoltage)
server.sleepfor(61)
}

imp.wakeup(0.0, dosomething);`
This test code worked for us. Thanks.

Batteries tend to recover when the load current is very low therefore the voltage when the battery is not under normal load doesn’t indicate the state of remaining charge or for AAs the remaining capacity.

I will make sure that load is same every time we read voltage. We do a battery test and we will learn how many sleep-wake-connect cycles the battery lasts and also how the voltage profile looks over the battery lifetime.