Hi guys,
I´m trying something very simple but somehow I´m a little stuck here.
What I´m trying:
The Internal LED of the ESP 32 should light up, when my Smartwatch is getting close to the ESP ( I thinks its measuring the signal strength, so not really the proximity)
Sidestory
Long story short, I got the idea from a youtube video and tried several codes, but they are either quite complex, or they make the LED light up, if ANY bluetooth device is detected near by.
I have the BLE "MAC Address" of my smartwatch and this address, is also visible to the ESP, also also within a App for Android ( nRF Connect)
Issue
I changed the code a bit, to refer to PIN 2, where the internal LED is assigned to, but the LED won´t light up. ( I also tried so invert the HIGH and LOW statement) but also then no change on the LED, maybe someone has a solution or may even a working code, where I just have to change the BLE "MAC address". But of course I´m keen to learn why its not working in this case.
Please note that I´m a beginner, so might be something obvious that is not working and I´m not seeing
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#define LED 2
String knownBLEAddresses[] = {"34:b2:0a:93:f4:02"};
int RSSI_THRESHOLD = -55;
bool device_found;
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
for (int i = 0; i < (sizeof(knownBLEAddresses) / sizeof(knownBLEAddresses[0])); i++)
{
//Uncomment to Enable Debug Information
//Serial.println("*************Start**************");
//Serial.println(sizeof(knownBLEAddresses));
//Serial.println(sizeof(knownBLEAddresses[0]));
//Serial.println(sizeof(knownBLEAddresses)/sizeof(knownBLEAddresses[0]));
//Serial.println(advertisedDevice.getAddress().toString().c_str());
//Serial.println(knownBLEAddresses[i].c_str());
//Serial.println("*************End**************");
if (strcmp(advertisedDevice.getAddress().toString().c_str(), knownBLEAddresses[i].c_str()) == 0)
{
device_found = true;
break;
}
else
device_found = false;
}
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup() {
Serial.begin(115200); //Enable UART on ESP32
Serial.println("Scanning..."); // Print Scanning
pinMode(LED,OUTPUT); //make BUILTIN_LED pin as output
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); //Init Callback Function
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100); // set Scan interval
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
for (int i = 0; i < foundDevices.getCount(); i++)
{
BLEAdvertisedDevice device = foundDevices.getDevice(i);
int rssi = device.getRSSI();
Serial.print("RSSI: ");
Serial.println(rssi);
if (rssi > RSSI_THRESHOLD && device_found == true)
digitalWrite(LED,HIGH);
else
digitalWrite(LED,LOW);
}
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
}
Looking forward for you help
PS: Code compiles properly and show no errors.