3. Python2

Python SDK for creating MetaWear apps on the Linux platform. This is a thin wrapper around the MetaWear C++ API so you will find the C++ documentation and API reference useful. Also, check out the scripts in the GitHub examples folder for full sample code.

3.1. Prerequsites

You should have completed the instructions in the previous section related to the Linux Installation.

We will be using the terminal so you should be comfortable with standard Linux commands.

First, let’s check that you have Python 2.7 installed. Open a terminal:

>>> which python
>>> which pip

If it’s not installed, these commands will not return anything.


You can use Python 3.4 if you would like to. This tutorial will not cover Python 2.7.

3.2. Installation

3.2.1. Install Python and Pip

Now, let’s start with a system update:

>>> sudo apt-get update
>>> sudo apt-get upgrade

This make take a while, be patient.


Now run the following command to install python 2.7 and pip:

>>> sudo apt-get install python2.7 python-pip

Once it’s done, you can check that everything looks good.


3.2.2. Upgrade Pip

It is essential that your pip be up to date. For good measure, run the commands below:

>>>  pip install pip --upgrade
>>>  pip install --upgrade pip setuptools

If anything goes wrong, just uninstall and re-install:

>>> sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall

3.2.3. Install Dependencies

The metawear libraries depend on warble, bluez, and cpp compilers, let’s install those first:

>>> sudo apt-get install build-essential
>>> sudo apt-get install bluez
>>> sudo apt-get install libboost-all-dev
>>> sudo apt-get install libbluetooth-dev

Make sure that you have a C++ compiler that support C++14. To check: >>> which g++ >>> which gcc >>> which make

Otherwise run something like sudo apt-get install -y gcc-6 g++-6 clang-3.8


3.2.4. Install Git

>>> sudo apt-get install git-core

Check that everything works:

>>> git --version

3.2.5. Install Warble

>>> git clone https://github.com/mbientlab/Warble.git
>>> git submodule update --init
>>> make
>>> sudo pip install warble

3.2.6. Install MetaWear

Use pip to install the metawear package. It depends on pygattlib so ensure your Linux environment first has the necessary dependencies installed.

>>> pip install metawear

you can also use the alternate command:

>>> python -m pip install --user metawear

you may need to use sudo depending on how you installed python:

>>> sudo pip install metawear

3.2.7. Download Examples

Head over to our Python Github page: https://github.com/mbientlab/MetaWear-SDK-Python


You can clone the repository or simply download as a ZIP file:

>>>  git clone https://github.com/mbientlab/MetaWear-SDK-Python.git

3.3. Run Script

3.3.1. Scan and Connect

This tutorial will take you through your first MetaWear Python App.


Make sure you have a MetaWear device with you, fully-charged, and that you are aware of that device’s MAC address.

We will ignore most of the items in the Github repository and instead we will focus on the examples folder:


Using your favorite editor, take a look at the scan_connect.py example.

This python code will scan for nearby Bluetooth devices and print out a list of available devices. You can then choose a device you want to connect to:

print("scanning for devices...")
devices = {}
def handler(result):
    devices[result.mac] = result.name



i = 0
for address, name in six.iteritems(devices):
    print("[%d] %s (%s)" % (i, address, name))
    i+= 1

msg = "Select your device (-1 to rescan): "
selection = int(raw_input(msg) if platform.python_version_tuple()[0] == '2' else input(msg))

Run the code:

>>>  # usage: python scan_connect.py
>>>  python scan_connect.py

Select the device you want to connect to. The MAC address for my favorite MetaSensor is D8:B5:11:21:96:06.

>>>  Select your device (-1 to rescan): 1

The code will then connect to this device for 5 seconds, retrieve device information, and disconnect.


As you can see, your Linux machine connected to the MetaSensor using Bluetooth and retrieved some information about the devices such as the firmware version and the manufacturer name.

Congratulations, you ran your first Python script!


Linux Bluetooth drivers are fast and work very well. As such, MetaSensors, which have a slow advertising rate, may not connect everytime or the first time. Simply try to re-connect (by running the Python script again or updating the code to automatically re-try a connection). You can always update the Advertising rate of the MetaSensor using our APIs.

3.4. Learn More

You can head over to the Windows Python tutorial for a breakdown of all of the examples in the Python folder.

As our Python API is based on the CPP library, we recommend that you have two documents open:

  1. The CPP documentation: https://mbientlab.com/cppdocs/latest
  2. The CPP API reference: https://mbientlab.com/documents/metawear/cpp/latest

3.4.1. Usage

Import the MetaWear class and libmetawear variable from the metawear module and everything from the cbindings module.

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

If you do not know the MAC address of your device, use pygattlib to scan for nearby devices.

from gattlib import DiscoveryService
service = DiscoveryService("hci0")
devices = service.discover(2)

# grab the first scanned device
address = devices.items()[0][0]

Once you have the device’s MAC address, create a MetaWear object with the MAC address and connect to the device.

device = MetaWear(address)
status = device.connect()

Upon a successful connection, you can begin calling any of the functions from the C++ SDK, for example, blinking the LED green.

pattern= LedPattern(repeat_count= Const.LED_REPEAT_INDEFINITELY)
libmetawear.mbl_mw_led_load_preset_pattern(byref(pattern), LedPreset.BLINK)
libmetawear.mbl_mw_led_write_pattern(device.board, byref(pattern), LedColor.GREEN)