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.
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.
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):
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.
@MichaelH
Hi Michael, I know it is a couple months ago, but did you sort out this issue?
I have exactly the same problem here, for me it is saying:
File "/usr/local/lib/python3.5/dist-packages/mbientlab/metawear/metawear.py", line 195, in connect
raise result[0]
mbientlab.warble.WarbleException: (status = 2)
every second time I run my script.
First run, connects fine and does what I want,
Second run, gives the above error
Third run works again.
and so on.
You could try to update some python packages including warble.