Now I can use the data on the Hexiwear over BLE with the gatttool (see “Tutorial: Hexiwear Bluetooth Low Energy Packet Sniffing with Wireshark” and “Tutorial: BLE Pairing the Raspberry Pi 3 Model B with Hexiwear“). This article is taking things a step further and uses a Python script on Linux to access the sensor data on the BLE device:
Outline
This article is about accessing the Mikroelektronika Bluetooth Low Energy Hexiwear (http://www.mikroe.com/hexiwear/) device with Python scripting from a Raspberry Pi. That way it is possible to send and receive data over BLE and do to whatever I want. Precondition is to have a working BLE connection and pairing with the Hexiwear using Bluez (see “Tutorial: BLE Pairing the Raspberry Pi 3 Model B with Hexiwear“). Python is a powerful scripting language and can be used for all kind of automation.
Installation
I’m using Python with the ‘pexpect’ package. First, make sure that latest packages are used:
sudo apt-get update
Next, make sure the PIP (Python Package Index) is installed:
sudo apt-get install git build-essential python-dev python-pip
Install the pexpect, a package to control other applications from Python:
sudo pip install pexpect
I’m using here the 4.2 version of the pexpect package.
Pexpect
With ‘pexpect‘ I can spawn a process from Python and then control it like I would type in the commands manually. With pexpect I can run the gatttool as I would type the commands on a console/terminal.
Here are the basic blocks to access the data on the Hexiwear with it:
The following Python variable is used to store the address of the BLE device used:
DEVICE = "00:32:40:08:00:12"
I run the gatttool with the -I (interactive) option:
child = pexpect.spawn("gatttool -I")
This returns the child process handle I can use. With
child.sendline("connect {0}".format(DEVICE))
I send the string to the gatttool (spawned process). The format member function is used to build the formatted string: “connect 00:32:40:08:00:12” as I would have it typed in.
Next I need to wait for the connection. For this I use
child.expect("Connection successful", timeout=5)
Which waits for the “Connection successful” string from the gatttool. I have specified that it should timeout after 5 seconds.
To read a BLE characteristics, I use
child.sendline("char-read-hnd 0x30")
The handle 0x30 is for reading the accelerometer values. The gatttool would print something like this:
Characteristic value/descriptor: 02 00 00 00 a1 ff
I’m waiting for the first part of the output:
child.expect("Characteristic value/descriptor: ", timeout=10)
and then for the end of line:
child.expect("\r\n", timeout=10)
with child.before I get the string just before the line end. E.g.
child.before[0:5]
Will return “02 00” as substring (first 4 characters) from “02 00 00 00 a1 ff” string. That way I get substrings of x, y and z accelerometer values.
To transform the hex string (in little endian) into a signed 16bit number, I use the following Python sub-function:
# function to transform hex string like "0a cd" into signed integer def hexStrToInt(hexstr): val = int(hexstr[0:2],16) + (int(hexstr[3:5],16)<<8) if ((val&0x8000)==0x8000): # treat signed 16bits val = -((val^0xffff)+1) return val
The Hexiwear accelerometer values are ‘centi-float’ values, for example the numerical value 123 would be 1.23. With this, I can print the x, y and z values:
print(float(hexStrToInt(child.before[0:5]))/100), print(float(hexStrToInt(child.before[6:11]))/100), print(float(hexStrToInt(child.before[12:17]))/100)
Python Code to read Accelerometer, Gyro and Magnetometer
Here is the full source code of the code discussed above:
# Using Hexiwear with Python import pexpect import time DEVICE = "00:32:40:08:00:12" print("Hexiwear address:"), print(DEVICE) # Run gatttool interactively. print("Run gatttool...") child = pexpect.spawn("gatttool -I") # Connect to the device. print("Connecting to "), print(DEVICE), child.sendline("connect {0}".format(DEVICE)) child.expect("Connection successful", timeout=5) print(" Connected!") # function to transform hex string like "0a cd" into signed integer def hexStrToInt(hexstr): val = int(hexstr[0:2],16) + (int(hexstr[3:5],16)<<8) if ((val&0x8000)==0x8000): # treat signed 16bits val = -((val^0xffff)+1) return val #while True: # Accelerometer child.sendline("char-read-hnd 0x30") child.expect("Characteristic value/descriptor: ", timeout=10) child.expect("\r\n", timeout=10) print("Accel: "), print(child.before), print(float(hexStrToInt(child.before[0:5]))/100), print(float(hexStrToInt(child.before[6:11]))/100), print(float(hexStrToInt(child.before[12:17]))/100) # Accelerometer child.sendline("char-read-hnd 0x34") child.expect("Characteristic value/descriptor: ", timeout=10) child.expect("\r\n", timeout=10) print("Gyro: "), print(child.before), print(float(hexStrToInt(child.before[0:5]))/100), print(float(hexStrToInt(child.before[6:11]))/100), print(float(hexStrToInt(child.before[12:17]))/100) # Magnetometer child.sendline("char-read-hnd 0x38") child.expect("Characteristic value/descriptor: ", timeout=10) child.expect("\r\n", timeout=10) print("Magneto:"), print(child.before), print(hexStrToInt(child.before[0:5])), print(hexStrToInt(child.before[6:11])), print(hexStrToInt(child.before[12:17]))
Save the script to a file (e.g. gatttool.py) and run it with
python gatttool.py
This produces something like this:
Hexiwear address: 00:32:40:08:00:12 Run gatttool... Connecting to 00:32:40:08:00:12 Connected! Accel: 03 00 ff ff a1 ff 0.03 -0.01 -0.95 Gyro: 00 00 02 00 00 00 0.0 0.02 0.0 Magneto: b8 fc e2 04 c8 28 -840 1250 10440
Instead of printing the values, I can store them to a file or whatever I would like to do.
Python Script to update Date/Time
Below is a Python script similar to the one above which sets the Hexiwear current date and time using the Unix time (seconds after 1970) from the Raspberry Pi:
# Python script to set the time on the Hexiwear import pexpect import time from time import gmtime, strftime print("---------------------") print("Setting linux time") print("local time: "), print(time.ctime()) unixTime = int(time.time()) print("secs since 1970: "), print(int(unixTime)) print("---------------------") DEVICE = "00:32:40:08:00:12" # Run gatttool interactively. print("Running gatttool...") child = pexpect.spawn("gatttool -I") # Connect to the device. print("Connecting to"), print(DEVICE), child.sendline("connect {0}".format(DEVICE)) child.expect("Connection successful", timeout=5) print("Connected!") # Write local time command = "char-write-req 61 0304{0:02x}{1:02x}{2:02x}{3:02x}0000000000000000000000000000".format(unixTime&0xff, (unixTime>>8)&0xff, (unixTime>>16)&0xff, (unixTime>>24)&0xff) print(command) child.sendline(command) child.expect("Characteristic value was written successfully", timeout=10) print("done!")
And this is how it looks on the terminal:
Summary
Python is great for scripting things. With the pexpect Python class I can spawn a process and then send and receive strings. I’m using this in my article to read sensor values from a BLE device connected to the Raspberry Pi.
Happy Pythoning 🙂
Links
- Mikroelektronika Hexiwear: http://www.mikroe.com/hexiwear/
- Wiresharking BLE packets: Tutorial: Hexiwear Bluetooth Low Energy Packet Sniffing with Wireshark
- Pairing BLE devices with Bluez and gatttool: Tutorial: BLE Pairing the Raspberry Pi 3 Model B with Hexiwear
- Getting started with Python on Raspberry Pi: https://www.raspberrypi.org/documentation/usage/python/
- pexpect package: https://pexpect.readthedocs.io/en/stable/
Hi Erich,
I am in trouble with my BLE device, It is a weighing scale device. I could not get the measurement value after a long time trying. It is because the CCCD value is not changing when I write it using both “char-write-cmd” and “char-write-req”. When char-write-req is used, it shows this error “characteristic write request failed: Internal application error:I/P” but I could write the date and time using the both write commands. Am I missing something? please help!
LikeLike
Can you check with a BLE packet sniffer what you are sending? See https://mcuoneclipse.com/2016/12/25/tutorial-hexiwear-bluetooth-low-energy-packet-sniffing-with-wireshark/. This would give you an idea what you are sending to the device.
LikeLike
Hi Erich,
Finally I got the measurement value using bluetoothctl commands. I updated the bluez version from 5.23==>5.39. Then I use the bluetoothctl to connect device.Then I use the “select-attribute” to select the attribute, finally I typed “notify on”. Here it comes the measurement values. I don’t why it won’t work in other bluez versions. Now I need to convert the little endian data to decimal using python!
LikeLike
ah, I did not realize that you were on an older bluez version. Indeed, that can have a big impact, as bluez has made lots of changes on the BLE side.
LikeLike
Erich I am stuck with how I convert the values from hex to float. I got “02 24 09 e1 07 02 10 09 07 08”. 24 09 represents the measrement value others are flags and time stamp. This is obtained from a BLE weighing scale. The float value corresponding to the received value is “11.7” which is in Kilograms. Measurement is in little endian. Please help me to convert this on python
LikeLike
Hi Erich,
I have converted the value to float. That was awesome. Now could you please tell how to print “not connected” if the raspberry pi can’t connect to the BLE device. It is very difficult to me because I don’t know python very much.
LikeLike
Great!
You don’t have to use python to connect or bind: get it working with the gatttool first.
LikeLike
Yes I did that! I wanted to automate this process that is why I am asking help from you to do it python.
LikeLike
Ah, sorry, I missed that. I cannot be much of help, but have a look at my example, and there are plenty of tutorials and examples with Python on the web. Simply try things out.
good luck!
LikeLike
Pingback: Using Python to Store Data from many BLE Devices | MCU on Eclipse
hello @Erich Styger can you please explain to me from when you got the char for acc equal to 0x30 and gyro 0x34 , because i check the document it only showed the uuid , and do u have any idea how to send the uuid and read the value in python
LikeLike
Have a read at https://mcuoneclipse.com/2016/12/19/tutorial-ble-pairing-the-raspberry-pi-3-model-b-with-hexiwear/ (as well listed in the links section of this article) which explains how to read the BLE characteristics data information.
You don’t need the UUID, all what you need is the ID of the characteristics.
I hope this helps,
Erich
LikeLike
yeah thanks for your help , i have one more question do you know how to get the ble rssi after i connect to the hexiwear
LikeLike