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)

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)

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 error. Who is responsible for fixing that?)

>>> %Run
error 1598280591.852555: Error on line: 296 (src/ Operation now in progress
Services disconvered
Characteristics discovered
Descriptors found
>>> %Run
error 1598280604.313508: Error on line: 296 (src/ Operation now in progress
Traceback (most recent call last):
  File "/home/pi/Python/", line 7, in <module>
  File "/usr/local/lib/python3.5/dist-packages/mbientlab/metawear/", 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. does not mention it puts the sensor to sleep.

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


  • 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)

    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/", line 53 in _private_write_async
      File "/usr/local/lib/python3.5/dist-packages/mbientlab/warble/", line 71 in write_without_resp_async
      File "/usr/local/lib/python3.5/dist-packages/mbientlab/metawear/", line 229 in _write_char_async
      File "/usr/local/lib/python3.5/dist-packages/mbientlab/metawear/", line 224 in completed
      File "/usr/local/lib/python3.5/dist-packages/mbientlab/warble/", line 49 in completed
    Thread 0x76f19640 (most recent call first):
      File "/usr/lib/python3/dist-packages/thonny/", line 882 in _fetch_command
      File "/usr/lib/python3/dist-packages/thonny/", line 156 in mainloop
      File "/usr/lib/python3/dist-packages/thonny/", 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.