Set iBeacon using macro

edited February 2021 in Python

Hi everyone,
I tried to set MetaMotion R as a beacon using the macro. It is working perfectly but when the MetaMotion R battery die and I recharge it again the macro not working anymore and I need to set it as a beacon again. Is there any way to make MetaMotion R working as a beacon even after the power off and on?
MetaMotion R hardware revision is 0.4 and firmware revision is 1.5.0
My Python code below:

def macro(mac):
    d = connect_2_bt(mac)
    if d is not None:
        try:
            # Start recording the macro
            libmetawear.mbl_mw_macro_record(d.board, 1)
            # Set major number to 31415
            libmetawear.mbl_mw_ibeacon_set_major(d.board, 31415)

            # Set minor number to 9265
            libmetawear.mbl_mw_ibeacon_set_minor(d.board, 9265)

            # Enable ibeacon mode
            libmetawear.mbl_mw_ibeacon_enable(d.board)

        except RuntimeError as err:

            print('Unable to set Macro for ({}) \n Error message: {}'.format(mac, err))
        finally:
            d.disconnect()
# Bluetooth connection function
def connect_2_bt(mac):

    print("Searching for device ({})...".format(mac))

    try:
        device = MetaWear(mac)
        device.connect()
        print("Connected to ({})".format(device.address))
        return device

    except Exception as err:
        print('Unable to connect to the node MAC({}) \n Error message: {}'.format(mac, err))
        return None

