[nfr52] battery level

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
msam
Posts: 4
Joined: Sat Aug 11, 2018 1:55 pm

[nfr52] battery level

Post by msam » Fri Aug 17, 2018 8:47 pm

Hi all,
Any nordic developer ?
I am trying the following code on nrf52 module:

MicroPython v1.9.4-378-gab81578-dirty on 2018-08-17; PCA10040 with NRF52832
Type "help()" for more information.
>> import machine
>>> machine.ADC.battery_level()
100
>>>
Then I installed the code below.
I can connect to nrf52 module using Nordic nRF Connect but I am reading battery level value 28%.
Any tips ?
from ubluepy import Service, Characteristic, UUID, Peripheral, constants
from machine import ADC

def event_handler(id, handle, data):
global periph
global serv_batt

if id == constants.EVT_GAP_CONNECTED:
LED(1).on()
elif id == constants.EVT_GAP_DISCONNECTED:
LED(1).off()
periph.advertise(device_name="micr_batt", services=[serv_batt])
elif id == constants.EVT_GATTS_READ:
data[0]= ADC.battery_level()
return

def start():
global periph
global serv_batt

LED(1).off()
uuid_batt_service = UUID("0x180F")
uuid_batt_chr = UUID("0x2A19")
serv_batt = Service(uuid_batt_service)
bat_props = Characteristic.PROP_NOTIFY | Characteristic.PROP_READ
bat_attrs = Characteristic.ATTR_CCCD
bat_chr = Characteristic(uuid_batt_chr, props = bat_props, attrs = bat_attrs)
serv_batt.addCharacteristic(bat_chr)

periph = Peripheral()
periph.addService(serv_batt)
periph.setConnectionHandler(event_handler)
periph.advertise(device_name="micr_batt", services=[serv_batt])

c45713
Posts: 17
Joined: Fri Dec 09, 2016 7:09 pm

Re: [nfr52] battery level

Post by c45713 » Thu Aug 30, 2018 9:14 pm

I took a look at this. I also get very strange results on the nrf52 when BLE is enabled. My value is reported to be 0x30 = 48% on coincell using the pca10040.

I used the following code for testing:

Code: Select all

from machine import RTCounter, ADC
from ubluepy import Service, Characteristic, UUID, Peripheral, constants
import machine

def event_handler(id, handle, data):
    global rtc
    global periph
    global batt_notif_enabled
    global char_batt
    global serv_battery

    if id == constants.EVT_GAP_CONNECTED:
        pass

    elif id == constants.EVT_GAP_DISCONNECTED:
        # stop low power timer
        rtc.stop()
        temp_notif_enabled = False
        rain_notif_enabled = False
        # restart advertisment
        periph.advertise(device_name="micr_batt", services=[serv_battery])
        
    elif id == constants.EVT_GATTS_WRITE:
        # a write to handle 17 is the battery characteristic CCCD in this sample.
        if handle == 17:
            if int(data[0]) == 1:
                batt_notif_enabled = True
                rtc.start()
            else:
                batt_notif_enabled = False
                rtc.stop()
    
def rtc_handler(timer_id):
    global batt_notif_enabled
    global char_batt

    if batt_notif_enabled:
        level = ADC.battery_level()
        char_batt.write(bytearray([level]))

batt_notif_enabled = False

# use RTC1 as RTC0 is used by bluetooth stack 
# set up RTC callback every 5 second
rtc = RTCounter(1, period=50, mode=RTCounter.PERIODIC, callback=rtc_handler)

uuid_battery = UUID("0x180F") # Battery service
uuid_battery_level = UUID("0x2A19") # Battery level characteristic

serv_battery = Service(uuid_battery)

battery_props = Characteristic.PROP_NOTIFY | Characteristic.PROP_READ
battery_attrs = Characteristic.ATTR_CCCD
char_batt = Characteristic(uuid_battery, props = battery_props, attrs = battery_attrs)

serv_battery.addCharacteristic(char_batt)

periph = Peripheral()
periph.addService(serv_battery)
periph.setConnectionHandler(event_handler)
periph.advertise(device_name="micr_batt", services=[serv_battery])

while True:
    machine.sleep()
I suspect there might be something wrong in the algorithm of battery_level(), as it is subtracting diode drop, which might not be the case on coin-cell. I'll see if i can help out by checking out the ADC driver for this.

msam
Posts: 4
Joined: Sat Aug 11, 2018 1:55 pm

Re: [nfr52] [solved] battery level

Post by msam » Wed Sep 05, 2018 12:29 pm

Hi c45713,
My code has several errors.Thanks for help.You find the problem:
I need to read battery level and write as bytearray
Your code:
level = ADC.battery_level()
char_batt.write(bytearray([level]))

Just one typo on your code:
char_batt = Characteristic(uuid_battery, props = battery_props, attrs = battery_attrs)
Should be:
char_batt = Characteristic(uuid_battery_level, props = battery_props, attrs = battery_attrs)

Best regards,

Post Reply