Getting RSSI Value from client which connected to ESP32 BLE Server
Posted: Fri Mar 08, 2024 12:11 pm
Hello everyone,
I have a project called tracking robot. In this project, I want to use ESP32 as a BLE server and connect my phone to ESP32. In this link, I want to take the RSSI values and convert them to distance. According to these distance values, I plan to control my DC Motor via the pins connected to my motor driver card. I wrote the following code without using BLE Server, but I can't translate it into a BLE server application. Can you help me with this?
I have a project called tracking robot. In this project, I want to use ESP32 as a BLE server and connect my phone to ESP32. In this link, I want to take the RSSI values and convert them to distance. According to these distance values, I plan to control my DC Motor via the pins connected to my motor driver card. I wrote the following code without using BLE Server, but I can't translate it into a BLE server application. Can you help me with this?
Code: Select all
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
BLEScan* pBLEScan;
BLEAddress DeviceAddress("60:d0:39:cc:8d:5a");
bool deviceFound = false;
float filteredRSSI = -50;
float alpha = 0.25;
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if (advertisedDevice.getAddress() == DeviceAddress) {
int rssi = advertisedDevice.getRSSI();
filteredRSSI = alpha * rssi + (1.0 - alpha) * filteredRSSI;
float distance = pow(10, ((-64- filteredRSSI) / 27.0));
Serial.print("Filtered RSSI Value: ");
Serial.print(filteredRSSI);
Serial.print("\tReal RSSI Value: ");
Serial.println(rssi);
Serial.print("Distance: ");
Serial.println(distance);
deviceFound = true;
}
}
};
void setup() {
Serial.begin(921600);
// Initialize BLE
BLEDevice::init("ESP32_BLE_Client");
// Start BLE scan
pBLEScan = BLEDevice::getScan(); // Get BLEScan object
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(100); // Set scan interval to 100ms
pBLEScan->setWindow(99); // Set scan window to 99ms
pBLEScan->setActiveScan(true); // Use active scanning
Serial.println("Scanning started...");
}
void loop() {
// You can perform other tasks during scanning
pBLEScan->start(1, false); // Scan for 1 second
if (!deviceFound)
{
Serial.println("Searching for BLE device...");
}
else
{
deviceFound = false; // Reset for the next scan
}
}