The Ultimate BLE Presence Detector

About: Photonics Engineer

Hi there,

In this instructable I will show how I made a really simple Bluetooth Low Energy presence detector, using my smart wristband and a relay I was able to control the ligths of my room;

Everytime I go in, turn the light on and if I left the room or cut the bluetooth connection, the lights turn off.

Step 1: Parts

I'm using a ESP32 Feather but any other will work

1 5v Relay

1 TIP31C Transsitor

1 BLE Server device (Any beacon device)

The TIP31C its ment to control the relay, beacuse the 3V3 digital outputs of the ESP32 are not enough in voltage and current

The relay to control the 120V lights and the wristband to detect the presence of the person.

Step 2: Circuit

This is really simple, the pin number 33 of the ESP32 goes to the base of the transistor, with this we can add the 5V VCC signal and control a bigger voltage with the 3V3 voltage output, then, with the relay we can controll then the 120V of the light.

Step 3: Code

#include "BLEDevice.h"
int Lampara = 33;
int Contador = 0;
static BLEAddress *pServerAddress;
BLEScan* pBLEScan;
BLEClient*  pClient;
bool deviceFound = false;
bool Encendida = false;
bool BotonOff = false;
String knownAddresses[] = { "your:device:mac:address"};
unsigned long entry;
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() > -85) {
          deviceFound = true;
        }
        else {
          deviceFound = false;
        }
        Device.getScan()->stop();
        delay(100);
      }
    }
};
void setup() {
  Serial.begin(115200);
  pinMode(Lampara,OUTPUT);
  digitalWrite(Lampara,LOW);
  BLEDevice::init("");
  pClient  = BLEDevice::createClient();
  pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);
  Serial.println("Done");
}
void Bluetooth() {
  Serial.println();
  Serial.println("BLE Scan restarted.....");
  deviceFound = false;
  BLEScanResults scanResults = pBLEScan->start(5);
  if (deviceFound) {
    Serial.println("Encender Lamara");
    Encendida = true;
    digitalWrite(Lampara,HIGH);
    Contador = 0;
    delay(10000);
  }
  else{
    digitalWrite(Lampara,LOW);
    delay(1000);
  }
}
void loop() { 
  Bluetooth();
}

Step 4: PCB for Light Control

I made this circtuit on a protoype pcb to make things cleaner.

Step 5: Done

And you are done!

You can use this code to open doors instead, or to control different things

I hope you like my instructable, and if you have any question make me a comment or send me an inbox, I'll be happy to answer

Share

    Recommendations

    • Organization Contest

      Organization Contest
    • Epilog X Contest

      Epilog X Contest
    • Paper Contest

      Paper Contest

    11 Discussions

    0
    None
    Lindermann95mischaka

    Reply 4 weeks ago

    Nice! Now the interesting part is what you connect to the rellay or what do you activate with when you are near the sensor

    0
    None
    mischakaLindermann95

    Reply 4 weeks ago

    Use Bluetooth relay: if I leave home I want the TV to not turn on. Children should not watch everything on TV without me. If I leave the workplace, my monitor automatically turns off with the help of a relay. The room in which I often go in and out, but where should I not go to strangers if I am not around? The door is automatically locked if I move away from this place.

    0
    None
    kebibg

    3 months ago

    That is what i search long time. I am not a professional programmer and project like this help me to create my own projects.What will make from this project?
    From long time ago i have a idea for good car imobiliser. Now is time to realize it. With little adding a code i will make it to work with 3-4 MACs (fitnes bands or ibeacons), will add deep sleep in code too.
    When you come to a car and open it (my is with keyless) CAN-BUS wakeup signal from a car will wakeup ESP32 from a deep sleep and he will check for a deisred MAC Address are inside in a range defined by RSSI. If it a present they will allow starting a car. All this procedure from unlock to allowing start take between 0.8 to 1.5 sec.. For more security and to can work with android phones (because now won't) will take UUID for checking instead of a MAC Address. Who don't work with android units? Because from version 5 or 6 for more security BLE in android start anytime with different random generated MAC Address. UUID are used for identifying different services and are unique.

    Sorry for my bad english :(
    If anyone can help with application for android with widged button who will send specific UUID via BLE are welcome.

    Thanks to Lindermann95.
    Regards

    2 replies
    0
    None
    Lindermann95kebibg

    Reply 2 months ago

    Hi there, I'm sorry, I haven't been around lately, but this car application is one of my plans too
    That's a good idea, the random generation MAC like the one that apple use on the WiFi connction. Maybe that could be the answer, Using WiFi...
    But I havent work that much with WiFi, BLE would be a suitable option at the moment, I'll work in this project and I'll keep in touch with you, have you already done it?

    0
    None
    kebibgLindermann95

    Reply 2 months ago

    Hi,
    i made it and he work fine at this time. For Authorization I use UUID, because MAC any time when you start advertising service are different this is in BLE standard. Now I use one application who create GATT server with UUID desired by me. I will write more in few days because this is hobby for me and now I have a many professional work.

    Regards

    0
    None
    kebibg

    3 months ago

    At this time i found you use library ESP32 BLE Arduino by Neil Kolban.
    That is right? Are you use arduino ide for this project or another one?

    Regards

    2 replies
    0
    None
    Lindermann95kebibg

    Reply 3 months ago

    Yes! I’m using the Neil Kolban library in the Arduino IDE,
    I think that the code didnt pase the way I wanted... but let me change it and add some comments
    Let me know if you have more questions

    0
    None
    kebibgLindermann95

    Reply 3 months ago

    Thank you for fast anwer.
    Ok that fine. From the code i assumed it was written on Adruino but not 100% sure.
    If it is on platformio then will have include Arduino.h and all will be clear.

    Regards

    0
    None
    kebibg

    Question 3 months ago

    Hi,
    You are add one source code but this code are for ???? platform
    Ok i see you add library BLEDevice.h but from where.
    I have a interest about this project and will be good to add more light about him :))
    Regards

    1 answer
    0
    None
    Lindermann95kebibg

    Answer 3 months ago

    Hi there! (I’ll answer in the other comment)