Passive Bluetooth presence detection notifications

aXXo250
Posts: 2
Joined: Sat Aug 10, 2024 2:14 pm

Passive Bluetooth presence detection notifications

Postby aXXo250 » Sat Aug 10, 2024 3:15 pm

Hi,
So I've never really posted on forums before, but I'm stuck and hoping someone could help?
I've been struggling for weeks to work out how to write code for a PKE (Passive keyless Entry) system.
I'm basicly developing a module (just for personal use) using a esp32 Wroom D1 mini with the NimBLE-Arduino libery to control a relay when in and out of proximity of the Esp to lock/unlock my van.
Ultimately I would like to tap into the Can-Bus system eventually to control this and engine start etc but that's out of my league atm.

Anyway I have been looking at BLE advertising Callbacks to change characteristics to be able to read the RSSI signal strength. I just can't figure out the code.
Any help would be much appreciated.

I will upload some code I have done already soon. (Code based on the "BLE-Notify" sketch)

Sorry if I haven't worded things right, I'm basicly just starting out on the coding side of things.
I've searched the net for info with little success maybe I'm not using the right keywords or knowone is interested in documenting it.

I get error message when compiling but I'm pretty sure it's not ready for compiling yet anyway. ( will look for it when I'm home and upload it)

[Codebox=cpp // /*
// Video: https://www.youtube.com/watch?v=oCMOYS71NIU
// Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippe ... Notify.cpp
// Ported to Arduino ESP32 by Evandro Copercini
// updated by chegewara

// Create a BLE server that, once we receive a connection, will send periodic notifications.
// The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b
// And has a characteristic of: beb5483e-36e1-4688-b7f5-ea07361b26a8

// The design of creating the BLE server is:
// 1. Create a BLE Server
// 2. Create a BLE Service
// 3. Create a BLE Characteristic on the Service
// 4. Create a BLE Descriptor on the characteristic
// 5. Start the service.
// 6. Start advertising.

// A connect hander associated with the server starts a background task that performs notification
// every couple of seconds.
// */

/** NimBLE differences highlighted in comment blocks **/

/*******original********
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
***********************/
#include <NimBLEDevice.h>

BLEServer* pServer = NULL;
BLECharacteristic* pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;

// See the following for generating UUIDs
// https://www.uuidgenerator.net/


#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
//#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

//#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"


// class MyServerCallbacks: public BLEServerCallbacks {
// void onConnect(BLEServer* pServer) {
// deviceConnected = true;
// };

// void onDisconnect(BLEServer* pServer) {
// deviceConnected = false;
// }
// };
//////////////////////////////////////////////////////////////////////////////////////////RSSi/////////////////

// class MyCallbacks : public BLECharacteristicCallbacks {
// void onWrite(BLECharacteristic *pCharacteristic) {
// String rxValue = pCharacteristic->SetVa();


class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;



};

void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};


class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();

if (rxValue.length() > 0) {
Serial.println("*********");
Serial.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++) {
Serial.print(rxValue);

Serial.println();
Serial.println("*********");
}

if (rxValue[0] == '0') {

if (deviceConnected) {

digitalWrite(5, LOW);
digitalWrite(16, LOW);
digitalWrite(17, LOW);


delay(10); // bluetooth stack will go into congestion, if too many packets are s
}

}

if (rxValue[0] == '2') {




if (deviceConnected) {

digitalWrite(5, HIGH);
digitalWrite(16, HIGH);
digitalWrite(17, HIGH);


delay(30); // bluetooth stack will go into congestion, if too many packets
}

}

}

}
/***************** New - Security handled here ********************
****** Note: these are the same return values as defaults ********/
uint32_t onPassKeyRequest() {
Serial.println("Server PassKeyRequest");
return 123456;
}

bool onConfirmPIN(uint32_t pass_key) {
Serial.print("The passkey YES/NO number: "); Serial.println(pass_key);
return true;
}

void onAuthenticationComplete(ble_gap_conn_desc desc) {
Serial.println("Starting BLE work!");
}
};

void setup() {
pinMode(5, OUTPUT); // Led Pin Configurations
pinMode(17, OUTPUT);
pinMode(16, OUTPUT);
Serial.begin(115200);

// Create the BLE Device

BLEDevice::setMTU(517);
BLEDevice::init("ESP32");

// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());


// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);

pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
NIMBLE_PROPERTY::NOTIFY
);

// pTxCharacteristic->addDescriptor(new BLE2902());

BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
NIMBLE_PROPERTY::WRITE
);

pRxCharacteristic->setCallbacks(new MyCallbacks());

// Start the service
pService->start();

//Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
// /** Note, this could be left out as that is the default value */
// pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter

pServer->getAdvertising()->start();
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}


void loop() {


if (deviceConnected) {
////////////////////////////Start of UART_BLE_MTU////////////////////

while (Serial.available() == 0);
String str = Serial.readString();
std::string str1 = str.c_str();
pTxCharacteristic->setValue(str1);
pTxCharacteristic->notify();
delay(10);
}





if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}]


Setup/Info about me :

Windows 10 pro

Preferred Language - C++

Microcontroller - Esp32 Wroom dev D1Mini

IDE- Aduino or Visual audio code

Level of coding experience - Starting out.

Project - PKE system using BLE

C++ libery cloned from GitHub -
Nimble-Arduino ( formally EspBLE I think.)

Thank you in advanced.
Chris

Who is online

Users browsing this forum: No registered users and 110 guests