i am trying to do a BLE proximity feature using ESP32. So, basically I have 2 ESP32, one is the transmitter and the other is receiver. What I am trying to do is, when I bring the transmitter further away from the receiver, the RSSI value received by the receiver should drop and when it drops below -90 I want he Builtin LED on the receiver to turn ON when the transmitter is brought back nearer to the receiver the LED should turn OFF.
What I have done so far is, I have the code for the receiver and the transmitter. So when I bring the transmitter further away the builtin LED on the receiver turn ON. However, the ESP32 receiver detects all the BLE device that near and not specifically my ESP32 transmitter. Now I am trying to amend the code for the receiver so that it only detects my ESP32 transmitter and not other BLE devices. I searched online and I understand that I get the BLE address of the transmitter and put a condition on the receiver to detect this specific address. So this where I am stuck, I am not sure where to and how to put this condition on the receiver.
This is the code for the receiver:
#include <BLEAdvertisedDevice.h>
#include <BLEDevice.h>
#include <BLEScan.h>
#include <Arduino.h>
int playing = 0;
//const int buzzer = 5;
const int PIN = 2;
const int CUTOFF = -90;
void setup() {
pinMode(PIN, OUTPUT);
BLEDevice::init("");
}
void loop() {
BLEScan *scan = BLEDevice::getScan();
scan->setActiveScan(true);
BLEScanResults results = scan->start(1);
int best = CUTOFF;
for (int i = 0; i < results.getCount(); i++) {
BLEAdvertisedDevice device = results.getDevice(i);
int rssi = device.getRSSI();
if (rssi > best) {
best = rssi;
}
}
if(best>CUTOFF){
digitalWrite(PIN,LOW);
}
else{
digitalWrite(PIN,HIGH);
}
}
This is the code for the transmitter:
#include "sys/time.h"
#include "BLEDevice.h"
#include "BLEUtils.h"
#include "BLEServer.h"
#include "BLEBeacon.h"
#include "esp_sleep.h"
#define GPIO_DEEP_SLEEP_DURATION 1 // sleep x seconds and then wake up
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory
BLEAdvertising *pAdvertising; // BLE Advertisement type
struct timeval now;
#define BEACON_UUID "87b99b2c-90fd-11e9-bc42-526af7764f64" // UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/)
void setBeacon()
{
BLEBeacon oBeacon = BLEBeacon();
oBeacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!)
oBeacon.setProximityUUID(BLEUUID(BEACON_UUID));
oBeacon.setMajor((bootcount & 0xFFFF0000) >> 16);
oBeacon.setMinor(bootcount & 0xFFFF);
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04
std::string strServiceData = "";
strServiceData += (char)26; // Len
strServiceData += (char)0xFF; // Type
strServiceData += oBeacon.getData();
oAdvertisementData.addData(strServiceData);
pAdvertising->setAdvertisementData(oAdvertisementData);
pAdvertising->setScanResponseData(oScanResponseData);
}
void setup() {
Serial.begin(115200);
gettimeofday(&now, NULL);
Serial.printf("start ESP32 %d\n", bootcount++);
Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n", now.tv_sec, now.tv_sec - last);
last = now.tv_sec;
}
void loop() {
Serial.println("Continuous mode");
// Create the BLE Device
BLEDevice::init("My Proximity Feature"); // Name of your Beacon Device
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer(); // <-- no longer required to instantiate BLEServer, less flash and ram usage
pAdvertising = BLEDevice::getAdvertising();
BLEDevice::startAdvertising();
setBeacon();
// Start advertising
pAdvertising->start();
Serial.println("Advertizing started...");
delay(1000);
pAdvertising->stop();
Serial.printf("enter deep sleep\n");
esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);
Serial.printf("in deep sleep\n");
}
The BLE Address of my transmitter is "6E:bc:55:18:cf:7b"
Any help would be much appreciated.