Mastering BLE Beacon Implementation on ESP32 Platform

BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. Beacons are small, low-energy devices that broadcast signals to nearby devices, enabling proximity detection and indoor navigation. The ESP32’s integrated BLE stack makes it ideal for deploying scalable beacon solutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsDiscover how ZGP enables battery-free IoT devices through energy harvesting with ESP32 integrations, supporting smart home and industrial applications.. This article explores the theory behind BLE beacons, practical implementations, and how to use them for location tracking with the ESP32.

Table of Contents🔗

Understanding BLE Beacons🔗

BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. beacons transmit advertising packets containing identifiers like UUIDs, major/minor values, and calibrated RSSI (Received Signal Strength Indicator). Devices like smartphones or ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. scanners detect these packets to estimate proximity.

Key Components of a Beacon Packet:

BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. beacons are commonly used in:

Beacon Protocols: iBeacon vs. Eddystone🔗

There are two main protocols for BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. beacons: iBeacon (Apple) and Eddystone (Google). Each has its own format and use casesZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsDiscover how ZGP enables battery-free IoT devices through energy harvesting with ESP32 integrations, supporting smart home and industrial applications..

FeatureiBeacon (Apple)Eddystone (Google)
Payload TypeStatic UUID/major/minorSupports URLs, TLM (telemetry)
Use CaseLocation-based triggersDynamic content delivery
CompatibilityiOS-centricCross-platform

iBeacon Example:

#include <esp_bt.h>
#include <esp_bt_main.h>
#include <esp_ibeacon_api.h>
void setup() {
  esp_bt_controller_init();
  esp_bt_controller_enable(ESP_BT_MODE_BLE);
  esp_bluedroid_init();
  esp_bluedroid_enable();
  // Define iBeacon parameters
  esp_ble_ibeacon_vendor_t vendor_config = {
    .uuid = {0xf7, 0x82, 0x6d, 0xa6, 0x4f, 0xa2, 0x4e, 0x98, 0x80, 0x24, 0xbc, 0x5b, 0x71, 0xe0, 0x89, 0x3e},
    .major = 123,
    .minor = 456,
    .measured_power = -59 // Calibrated RSSI at 1m
  };
  esp_ble_ibeacon_set(&vendor_config);
  esp_ble_gap_start_advertising(&adv_params); // Start advertising
}
void loop() {}

Eddystone Example:

// Eddystone-URL frame
uint8_t url_data[] = {0x03, 'e', 's', 'p', '3', '2'}; // Encodes "esp32.com"
esp_eddystone_url_config_t eddystone_url = {
  .tx_power = -40,
  .url_scheme = EDDYSTONE_URL_SCHEME_HTTPS,
  .url = url_data,
  .url_len = sizeof(url_data)
};
esp_eddystone_config(&eddystone_url);

Setting Up ESP32 as a BLE Beacon🔗

To configure the ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. as a BLE beacon, you’ll use the esp-idfZigbee Over-the-Air (OTA) Firmware Updates with ESP32 CoordinatorsZigbee Over-the-Air (OTA) Firmware Updates with ESP32 CoordinatorsSecure your IoT network with OTA firmware upgrades using an ESP32 coordinator. Our guide details firmware setup, packaging, security, and troubleshooting. or Arduino framework. Below is an example using the Arduino IDE to set up an iBeacon:

#include <BLEDevice.h>
#include <BLEBeacon.h>
BLEBeacon beacon;
void setup() {
  BLEDevice::init("ESP32 iBeacon");
  BLEServer *pServer = BLEDevice::createServer();
  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  beacon.setManufacturerId(0x4C00); // Apple's Manufacturer ID
  beacon.setProximityUUID(BLEUUID("B9407F30-F5F8-466E-AFF9-25556B57FE6D"));
  beacon.setMajor(1);
  beacon.setMinor(1);
  beacon.setSignalPower(-59); // RSSI at 1 meter
  BLEAdvertisementData advertisementData;
  advertisementData.setFlags(0x06); // General Discoverable Mode
  advertisementData.setManufacturerData(beacon.getData());
  pAdvertising->setAdvertisementData(advertisementData);
  pAdvertising->start();
}
void loop() {
  // Beacon is broadcasting; no need for additional code here.
}

