ESP32 Bluetooth Proximity Sensor

Hi There

I am trying to make a Bluetooth proximity sensor that turns on a GPIO pin on my ESP32 dev board when a bluetooth device is in range of my set cutoff, and if that device's name matches my specified name

I had a look at this youtube video and am trying to adapt the code to work for my needs, but I'm not having much luck

Code:

#include <BLEAdvertisedDevice.h>
#include <BLEDevice.h>
#include <BLEScan.h>

const int PIN = 2;
const int CUTOFF = -60;
String setName = "Some Device Name";
bool isOn = false;

void setup() {
  pinMode(PIN, OUTPUT);
  BLEDevice::init("");
}

void loop() {
  BLEScan *scan = BLEDevice::getScan();
  scan->setActiveScan(true);
  BLEScanResults results = scan->start(1);
  String deviceName = "";
  int rssi = 0;
  for (int i = 0; i < results.getCount(); i++) {
    BLEAdvertisedDevice device = results.getDevice(i);
    deviceName = device.getName().c_str();
    rssi = device.getRSSI();
    if(deviceName == setName && rssi > CUTOFF)
    {
      isOn = true;
    }
    else
    {
      isOn = false;
    }
  }
  if(isOn == true)
  {
    digitalWrite(PIN, HIGH);
  }
  else
  {
    digitalWrite(PIN, LOW);
  }
  delay(100);
}

I would add Serial output to show what device names are detected. Perhaps you misspelled the name.

That seems to be a problem as well, if I try to print the name, nothing comes up in the serial monitor, I've checked for correct baud rates and everything, but no serial output

Even with a simple sketch like this?

vois setup()
{
  Serial.begin(115200);
  delay(2000);
  while (!Serial) {}
  Serial.println("Succefully sent Serial output.");
}

void loop() {}

Not that it would be a problem, but instead of converting string types you could use what is given:

const std::string WANTED_DEVICE_NAME = "abc";

...

if((WANTED_DEVICE_NAME.compare(deviceName) == 0) && (rssi > CUTOFF))
...

In the simple sketch like you have above, there is a serial output, however if I try to print the device name in my sketch it doesn't have a output

Have you tried Serial.println(device.getName());?

If you aren't getting any names, that might explain why you never get a match for your desired name.

My thoughts exactly, however, refering to the code that I adapted from, the original code (on the YouTube video) works just fine, and lights up my led when brought close to my Bluetooth device

Sounds like some change you made broke the code. I would look at the changes very carefully.

Link to the working code?

Try this loop:

const std::string WANTED_DEVICE_NAME = "abc";

void loop()
{
  BLEScan *scan = BLEDevice::getScan();
  scan->setActiveScan(true);
  BLEScanResults results = scan->start(1);
  isOn = false;
  for (int i = 0; i < results.getCount(); i++) {
    BLEAdvertisedDevice device = results.getDevice(i);
    if((WANTED_DEVICE_NAME.compare(device.getName()) == 0) && (device.getRSSI() > CUTOFF))
    {
      isOn = true;
      break; //End for loop
    }
  }
  digitalWrite(PIN, isOn ? HIGH : LOW);
  scan->clearResults();
  delay(100);
}

I gave it a try, but it doesnt seem to work


bt



So as you can see, i set it up to print the number of devices that it picks up, it returns 1 but i have 3 active bluetooth devices around me, my laptop, my phone, and a bluetooth controller

Please do not post images of text......

What do you get if you print the name mismatch like this:

Serial.print("Name does NOT match: '");
Serial.print(device.getName());
Serial.println("'");

No name gets returned,
Is it possible that I need to establish a connection between each device first to read the name?

You could try to see if that changes anything, but I doubt. Usually you would be able to see the name of the devices before you pair / connect them.

EDIT1: Earlier you said, that some other code you've tried works, please post that code or a link to it.

EDIT2: If the code you used is from the video, then there is no identification by name, it just checks if there is any device within a certain range. You may have luck identifying a device by its device- or service-UUID, but make sure that it is unique to the device.

Yes it was just the code from the video, working in the sense that it lit up the led when a Bluetooth device was close to it.

Question, would getting the address also not be unique to the device?

I do not know if the address is unique to the device, the device type or if is assigned by the server.

I just had a look and it seems that the address gets assigned by the server and thus changes on every reboot. It looks like i have use the getName function

I did some more tests and i used:

device.toString();

That seems to dump all the data from the found devices, however, the very first thing listed by this method is the name, and guess what, both devices show nothing on the name.
Honestly I dont know why, I am using nRF Connect for my phone and my laptop as my second device, they seem to find each other perfectly with names and everything but the ESP32 wont seem to find the names