Projects / Room presence with BLE beacons

Room presence with BLE beacons

Intermediate3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Make the tag

    Advertise the name ELTAG.

  2. ESP32

    Scan on one board

    Print the RSSI.

  3. ESP32

    Smooth the values

    Moving average.

  4. ESP32

    Add MQTT

    Publish to the broker.

  5. ESP32

    Add two more rooms

    Change ROOM_INDEX on each.

  6. ESP32

    Pick the winner

    Strongest smoothed signal.

  7. ESP32

    Link to a light

    In your home automation.

In short

How do you build a room presence with BLE beacons?

ESP32s in each room scan for your phone or a tag and report which room you are in. You need ESP32 DevKit V1 × 3 and NRF52840 dongle. It takes 7 steps and about 180 minutes.

Level
Intermediate
Time
About 180 min
Parts
2

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ESP32 in each roomUSB power supplyPower (three rooms, three boards)
TagYour own BLE tag or a spare ESP32Advertises the name ELTAG
Wi-FiRouter in range of all three boardsFor the MQTT messages
BrokerMQTT broker on your own networkSuch as Mosquitto on a Pi

Code

A Bluetooth tag shouts its name a few times a second, and the closer a listener is, the stronger the signal it hears. Put a listener in each room, and the room with the strongest signal is the room you are in. The numbers jump around, so each board smooths its readings, and any board whose signal fades because the tag is far away loses to the one that still hears it. The three boards share their signal strength over MQTT, and the board in the winning room announces it, so a home automation system can turn lights on where you are. Walls and bodies change the signal, so test and place the boards near the middle of each room, away from metal. Track only a tag you own and with the knowledge of everyone in the home: following people around without their agreement is not acceptable. Put your own Wi-Fi details and broker address in the placeholders and keep them private; the broker traffic is not encrypted on a normal setup. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// BLE room presence: each ESP32 scans for one tag named ELTAG, smooths its signal strength (RSSI) and shares it over MQTT.
// Every board runs this sketch with its own ROOM_INDEX. Library: PubSubClient. Board: ESP32 Dev Module.
// Tag sketch (a separate spare ESP32): BLEDevice::init("ELTAG"); BLEDevice::getAdvertising()->start();
#include <WiFi.h>
#include <PubSubClient.h>
#include <BLEDevice.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
const char* SSID = "YOUR_WIFI_NAME";
const char* PASS = "YOUR_WIFI_PASSWORD";
const char* BROKER = "192.168.1.10";                 // your MQTT broker on the local network
const char* ROOMS[3] = {"kitchen", "bedroom", "study"};
const int ROOM_INDEX = 0;                            // set 0, 1 or 2 on each board
WiFiClient wifi;
PubSubClient mqtt(wifi);
float rssi[3] = {-100, -100, -100};                  // smoothed signal strength for each room
float myRssi = -100;
int lastWinner = -1;
BLEScan* scan;
class Seen : public BLEAdvertisedDeviceCallbacks {
  void onResult(BLEAdvertisedDevice d) {
    if (d.haveName() && d.getName() == "ELTAG") myRssi = 0.7 * myRssi + 0.3 * d.getRSSI();   // smoothing
  }
};
void onMessage(char* topic, byte* payload, unsigned int len) {
  String t = topic; String v; for (unsigned int i = 0; i < len; i++) v += (char)payload[i];
  for (int r = 0; r < 3; r++) if (t == String("home/presence/") + ROOMS[r] + "/rssi") rssi[r] = v.toFloat();
}
void connectAll() {
  if (WiFi.status() != WL_CONNECTED) { WiFi.begin(SSID, PASS); while (WiFi.status() != WL_CONNECTED) delay(300); }
  while (!mqtt.connected()) {
    if (mqtt.connect(ROOMS[ROOM_INDEX])) mqtt.subscribe("home/presence/+/rssi"); else delay(2000);
  }
}
void setup() {
  Serial.begin(115200);
  BLEDevice::init("");
  scan = BLEDevice::getScan();
  scan->setAdvertisedDeviceCallbacks(new Seen());
  scan->setActiveScan(true);
  mqtt.setServer(BROKER, 1883);
  mqtt.setCallback(onMessage);
}
void loop() {
  connectAll();
  scan->start(2, false); scan->clearResults();       // two second scan
  myRssi -= 1.5;                                      // fade if nothing was seen, so an empty room loses
  rssi[ROOM_INDEX] = myRssi;
  mqtt.publish((String("home/presence/") + ROOMS[ROOM_INDEX] + "/rssi").c_str(), String(myRssi, 1).c_str());
  mqtt.loop();
  int w = 0; for (int r = 1; r < 3; r++) if (rssi[r] > rssi[w]) w = r;
  if (rssi[w] > -95 && w != lastWinner && w == ROOM_INDEX) {          // the winning board announces the room
    mqtt.publish("home/presence/room", ROOMS[w], true);
    lastWinner = w;
  }
  if (w != ROOM_INDEX) lastWinner = w;
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Tag in the kitchenKitchen board has the strongest value
Walk to the studyWinner changes within seconds
Tag removedNo room announced
Two boardsMessages from both visible

If it does not work

Winner flips back and forth

Smooth more and add a margin.

Nothing seen

Check the tag name and that the tag is advertising.

No MQTT

Check the broker address and Wi-Fi.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the room presence with BLE beacons?

ESP32 DevKit V1 × 3 and NRF52840 dongle. NRF52840 dongle: As a BLE tag.

How long does the room presence with BLE beacons take to build?

About 180 minutes for a intermediate maker, in 7 steps.

How do I know the room presence with BLE beacons works?

Tag in the kitchen: Kitchen board has the strongest value. Walk to the study: Winner changes within seconds. Tag removed: No room announced.

Winner flips back and forth: what should I check?

Smooth more and add a margin.

Nothing seen: what should I check?

Check the tag name and that the tag is advertising.

No MQTT: what should I check?

Check the broker address and Wi-Fi.