Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring proximity with bluetooth on raspberry Pi

I've been trying to use this script https://github.com/karulis/pybluez/blob/master/examples/advanced/inquiry-with-rssi.py but it seems that sock = bluez.hci_open_dev(dev_id) returns a non-working socket. Every time sock is passed into a function error(9, 'Bad file descriptor') is thrown.

This script is pretty old so there is a decent chance it doesn't work any more. So I have two questions. Does anyone know how to use the pybluez library (or a more modern equivalent) to measure proximity of a bluetooth device with a raspberry pi?

And what am I doing wrong with this script that is causing me to build a broken socket?

Thanks.

like image 773
Daniel Nill Avatar asked Oct 02 '22 07:10

Daniel Nill


2 Answers

Try this:

Run hcitool dev to get the address of your bluetooth device.

In the script you linked to, change line 120 from:

dev_id = 0

to:

dev_id = bluez.hci_get_route(ADDRESS_FOR_YOUR_BLUETOOTH_DEVICE)

To measure proximity, the script calls the function

device_inquiry_with_with_rssi(sock)

which should print a list of bluetooth device ids and their corresponding RSSI values (see lines 95-102). Typically, devices must be in pairing mode to show up in the inquiry results. The function also returns the list of IDs/RSSIs as an array, so you can call it from your own code and process the returned results. The RSSI value indicates the signal strength of a device, and so is an indirect measure of proximity (see Finding distance from RSSI value of Bluetooth Low Energy enabled device ).

like image 103
imjosh Avatar answered Oct 07 '22 04:10

imjosh


Depending on the device you want to use, Bluepy in Python might be a better method. I used a Pi3 to measure RSSI from Bluetooth modules (HM-10, CC254x-based devices) and was able to get reasonable estimates of distance. There's a ton of noise in RSSI, so expect inaccuracies of no less than 1m with some signal processing. I wrote a blog post on RSSI from HM-10 and Rpi, check it out for a more in-depth method of how I do it. I even included some Python code:

https://engineersportal.com/blog/2017/12/31/using-raspberry-pi-hm-10-and-bluepy-to-develop-an-ibeacon-mesh-network-part-1

like image 39
Joshua Hrisko Avatar answered Oct 07 '22 06:10

Joshua Hrisko