Code only runs once

Based on the tutorials I cobbled together my first 'own' Python script such as this:

from mbientlab.metawear import MetaWear, libmetawear
from mbientlab.metawear.cbindings import *
import sys

address = "c1:df:15:9e:4d:7b"
device = MetaWear(address)
device.connect()

pattern= LedPattern(repeat_count= 10)
libmetawear.mbl_mw_led_load_preset_pattern(byref(pattern), LedPreset.BLINK)
libmetawear.mbl_mw_led_write_pattern(device.board, byref(pattern), LedColor.GREEN)
libmetawear.mbl_mw_led_play(device.board)
device.disconnect()

This runs and flashes the LED 10 times on the MMR as desired, but when I run the code again there is an unintelligible error that feels like it is trying to communicate that it cannot connect:
(I already know to ignore the blestatemachine.cc error. Who is responsible for fixing that?)

>>> %Run test.py
error 1598280591.852555: Error on line: 296 (src/blestatemachine.cc): Operation now in progress
Connected
Services disconvered
Characteristics discovered
Descriptors found
>>> %Run test.py
error 1598280604.313508: Error on line: 296 (src/blestatemachine.cc): Operation now in progress
Traceback (most recent call last):
  File "/home/pi/Python/test.py", line 7, in <module>
    device.connect()
  File "/usr/local/lib/python3.5/dist-packages/mbientlab/metawear/metawear.py", line 195, in connect
    raise result[0]
mbientlab.warble.WarbleException:  (status = 2)

Funny enough when I push the button after the first run then the code runs again ok, or after waiting several minutes.
1) Is device.disconnect() not enough to cleanly reset it to a stage where it can accept a new connection request?
2) What exactly does the button do? So far I thought it is only needed to wake the sensor up from sleep. https://mbientlab.com/tutorials/CppDevelopment.html#disconnect does not mention it puts the sensor to sleep.

Thanks for any insights that help me better understand the interaction with the sensor.

Comments

  • 1) No, please see our tutorial on proper resets/disconnects to ensure no issues with the BLE link.
    2) The button doesn't do anything. It is fully programmable to do what you want it to do.

  • The button is not used to put the sensor to sleep, there is an API call for that.

  • @Laura said:
    1) No, please see our tutorial on proper resets/disconnects to ensure no issues with the BLE link.

    The tutorial basically shows

    device = MetaWear(address)
    device.disconnect()

    so there's not much one can do wrong. If you are saying this is sufficient then why can my code not be run twice in a row? And why does pushing the button (that I have not modified in any way) make a difference?

  • Check out the reset section.

  • @Laura said:
    Check out the reset section.

    That section starts with an 'if', the documentation does not say 'run this every time to ensure a clean shutdown'. In any case, why would the board be "stuck in a bad state or need to be reset" after running my simple script?

    Just to try it I ran the entire reset code and it does not help, instead I get this (Python 3.5.3):

    Backend terminated (returncode: -11)
    Fatal Python error: Segmentation fault
    
    Current thread 0x75769470 (most recent call first):
      File "/usr/local/lib/python3.5/dist-packages/mbientlab/warble/gattchar.py", line 53 in _private_write_async
      File "/usr/local/lib/python3.5/dist-packages/mbientlab/warble/gattchar.py", line 71 in write_without_resp_async
      File "/usr/local/lib/python3.5/dist-packages/mbientlab/metawear/metawear.py", line 229 in _write_char_async
      File "/usr/local/lib/python3.5/dist-packages/mbientlab/metawear/metawear.py", line 224 in completed
      File "/usr/local/lib/python3.5/dist-packages/mbientlab/warble/gattchar.py", line 49 in completed
    
    Thread 0x76f19640 (most recent call first):
      File "/usr/lib/python3/dist-packages/thonny/backend.py", line 882 in _fetch_command
      File "/usr/lib/python3/dist-packages/thonny/backend.py", line 156 in mainloop
      File "/usr/lib/python3/dist-packages/thonny/backend_launcher.py", line 70 in <module>
    Use 'Stop/Restart' to restart the backend ...
    
  • edited August 31

    If you get a segmentation fault, it means the API calls are not being used correctly. It is a code error.
    For example you are sending an INT to a callback that expects an POINTER type.

Sign In or Register to comment.