Python Battery reading example

edited November 2019 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):
    print(data[0])
    print(data[1]`

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? https://mbientlab.com/documents/metawear/cpp/0/structMblMwBatteryState.html
and why it read only once and stop reading anymore?

Sorry for the format, it's not working.

Thank you in advance,
Puckapao

Comments

  • edited November 2019

    Hey take a look at this testing code we use in Python: https://github.com/mbientlab/MetaWear-SDK-Cpp/blob/master/test/test_settings.py <-- there's a bunch of examples here on how to read the voltage and %.

    [CODE]

    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)
        self.libmetawear.mbl_mw_datasignal_read(voltage)
        self.libmetawear.mbl_mw_datasignal_read(charge)
    

    [/CODE]

  • Thank you for your guide but I still cannot get the data. I will try more

  • Please read our tutorials and API docs and try to get one thing working at a time and then add them together.

  • This example shows how to read the battery in C++ and translates fairly well to python
    https://github.com/mbientlab/MetaWear-SDK-Cpp/blob/d679878b8f7f88471697491f352469c391c7f27f/cppdocs/source/settings.rst#battery-state

    Here's my simple handler:

        def battery_handler(self, ctx, data):
            value = parse_value(data, n_elem=1)
            # convert ms to ns
            timestamp = data.contents.epoch * 1000000
            self.telegraf_client.metric(
                "battery", {"voltage": value.voltage, "charge": value.charge}, tags=self.tags, timestamp=timestamp)
    

    Unfortunately, mbl_mw_datasignal_read seems to perform a one-time read of the data signal. Is there any way to start periodic sampling of the battery, or a generic operation that periodically samples any signal? If there is, I can't find it, and I'd like the battery data to come through the fuser data processor along with my other data.

  • @abferm said:
    Unfortunately, mbl_mw_datasignal_read seems to perform a one-time read of the data signal. Is there any way to start periodic sampling of the battery, or a generic operation that periodically samples any signal? If there is, I can't find it, and I'd like the battery data to come through the fuser data processor along with my other data.

    Use the timer module.

    https://mbientlab.com/tutorials/WPython2.html#log-temperature-and-timer

  • @abferm said:
    This example shows how to read the battery in C++ and translates fairly well to python
    https://github.com/mbientlab/MetaWear-SDK-Cpp/blob/d679878b8f7f88471697491f352469c391c7f27f/cppdocs/source/settings.rst#battery-state

    Here's my simple handler:

        def battery_handler(self, ctx, data):
            value = parse_value(data, n_elem=1)
            # convert ms to ns
            timestamp = data.contents.epoch * 1000000
            self.telegraf_client.metric(
                "battery", {"voltage": value.voltage, "charge": value.charge}, tags=self.tags, timestamp=timestamp)
    

    Unfortunately, mbl_mw_datasignal_read seems to perform a one-time read of the data signal. Is there any way to start periodic sampling of the battery, or a generic operation that periodically samples any signal? If there is, I can't find it, and I'd like the battery data to come through the fuser data processor along with my other data.

    Thank you, btw how did you call this callback handler? I tried putting lines from Laura's answer but it doesn't even called once.

  • Yes, you can use a timer to periodically read the signal.
    Check out this example where you would read the battery signal instead of the temp signal (Everything else is the same): https://github.com/mbientlab/MetaWear-SDK-Python/blob/master/examples/log_temp.py

Sign In or Register to comment.