Configuring Beacon Parameters🔗

The key parameters for a BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. beacon include:

Adjust these parameters based on your application. For example, a shorter advertising interval improves accuracy but increases power consumptionQuick Comparison: Range, power consumption, costs, and complexity of each technologyQuick Comparison: Range, power consumption, costs, and complexity of each technologyDiscover the ideal wireless solution for your ESP32 IoT project by analyzing range, power, cost, and complexity. Optimize connectivity now..

Scanning for BLE Beacons with ESP32🔗

To track beacons, the ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. can act as a scanner. Here’s an example of scanning for iBeacons:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
        if (advertisedDevice.haveManufacturerData() && advertisedDevice.getManufacturerData().length() >= 25) {
            std::string strManufacturerData = advertisedDevice.getManufacturerData();
            if (strManufacturerData[0] == 0x4C && strManufacturerData[1] == 0x00) { // Apple's Manufacturer ID
                BLEBeacon beacon;
                beacon.setData(strManufacturerData);
                Serial.printf("Beacon Found: UUID=%s, Major=%d, Minor=%d, RSSI=%d\n",
                    beacon.getProximityUUID().toString().c_str(), beacon.getMajor(), beacon.getMinor(), advertisedDevice.getRSSI());
            }
        }
    }
};
void setup() {
  Serial.begin(115200);
  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);
  pBLEScan->setInterval(100);
  pBLEScan->setWindow(99);
}
void loop() {
  BLEScanResults foundDevices = pBLEScan->start(5, false);
  Serial.printf("Devices found: %d\n", foundDevices.getCount());
  pBLEScan->clearResults();
  delay(5000);
}

Proximity Estimation with RSSI🔗

Distance is estimated using the path loss formula:

\[

d = 10^{\frac{(TX_{power} - RSSI)}{10 \times n}}

\]
  • n: Path-loss exponent (2 for open spaces, 4 for cluttered areas).
RSSI RangeProximity
≥ -50 dBmImmediate (0-1m)
-50 dBm to -70 dBmNear (1-5m)
≤ -70 dBmFar (>5m)

Tip: Use averaging filters to smooth RSSI fluctuations.

Implementing Location Tracking🔗

To estimate the location of a beacon, use the RSSI (Received Signal Strength Indicator) value. RSSI decreases as the distance from the beacon increases. By combining RSSI values from multiple beacons, you can triangulate the position of a device.

For example:

1. Place three beacons in known locations.

2. Measure the RSSI from each beacon.

3. Use trilateration to estimate the device’s position.

Practical Applications and Use Cases🔗

  • Indoor Navigation: Guide users through large buildings like airports or hospitals.
  • Asset Tracking: Monitor the location of high-value equipment in real-time.
  • Proximity Alerts: Trigger notifications when a user enters or exits a specific area.

Security Considerations🔗

While BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. beacons are convenient, they can be vulnerable to spoofing or cloning. To enhance security:

Optimizing Power Consumption🔗

To extend battery lifeCost Analysis: Total Ownership for ESP32 Connectivity SolutionsCost Analysis: Total Ownership for ESP32 Connectivity SolutionsUnlock cost savings with ESP32 IoT solutions. This guide reveals how to balance hardware, connectivity, power, and maintenance costs to master TCO.:

Challenges and Best Practices🔗

While BLE beacon-based location tracking is versatile, there are several challengesZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsDiscover how ZGP enables battery-free IoT devices through energy harvesting with ESP32 integrations, supporting smart home and industrial applications. to consider:

Conclusion🔗

Implementing BLE beacons with the ESP32 for location tracking is a practical way to harness wireless technology for indoor navigation, asset tracking, and various IoT applicationsConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies.. By configuring your ESP32 as a beacon and deploying effective scanning and signal processing methods, you can build robust and cost-effective location-based systems. The balance between power, range, and accuracy is key-so be sure to calibrate and test thoroughly under actual operating conditions. Happy coding and good luck with your IoT projects!

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

Share article

Related Articles