ESP32 BLE with RSSI Relay

LumenMesh
Posts: 1
Joined: Tue Jun 02, 2020 12:00 am

ESP32 BLE with RSSI Relay

Postby LumenMesh » Tue Jun 02, 2020 12:08 am

Hello - I'm very new to ESP32 and the Arduino IDE programming. I am looking to program my ESP32 to connect only to certain BLE addresses, once those connections are made and the RSSI reaches a certain threshold, it triggers the relay. I'm about 90% there with this sample sketch i found from a youtube video.


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


const int RELAY = 4;
const int CUTOFF = -80;

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

void loop() {
BLEScan *scan = BLEDevice::getScan();
scan->setActiveScan(true);
BLEScanResults results = scan->start(4);
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;
}
}
digitalWrite(RELAY, best > CUTOFF ? HIGH : LOW);
}



So far this is working well but it will trigger when any bluetooth device reaches the RSSI cutoff, how do I make it so it only connects to my iPhone's address? Also, once it connects to my device, it stops scanning, and keeps the relay on until the connection is broken. I'm using this as a "kill switch" for an electric motorcycle, so my phone will always be within the RSSI threshold while i'm riding, and i don't need it to continue scanning once i'm on the motorcycle.

Thanks for any help you can provide.

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 BLE with RSSI Relay

Postby chegewara » Tue Jun 02, 2020 5:38 am

Hi,
without client app on iPhone it is not possible, and without connection, just using scan and RSSI it is not safe, because it will be very easy to clone advertising parameters and steal your bike with only android smartphone and nRF connect installed on it.

Who is online

Users browsing this forum: No registered users and 105 guests