Python Battery reading example

edited November 24 in Python

Hello there,

I'm newbie on both python and metawear. I would like to know how to read battery charge state or percentage.

This is where I tried subscribe to battery status data

battery_data = libmetawear.mbl_mw_settings_get_battery_state_data_signal(s.device.board) libmetawear.mbl_mw_datasignal_subscribe(battery_data, None, s.battery_callback)

and this is where the callback is called

`class Sensor:
def init(self, device):
self.device = device
self.samples = 0
self.callback = FnVoid_VoidP_DataP(self.data_handler)
self.battery_callback = FnVoid_VoidP_DataP(self.battery_handler)
self.magnitude = 0.0

def battery_handler(self, ctx, data):

It shows

{epoch : 1574588997176, extra : 457286542496, value : 2459999801104, type_id : 5, length : 4} {epoch : 2456721293460, extra : 9223429214795562267, value : 3632874409166831648, type_id : -1852471861, length : 217}

Could you please help where I have to look at the value, it's not mV and Percentage as api said?
and why it read only once and stop reading anymore?

Sorry for the format, it's not working.

Thank you in advance,


  • edited November 24

    Hey take a look at this testing code we use in Python: <-- there's a bunch of examples here on how to read the voltage and %.


    def test_read_battery_component(self):
            expected_cmds= [
                    [0x11, 0xcc],
                    [0x11, 0x8c]
        signal= self.libmetawear.mbl_mw_settings_get_battery_state_data_signal(self.board)
        voltage = self.libmetawear.mbl_mw_datasignal_get_component(signal, Const.SETTINGS_BATTERY_VOLTAGE_INDEX)
        charge = self.libmetawear.mbl_mw_datasignal_get_component(signal, Const.SETTINGS_BATTERY_CHARGE_INDEX)
        self.libmetawear.mbl_mw_datasignal_subscribe(charge, None, self.sensor_data_handler)


Sign In or Register to comment.