Help with code esp 32 keyless entry

Hi can anyone help me with this code. It uses an esp 32 dev V1, The task is search for a specified Bluetooth Mac address, When it is in range it turns on the led on pin 2. if it gets out of range it turns off. I want to make a crude keyless entry system with multiple smartwatches as keys (security is not important) but i can only get it to work with 1 Mac address at a time (the one specified as knownAdresses) can someone help me use this code with multiple Mac addresses as a key.

Code:

#include "BLEDevice.h"
 
unsigned int frequency = 1000;  
unsigned int onDuration = 50;
unsigned int offDuration = 100;
unsigned int pauseDuration = 500;
unsigned int cycles = 10;
 
int LED = 2; // on-board LED at pin 2
int BUTTON = 0;
static BLEAddress *pServerAddress;
BLEScan* pBLEScan;
BLEClient*  pClient;
bool deviceFound = false;
bool LEDoff = false;
bool BotonOff = false;
String knownAddresses[] = {"54:b7:e5:6c:79:d0"}; // change the MAC
unsigned long entry;
 
static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
  Serial.print("Notify callback for characteristic ");
  Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
  Serial.print(" of data length ");
  Serial.println(length);
}
 
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice Device){
      // show the MAC of other BLE devices
      //Serial.print("BLE Advertised Device found: ");
      //Serial.println(Device.toString().c_str());
      pServerAddress = new BLEAddress(Device.getAddress()); 
      bool known = false;
      bool Master = false;
      for (int i = 0; i < (sizeof(knownAddresses) / sizeof(knownAddresses[0])); i++) {
        if (strcmp(pServerAddress->toString().c_str(), knownAddresses[i].c_str()) == 0) 
          known = true;
      }
      if (known) {
        Serial.print("Our device found!");
        Serial.print("Device distance:");
        Serial.println(Device.getRSSI());
        // adjust the value. -85 is medium distance
        // -60 is closer than -85
        if (Device.getRSSI() > -100) {
          deviceFound = true;
        }
        else {
          deviceFound = false;
        }
        Device.getScan()->stop();
        delay(100);
      }
    }
};
void setup() {
  Serial.begin(115200);
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
  BLEDevice::init("");
  pClient  = BLEDevice::createClient();
  pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);
  Serial.println("Done");
}
void Bluetooth() {
  Serial.println();
  Serial.println("BLE Scan restarted.....");
  deviceFound = false;
  BLEScanResults scanResults = pBLEScan->start(5);
  if (deviceFound) {
    Serial.println("LED is ON now");
    LEDoff = true;
    digitalWrite(LED,HIGH);
    BUTTON = 0;
    delay(1000);
  }
  else{
    digitalWrite(LED,LOW);
    delay(1000);
  }
}
void loop() { 
  Bluetooth();
}

You seem to be using a mixture of Strings and strings. Why not stick with one or the other ?

Judging by these two lines of code

static BLEAddress *pServerAddress;
//
pServerAddress = new BLEAddress(Device.getAddress());

Then pServerAddress is a string. If that is the case then why not hold the list of valid addresses in an array of strings or pointers to strings which you can iterate through and compare using strcmp() ?

This

pServerAddress->toString().c_str()

appears to convert a string to a String and back again. Why ?

What happens when you put more than one address in the "knownAddresses" list?

Maybe the problem is calling this function whether the first advertised address matches or not:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.