Problem upgrading to newest python API

edited December 2017 in General
Hi,

I have been using the API through the pymetawear package (version 0.8.0). I would like to upgrade my code to the official Python SDK, but I fail to run even the led.py example.

I created a new pip virtual environment, installed mbientlab version of pygattlib and metawear packages.
When trying to run the examples in the examples folder, e.g. python led.py MAC_ADDRESS, I get one of the two following errors:
-----------------------------------------
Traceback (most recent call last):
  File "led.py", line 10, in <module>
    device.connect()
  File "/home/aribes/Environments/metawear/local/lib/python2.7/site-packages/mbientlab/metawear/__init__.py", line 155, in connect
    self.gatt.connect(True, channel_type='random')
RuntimeError: Channel or attrib not ready
-------------------------------------------
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 315, in 'calling callback function'
  File "/home/aribes/Environments/metawear/local/lib/python2.7/site-packages/mbientlab/metawear/__init__.py", line 198, in _read_gatt_char
    raw = self.gatt.read_by_uuid(uuid)[0]
RuntimeError: Device is not responding!
Traceback (most recent call last):
  File "led.py", line 10, in <module>
    device.connect()
  File "/home/aribes/Environments/metawear/local/lib/python2.7/site-packages/mbientlab/metawear/__init__.py", line 189, in connect
    raise RuntimeError("Error initializing the API (%d)" % (self.init_status))
RuntimeError: Error initializing the API (16)
-------------------------------------------------------

But if I then switch to version 0.8.0 of pymetawear (which wraps Cpp API), it works well. 

Also I noted that pymetawear develop version (0.9.x) is a wrapper around official python wrapper. I am also not able to use it (obviously, if official python sdk does not work, that pymetawear version should not as well).