Comments

  • Yes, the macro will get this done. If you aren't successful it is because you programmed the macro wrong. I can already tell this is the issue as your code is missing the end_macro command.

  • Thank you @Laura
    I will try to add "end macro" function and check

  • @Laura said:
    Yes, the macro will get this done. If you aren't successful it is because you programmed the macro wrong. I can already tell this is the issue as your code is missing the end_macro command.

    Hi @Laura,
    I end the macro but I still facing the same problem.
    please check my code below:

    def macro(mac):
        d = connect_2_bt(mac)
        if d is not None:
            try:
                e = Event()
    
                # Start recording the macro
                libmetawear.mbl_mw_macro_record(d.board, 4)
    
                # Set major number to 31415
                libmetawear.mbl_mw_ibeacon_set_major(d.board, 31415)
    
                # Set minor number to 9265
                libmetawear.mbl_mw_ibeacon_set_minor(d.board, 9265)
    
                # Enable ibeacon mode
                libmetawear.mbl_mw_ibeacon_enable(d.board)
    
                create_voidp_int(lambda fn: libmetawear.mbl_mw_macro_end_record(d.board, None, fn), event=e)
    
            except RuntimeError as err:
                print('Unable to set Macro for ({}) \n Error message: {}'.format(mac, err))
    
            finally:
                d.disconnect()
    
  • Take a look at the swift examples for this and make sure you are doing the same thing in python.
    Also if you read our tutorials, it says you have to disconnect and then just scan the metawear to make sure it worked.

  • Hi @Laura
    Unfortunately, I'm not a SWIFT developer and I didn't deal with it before, but I tried my best and updated my code according to the SWIFT example for beacons but it still the same issue.
    I hope you can help us to solve this problem.

    my new code:

    def macro(mac):
        d = connect_2_bt(mac)
        if d is not None:
            try:
                e = Event()
                # Start recording the macro
                libmetawear.mbl_mw_macro_record(d.board, 4)
                # Set major number to 31415
                libmetawear.mbl_mw_ibeacon_set_major(d.board, 31415)
    
                # Set minor number to 9265
                libmetawear.mbl_mw_ibeacon_set_minor(d.board, 9265)
    
                _uuid1 = '12345678abcd88ccabcd1111aaaa2222'
                _uuid =[int(i, 16) for i in _uuid1[0:15]]
    
                uuid = (ctypes.c_ubyte * 16)(* _uuid)
                libmetawear.mbl_mw_ibeacon_set_period(d.board, 1000)
                libmetawear.mbl_mw_ibeacon_set_rx_power(d.board, -55)
                libmetawear.mbl_mw_ibeacon_set_tx_power(d.board, -12)
                libmetawear.mbl_mw_ibeacon_set_uuid(d.board, uuid)
    
                # Enable ibeacon mode
                libmetawear.mbl_mw_ibeacon_enable(d.board)
    
                create_voidp_int(lambda fn: libmetawear.mbl_mw_macro_end_record(d.board, None, fn), event=e)
    
            except RuntimeError as err:
                #  If debug true print error message
                if cfg.config_data['DEBUG']:
                    print('Unable to set Macro for ({}) \n Error message: {}'.format(mac, err))
            finally:
                d.disconnect()
    
    
  • edited March 2021

    You need to make it a boot macro: libmetawear.mbl_mw_macro_record(d.board, 0) <-- must be 0

    void mbl_mw_macro_record(MblMwMetaWearBoard *board, uint8_t exec_on_boot) {
            auto state = GET_MACRO_STATE(board);
            state->commands.clear();
            state->is_recording = true;
            state->exec_on_boot = exec_on_boot == 0 ? 0 : 1;
    }
    
  • @Laura said:
    You need to make it a boot macro: libmetawear.mbl_mw_macro_record(d.board, 0) <-- must be 0

    void mbl_mw_macro_record(MblMwMetaWearBoard *board, uint8_t exec_on_boot) {
    auto state = GET_MACRO_STATE(board);
    state->commands.clear();
    state->is_recording = true;
    state->exec_on_boot = exec_on_boot == 0 ? 0 : 1;
    }

    Hi @Laura
    As mentioned in documentation (1) to run the macro on boot not (0) as you mentioned

    libmetawear.mbl_mw_macro_record(board, 1) # ON BOOT
    libmetawear.mbl_mw_macro_record(board, 0) # NOT ON BOOT
    

    you can check the documentation at this link
    https://github.com/mbientlab/MetaWear-SDK-Python/blob/master/docs/source/macro.rst

    anyway I was succeeded to set iBeacon to run on the boot
    this my working code in case anybody faces the same problem can use my code

    #!/usr/bin/python3
    # Macros
    import uuid
    from threading import Event
    from mbientlab.metawear.cbindings import *
    from mbientlab.metawear import MetaWear, libmetawear
    
    def macro(mac):
        d = connect_2_bt(mac)
        if d is not None:
            try:
                e = Event()
                # Start recording the macro
                libmetawear.mbl_mw_macro_record(d.board, 1)
    
                # Set minor number to 9265
                libmetawear.mbl_mw_ibeacon_set_minor(d.board, 9265)
                # Set major number to 31415
                libmetawear.mbl_mw_ibeacon_set_major(d.board, 31415)
                # Set sending period to 1 second
                libmetawear.mbl_mw_ibeacon_set_period(d.board, 1000)
                # Set receiving power to highest sensitivity (-96)
                libmetawear.mbl_mw_ibeacon_set_rx_power(d.board, -96)
                # Set transmitting power to 4 dBm
                # Note: power can be set between -20 (lower) to +4 (higher)
                libmetawear.mbl_mw_ibeacon_set_tx_power(d.board, 4)
                # create UUID
                bytes = uuid.UUID('{12345678-abcd-88cc-abcd-1111aaaa2222}').bytes[::-1]
                _uuid = cast(bytes, POINTER(c_ubyte * 16))
                # Set UUID
                libmetawear.mbl_mw_ibeacon_set_uuid(d.board, _uuid.contents)
                # Enable iBeacon mode
                libmetawear.mbl_mw_ibeacon_enable(d.board)
                # create callback function
                callback = FnVoid_VoidP_VoidP_Int(lambda ctx, board, status: e.set())
                # End macro recording
                libmetawear.mbl_mw_macro_end_record(d.board, None, callback)
                e.wait()
    
            except RuntimeError as err:
                #  If debug true print error message
                print('Unable to set iBeacon Macro for ({}) \n Error message: {}'.format(mac, err))
            finally:
                d.disconnect()
    
    
    
    def connect_2_bt(mac):
        print("Searching for device ({})...".format(mac))
        try:
            device = MetaWear(mac)
            device.connect()
            print("Connected to ({})".format(device.address))
            return device
        except Exception as err:
            print('Unable to connect to the node MAC({}) \n Error message: {}'.format(mac, err))
            return None
    
    
    
    if __name__ == '__main__':
        macro('e4:71:c8:04:69:fc')
    
    
  • Awesome, thanks for the update @qudor.
    Totally my bad for getting them mixed up. Thank you for pointing that out.

Sign In or Register to comment.