Presence Sensor With ITag Bluetooth BLE

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects. New tutorials on Tuesdays and Fridays.

Today, we are going to talk about iTag. This is a keyring with technology similar to iBeacon, Bluetooth BLE, which serves as a presence detector. This is a wonderful example of residential automation. Let's use an ESP32 programed with Arduino IDE.

This assembly has thousands of uses: the garage door can perceive its approach and open alone, lights turn on according to the detection of its presence, among other innumerable uses. Not to mention that every resident of a house can have their own locksmith and make their own choices regarding the execution of commands in a certain environment.

I want to point out that our assembly with the ESP32 allows the Bluetooth radio to detect presence by a certain radius. This is performed through the dbm signal, which makes it possible (for example) to locate or track people within a location, and specific individual product identification, among others.

In our project, instead of connecting a relay, we light an LED that is already inside the ESP32. But in your source code, if you're automating something, you're going to have the ESP32 hook up a pin that turns the relay on.

Step 1: Demonstration

In the demonstration, we have the ESP32 with a blue LED representing an activated relay. As the key ring is close to the ESP32 (at a distance between 6 and 10 meters), it activates this pin. Both are Bluetooth BLE, meaning low power consumption.

Step 2: ITag.ino

We have included the BLEDevice library and set the parameters. We also point out the variable that will save the scan, among other details.

#include <BLEDevice.h>
#define ADDRESS "ff:ff:c2:07:ab:16" //Endereço do iTag, conseguido pelo próprio scan #define RELAY_PIN 2 //Pino do Relê #define SCAN_INTERVAL 1000 //intervalo entre cada scan #define TARGET_RSSI -100 //RSSI limite para ligar o relê. #define MAX_MISSING_TIME 5000 //Tempo para desligar o relê desde o momento que o iTag não for encontrado BLEScan* pBLEScan; //Variável que irá guardar o scan uint32_t lastScanTime = 0; //Quando ocorreu o último scan boolean found = false; //Se encontrou o iTag no último scan uint32_t lastFoundTime = 0; //Tempo em que encontrou o iTag pela última vez int rssi = 0;

onResult

Here, we have the callback of calls to the scan, and the function that shows us when a device is identified, and if this is what we expected. If it is, it saves the rssi of this device.

//Callback das chamadas ao scan
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { void onResult(BLEAdvertisedDevice advertisedDevice) { //Sempre que um dispositivo for encontrado ele é mostrado aqui Serial.print("Device found: "); Serial.println(advertisedDevice.toString().c_str()); //Se for o dispositivo que esperamos if(advertisedDevice.getAddress().toString() == ADDRESS) { //Marcamos como encontrado, paramos o scan e guardamos o rssi found = true; advertisedDevice.getScan()->stop(); rssi = advertisedDevice.getRSSI(); Serial.println("RSSI: " + rssi); } } };

setup

In setup, we set the relay pin as the output, and set it as low. Also, we save the reference and configure the scan object.

void setup()
{ Serial.begin(115200); //Configura o pino do relê como saída e coloca com low pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, LOW); //Guardamos a referência e configuramos o objeto scan BLEDevice::init(""); pBLEScan = BLEDevice::getScan(); pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setActiveScan(true); }

loop

We set the time in milliseconds from the boot, and we work with the possibilities of locating an iTag.

void loop()
{ uint32_t now = millis(); //Tempo em milissegundos desde o boot if(found){ //Se econtrou o iTag no último scan lastFoundTime = millis(); //Tempo em milissegundos de quando encontrou found = false; if(rssi > TARGET_RSSI){ //Se está dentro do limite, ligamos o relê digitalWrite(RELAY_PIN, HIGH); } else{ //senão desligamos digitalWrite(RELAY_PIN, LOW); } } //Se não encontrou e o tempo desde a última vez que econtrou for maior que o tempo máximo de desaparecido else if(now - lastFoundTime > MAX_MISSING_TIME){ digitalWrite(RELAY_PIN, LOW); //Desligamos o relê } if(now - lastScanTime > SCAN_INTERVAL){ //Se está no tempo de fazer scan //Marca quando ocorreu o último scan e começa o scan lastScanTime = now; pBLEScan->start(1); } }

Step 3: Files

Download the files:

PDF

INO

Share

    Recommendations

    • Beauty Tips Contest

      Beauty Tips Contest
    • Sensors Contest

      Sensors Contest
    • Frozen Treats Challenge

      Frozen Treats Challenge

    Discussions