Comments

  • edited December 2017
    Which Linux distro are you running?  The latest Python SDK works fine on my Ubuntu 16.04 and openSUSE tumbleweed VMs
  • Ubuntu 16.04 and python 2.7
  • Are you running as superuser?
  • edited December 2017
    No, but that never has caused a problem, thanks to this post on SO https://unix.stackexchange.com/questions/96106/bluetooth-le-scan-as-non-root

    I run this command, so python gets capabilities to scan. That was enough for previous versions.

    sudo setcap 'cap_net_raw,cap_net_admin+eip' /home/aribes/Environments/metawear/bin/python

  • Hrm, well the only difference with older pymetawear versions and the current Python SDKs is the underlying Python ble library.  We use a custom fork of pygattlib, which you can find here


    Are these connection issues frequently occurring?  Have you tried resetting the BT adapter?
  • I am using your pygattlib fork, as recommended in the docs.

    I also realized that if I use pygatt==3.2.0, with the pymetawear==0.8.0, I get the following error:

    Traceback (most recent call last):
      File "_ctypes/callbacks.c", line 315, in 'calling callback function'
      File "/home/aribes/Projects/wear/sensor_manager/venv/local/lib/python2.7/site-packages/pymetawear-0.8.0-py2.7.egg/pymetawear/backends/__init__.py", line 154, in mbl_mw_write_gatt_char
        self.write_gatt_char_by_uuid(characteristic, command, length)
      File "/home/aribes/Projects/wear/sensor_manager/venv/local/lib/python2.7/site-packages/pymetawear-0.8.0-py2.7.egg/pymetawear/backends/pygatt.py", line 134, in write_gatt_char_by_uuid
        self.requester.char_write(str(self.get_uuid(characteristic)), data_to_send)
      File "/home/aribes/Projects/wear/sensor_manager/venv/local/lib/python2.7/site-packages/pygatt/device.py", line 92, in char_write
        return self.char_write_handle(self.get_handle(uuid), value,
      File "/home/aribes/Projects/wear/sensor_manager/venv/local/lib/python2.7/site-packages/pygatt/device.py", line 208, in get_handle
        raise exceptions.BLEError(message)
    pygatt.exceptions.BLEError: No characteristic found matching 326a9001-85cb-9195-d9dd-464cfbbae75a

    So I have to use version 3.1.0. 

    I have tried resetting the BT adapter, and rebooting the machine. These errors occur always when using the latest version of the python sdk, so right now I am stuck with pymetawear==0.8.0 which is the only that works.
  • Starting a new virtualenv to run some more tests in the Raspberry Pi 3, with python 2.7, I get the following errors:

    First one because of permissions:

    Traceback (most recent call last):
      File "multi_device.py", line 26, in <module>
        d.connect()
      File "/home/pi/Libraries/metawear/local/lib/python2.7/site-packages/mbientlab/metawear/__init__.py", line 155, in connect
        self.gatt.connect(True, channel_type='random')
    RuntimeError: Could not update HCI connection: Operation not permitted

    Then I use this command to solve it:
    sudo setcap 'cap_net_raw,cap_net_admin+eip' /home/pi/Libraries/metawear/bin/python

    Then if I run the following MetaWear-SDK-python example:

    $ python multi_device.py EC:44:5E:DD:E1:74

    I get one of the following two errors:

    Traceback (most recent call last):
      File "_ctypes/callbacks.c", line 315, in 'calling callback function'
      File "/home/pi/Libraries/metawear/local/lib/python2.7/site-packages/mbientlab/metawear/__init__.py", line 198, in _read_gatt_char
        raw = self.gatt.read_by_uuid(uuid)[0]
    RuntimeError: Device is not responding!
    Violación de segmento (means segment violation)

    Or this other one

    *** Error in `python': double free or corruption (fasttop): 0x75b02a68 ***
    Abortado

    My pip freeze command outputs the following (note that I installed ipython, so it has more stuff than minimum needed)

    backports.shutil-get-terminal-size==1.0.0
    certifi==2018.1.18
    chardet==3.0.4
    decorator==4.2.1
    enum34==1.1.6
    gattlib==0.20171002
    idna==2.6
    ipython==5.5.0
    ipython-genutils==0.2.0
    metawear==0.3.1
    pathlib2==2.3.0
    pexpect==4.3.1
    pickleshare==0.7.4
    prompt-toolkit==1.0.15
    ptyprocess==0.5.2
    Pygments==2.2.0
    requests==2.18.4
    scandir==1.6
    simplegeneric==0.8.1
    six==1.11.0
    traitlets==4.3.2
    urllib3==1.22
    wcwidth==0.1.7

    What else could I test? Can you provide me with some environment that you know works for you with raspberry pi 3 that I can test?
  • edited January 2018
    Can you try running your scripts outside of virtualenv?
    What bluez version are using?
  • Ok, I will try without virtualenv. I am not using bluez but pygatt. With bluez backend (pymetawear 0.8.0) I remember I was getting some error.
  • Bluez is the official Linux Bluetooth protocol stack.  All linux ble libraries use it.
  • apt-cache show bluez gives this output

    Package: bluez
    Version: 5.43-2+deb9u1
    Architecture: armhf
    Maintainer: Debian Bluetooth Maintainers <pkg-bluetooth-maintainers@lists.alioth.debian.org>
    Installed-Size: 3364
    Depends: libc6 (>= 2.15), libdbus-1-3 (>= 1.9.14), libglib2.0-0 (>= 2.31.8), libreadline7 (>= 6.0), libudev1 (>= 196), init-system-helpers (>= 1.18~), kmod, udev (>= 170-1), lsb-base, dbus
    Suggests: pulseaudio-module-bluetooth
    Conflicts: bluez-audio (<= 3.36-3), bluez-utils (<= 3.36-3)
    Breaks: udev (<< 170-1)
    Replaces: bluez-audio (<= 3.36-3), bluez-input, bluez-network, bluez-serial, bluez-utils (<= 3.36-3), udev (<< 170-1)

  • By the way, could you provide me with info of what versions of what packages are shipped in the SensorHub? Could I get a downloadable image of it? 
  • I'll check once I have access to our Pi.  

    MetaHub images are only provided to hub owners.
  • Ok, then even though I cannot get an image, could you provide me with the versions of the critical packages? I guess kernel and bluez versions should suffice, but maybe there are other packages that play a role into the problems I am experiencing.
  • kernel: Linux raspberrypi 4.9.59-v7+
    bluez: 5.43-2+deb9u1
    python: 2.7.13-2
    python3: 3.5.3-1
    nodejs: 8.9.3
This discussion has been closed.