Hi,
I have proximity sensor using BLE. My code like this :
// Library
// Library untuk bluetooth
#include "BLEDevice.h"
// Library untuk RFID
#include <SPI.h>
#include <MFRC522.h>
// Inisialisasi variabel bluetooth
static BLEAddress *pServerAddress;
BLEScan* pBLEScan;
BLEClient* pClient;
bool deviceFound = false;
bool session = false;
bool BotonOff = false;
String knownAddresses[] = {"2d:1c:1f:c9:85:0a"};
unsigned long entry;
// Inisialisasi variabel RFID
String registered_rfid[] = {"4B 91 7D 00","49 31 B2 47"};
// Inisialisasi PIN RFID
#define SS_PIN 21
#define RST_PIN 22
#define RELAY_PIN 25
#define BUZZER_PIN 26
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
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){
//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("Device found: ");
Serial.println(Device.getRSSI());
if (Device.getRSSI() > -60) {
deviceFound = true;
}
else {
deviceFound = false;
}
Device.getScan()->stop();
delay(100);
}
}
};
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN,OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
BLEDevice::init("");
pClient = BLEDevice::createClient();
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
Serial.println("Done");
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
}
void Bluetooth() {
Serial.println();
Serial.println("BLE Scan restarted.....");
deviceFound = false;
BLEScanResults scanResults = pBLEScan->start(1);
if (deviceFound) {
session = true;
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
}
}
void Rfid() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
content.toUpperCase();
// Cek apakah kartu terdaftar atau tidak
for (int x=0; x<(sizeof(registered_rfid) / sizeof(registered_rfid[0])); x++){
if (content.substring(1) == registered_rfid[x]) //change here the UID of the card/cards that you want to give access
{
Serial.println("Authorized access");
Serial.println(content.substring(1));
session = true;
}
}
}
void loop() {
if (session == false) {
Bluetooth();
}
if (session == false) {
Rfid();
}
if (session == true) {
digitalWrite(LED_BUILTIN,HIGH);
digitalWrite(RELAY_PIN, HIGH);
}
}
That code works fine, but that code based on bluetooth address. The problem is, my bluetooth server address are dynamically changes. So, how do I change the code so that it's based on the service uuid instead of the address?