Using Python to Store Data from many BLE Devices

BLE (Bluetooth Low Energy) sensor devices like the Hexiwear are great, but they cannot store a large amount of data. For a research project I have to collect data from many BLE devices for later processing. What I’m using is a Python script running on the Raspberry Pi which collects the data and stores it on a file:

Raspberry Pi with Python controlling a set of Hexiwear BLE Devices

Raspberry Pi with Python controlling a set of Hexiwear BLE Devices

Getting Data from a Single Device

The following script gets sensor data from a single device:

 
# Using Hexiwear with Python
# Script to get the device data and append it to a file
# Usage
# python GetData.py <device>
# e.g. python GetData.py "00:29:40:08:00:01"
import pexpect
import time
import sys
import os

# ---------------------------------------------------------------------
# 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
# ---------------------------------------------------------------------

DEVICE = "00:29:40:08:00:01"   # device #24

if len(sys.argv) == 2:
  DEVICE = str(sys.argv[1])

# Run gatttool interactively.
child = pexpect.spawn("gatttool -I")

# Connect to the device.
print("Connecting to:"),
print(DEVICE)

NOF_REMAINING_RETRY = 3

while True:
  try:
    child.sendline("connect {0}".format(DEVICE))
    child.expect("Connection successful", timeout=5)
  except pexpect.TIMEOUT:
    NOF_REMAINING_RETRY = NOF_REMAINING_RETRY-1
    if (NOF_REMAINING_RETRY>0):
      print "timeout, retry..."
      continue
    else:
      print "timeout, giving up."
      break
  else:
    print("Connected!")
    break

if NOF_REMAINING_RETRY>0:
  unixTime = int(time.time())
  unixTime += 60*60 # GMT+1
  unixTime += 60*60 # added daylight saving time of one hour
  
  # open file
  file = open("data.csv", "a")
  if (os.path.getsize("data.csv")==0):
    file.write("Device\ttime\tAppMode\tBattery\tAmbient\tTemperature\tHumidity\tPressure\tHeartRate\tSteps\tCalorie\tAccX\tAccY\tAccZ\tGyroX\tGyroY\tGyroZ\tMagX\tMagY\tMagZ\n")
  
  file.write(DEVICE)
  file.write("\t")
  file.write(str(unixTime)) # Unix timestamp in seconds 
  file.write("\t")

  # App mode
  child.sendline("char-read-hnd 0x6d")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("AppMode:  "),
  print(child.before),
  print(str(int(child.before[0:2],16)))

  file.write(str(int(child.before[0:2],16)))
  file.write("\t")
  
  # Battery
  child.sendline("char-read-hnd 0x28")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("Battery:  "),
  print(child.before),
  print(str(int(child.before[0:2],16)))

  file.write(str(int(child.before[0:2],16)))
  file.write("\t")
  
  # Ambient Light (0x2011)
  child.sendline("char-read-hnd 0x3f")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("Ambient:  "),
  print(child.before),
  print(str(int(child.before[0:2],16)))

  file.write(str(int(child.before[0:2],16)))
  file.write("\t")

  # Temperature (0x2012)
  child.sendline("char-read-hnd 0x43")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("Temperature:  "),
  print(child.before),
  print(float(hexStrToInt(child.before[0:5]))/100)

  file.write(str(float(hexStrToInt(child.before[0:5]))/100))
  file.write("\t")
  
  # Humidity (0x2013)
  child.sendline("char-read-hnd 0x47")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("Humidity:  "),
  print(child.before),
  print(float(hexStrToInt(child.before[0:5]))/100)

  file.write(str(float(hexStrToInt(child.before[0:5]))/100))
  file.write("\t")

  # Pressure (0x2014)
  child.sendline("char-read-hnd 0x4b")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("Pressure:  "),
  print(child.before),
  print(float(hexStrToInt(child.before[0:5]))/100)

  file.write(str(float(hexStrToInt(child.before[0:5]))/100))
  file.write("\t")

  # HeartRate (0x2021)
  child.sendline("char-read-hnd 0x52")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("HeartRate:  "),
  print(child.before),
  print(str(int(child.before[0:2],16)))

  file.write(str(int(child.before[0:2],16)))
  file.write("\t")

  # Steps (0x2022)
  child.sendline("char-read-hnd 0x56")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("Steps:  "),
  print(child.before),
  print(hexStrToInt(child.before[0:5]))

  file.write(str(hexStrToInt(child.before[0:5])))
  file.write("\t")

  # Calorie (0x2023)
  child.sendline("char-read-hnd 0x5a")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("Calorie:  "),
  print(child.before),
  print(hexStrToInt(child.before[0:5]))

  file.write(str(hexStrToInt(child.before[0:5])))
  file.write("\t")

  # Accelerometer
  child.sendline("char-read-hnd 0x30")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  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)
  
  file.write(str(float(hexStrToInt(child.before[0:5]))/100))
  file.write("\t")
  file.write(str(float(hexStrToInt(child.before[6:11]))/100))
  file.write("\t")
  file.write(str(float(hexStrToInt(child.before[12:17]))/100))
  file.write("\t")
  
  # Gyro
  child.sendline("char-read-hnd 0x34")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  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)
  file.write(str(float(hexStrToInt(child.before[0:5]))/100))
  file.write("\t")
  file.write(str(float(hexStrToInt(child.before[6:11]))/100))
  file.write("\t")
  file.write(str(float(hexStrToInt(child.before[12:17]))/100))
  file.write("\t")
  
  # Magnetometer
  child.sendline("char-read-hnd 0x38")
  child.expect("Characteristic value/descriptor: ", timeout=5)
  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]))

  file.write(str(float(hexStrToInt(child.before[0:5]))/100))
  file.write("\t")
  file.write(str(float(hexStrToInt(child.before[6:11]))/100))
  file.write("\t")
  file.write(str(float(hexStrToInt(child.before[12:17]))/100))
  file.write("\t")

  file.write("\n")
  file.close()
  print("done!")
  
  sys.exit(0)
