When I started working at this Application (about 1 month ago) there was not too much documentation if any at all, in regards to meassuring distance via Bluetooth Classic (BT) for the Esp32.
There are, however, some tutorials and there is some documentation for Bluetooth Low Energy (BLE) which you may find by doing a simple Google Search using the apropriate keywords.
With all of that being said I am sharing here the progress I've made to try and make this task a bit more easy on those who wanted to use this technology and to improve it and play with it.
The reason I went with using Bluetooth Classic over Bluetooth Low Energy is that of range, BT Classic having a much bigger range at which it can operate at, unlike BLE (at least that's what I've read).
Both Bluetooth Modes have their ups and downs so it's up to you what's the best option, depending on your project terms, the range of operability and the battery consumption aspects, etc.
What does this code do, more exacly?
-It allows anyone with an Esp32 at their disposal to develop aplications that can trigger events based on the distance between the Esp32 module and the other device it is connected to *(in my case I used an Android Smartphone (Nexus 5x)). *
*The RSSI meassurement is incredibily decent, with high values that stop at 0 when you're near the Esp32 and low values that go down to -50 aprox. when you move at arpoximative 15-25 meters away. *
I tested the algorythm indoors (through walls) and in open air and it's pretty stable (meaning the signal will not tend to drop or increase too much when stationary or walking towards or away from the Esp32)
-The code comes already equiped with an applied Kalman noise filter, which can be calibrated even further
-It can trigger a certain PIN on your Esp32 devices (or more) at whatever signal strength or distance range you want to
I've heard alot of people saying that you can't really use the Bluetooth for Distance meassuring but I can tell you one thing about this, from my tests:
Yes, maybe it can't tell exacly at what distance you are standing from the Esp32, but it can sure tell if you're far or close, if you come close or go further away and the big signal fluctuations are pretty rare, at least in my case.
A Serial Plot Monitor Screenshot with the code in action:
There are a few things that are currently functional but not properly calibrated:
-Some variables of the distance meassuring algorythm (this is the most uncalibrated one)
What does it mean?
It means that if you are 1 meter away from the device it will indeed show that you're that far away, but the problem is that it ain't yet calibrated to add enough distance when you move away and the signal strength drops.
-The Kalman Filter works pretty good as it is right now, it really smoothens the RSSI and it makes it a bit more stable but it could be calibrated even further if you need to
Note: Until someone properly calibrates the Distance algorythm, I advise you to use the RSSI values as the trigger for any proximty application you may want to create, trust me on this one.
Before using the code, make sure to install all the Required Arduino Dependencies:
The Arduino Code: 0bin - encrypted pastebin
(paste this into a new sketch and it will be ready to run!)
Note: I may update this post and the code in order to make it more readable whenever I have some time.
If you do make any improvements or find smart ways to improve the code and its aplications, share it with the community if you wish, that'd be awesome.
This project wouldn't have been possible without all the help I've received from some very special people: from the Arduino and Electronicity Discord Channel Communities (thank you guys for all the help if you read this!) and with the help of some very special people from this community here: @SteveJG @Taifun @ABG @TIMAI2 and @Salman_Dev (you guys are awesome and have alot of patience!)
Much love, my dears!