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

    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • Plastics Contest

      Plastics Contest
    • Optics Contest

      Optics Contest

    6 Discussions

    0
    None
    kebibg

    7 days 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 MAC are inside in a car. If it a present they will allow starting of a car. After test this take between 0.8 to 1.5 sec., this is good time.For more security and to can work with android phones (because not won't) will take UUID for checking not a MAC. 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. UUID are used for identifying.

    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

    0
    None
    kebibg

    12 days 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 11 days 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 11 days 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 12 days 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 more answer
    0
    None
    Lindermann95kebibg

    Answer 11 days ago

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