else:
  print("FAILED!")
  sys.exit(-1)

Calling it with the device ID it gets the sensor node data, appends it to the file and shows it on the console:

pi@raspberrypi:~ $ python GetData.py "00:2A:40:0B:00:4E"
Connecting to: 00:2A:40:0B:00:4E
Connected!
AppMode: 02 2
Battery: 64 100
Ambient: 0b 11
Accel: 05 00 02 00 9b ff 0.05 0.02 -1.01
...
done!

The data gets stored into a CSV text file.

Getting data from Multiple Devices

The following script file:

# Python script to get data from multiple devices
# Usage:
# python GetDataAll.py

import subprocess

# list of devices
devices = [
  "00:32:40:08:00:12",  # device 01
  "00:2F:17:03:00:35",  # device 20
  "00:28:22:0C:00:15",  # device 21
  "00:15:77:03:10:01",  # device 22
  "00:2A:40:0B:00:4E",  # device 23
  "00:29:40:08:00:01",  # device 24
   ]

for x in range(0,len(devices)):
  cmd = "python GetData.py " + devices[x]
  subprocess.call(cmd, shell=True)

print("finished all devices!")

The script has a list of BLE devices which are used to poll the data.

Summary

With a Raspberry Pi and Python scripting, I can collect data from multiple BLE devices and store the information into a file for further processing. Because I don’t need to keep the BLE connection active, I can extend this to almost unlimited number of devices, without running into the typical BLE connection limit of around 8 devices.
I’m using a similar way to update all the devices (e.g. with the current time/date), plus I have implemented a remote shell in each BLE device: that way I can send commands to each node which then are executed on the BLE device.

The Python scripts used can be found on GitHub.

Happy Saving 🙂

Links

7 thoughts on “Using Python to Store Data from many BLE Devices

  1. Hello, Erich!
    Very nice post!
    I have recently built a robot based on a Raspberry Pi and a FRDM-KL25z. The FRDM is the one who controls the motors and reads the sensors, and the RPI does the networking and transmits the commands to the FRDM with a serial connection through a USB cable.
    The robot can be controlled from the internet and does a live streaming of the RPI Camera.

    I just love the versatility of this development board and i think it can be a very good partener with the NXP microcontroller.

    Alex

    Like

  2. Nice work. Is it possible to use your example to work with other BLE device? I tried it already with other BLE device, for example, Arduino 101, it failed. Thank u

    Like

    • Yes, that approach works for any BLE device. But of course you need to change the attribute numbers to match your device. Additionally you need to pair to your device based on what they need. See the links at the end of this article which explain each of these steps.

      Like

      • Whenever I pair a BLE with RPi3, RPi3 show No Service Available for this device. I also, tried Python Blue but no luck. Is gattool preinstalled library?

        Like

        • No, all these BLE tools are not installed by default. See the related articles in the links section of this post how to install and use them.
          I hope this helps,
          Erich

          Like

  3. Erich: Great work as always! (Do you actually sleep?) I’m grinding through your previous blog entries on BLE, and have arrived at a few questions:

    * From the photo, it looks like you *may* be using a BLE USB Dongle — is that true? Or are you using the built-in Bluetooth radio?
    * I’ve heard that built-in BLE support has improved in recent versions of Raspbian (+ Jessie & Bluez). Does that significantly change your recipe (i.e. is there an updated blog entry in the making :-)?
    * Have you had any experience with the Pi Zero W and BLE?

    Like

    • 1) No, I’m using a USB mouse/keyboard dongle. I’m using the built-in BLE of that Raspy.
      2) I have if there is any other improvement with tha latest-latest version of the Linux distribution for the Raspy. But I assume the steps remain the same. Did not look at that recently.
      3) I have them on my desk, but because a some time I need to sleep, I had no time to look at it 😉

      Like

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.