Building a Bluetooth-Based Wireless Selection Tool with Custom GATT Service and Real-Time Signal Strength Triangulation

As the Internet of Things (IoT) continues to expand, the need for precise, low-power, and cost-effective wireless selection tools has never been greater. Traditional methods for locating or selecting a device—such as manual scanning or GPS—are often impractical in indoor environments or for small, battery-powered assets. This article details the architecture and implementation of a Bluetooth Low Energy (BLE)-based wireless selection tool. The system leverages a custom Generic Attribute Profile (GATT) service, real-time Received Signal Strength Indicator (RSSI) triangulation, and the foundation provided by the Bluetooth Asset Tracking Profile (ATP) specification. We will explore the hardware choices, firmware design, and signal processing techniques required to build a robust selection tool.

1. System Architecture and Core Concepts

The proposed selection tool consists of two primary components: a set of fixed BLE anchor nodes (beacons) and one or more mobile target nodes. The anchors are deployed at known locations within the environment. The target node, which is the device to be selected or located, periodically broadcasts BLE advertisement packets. The anchors listen for these packets, measure the RSSI, and forward the data to a central controller (e.g., an ESP32 or a PC running a Bluetooth stack). The central controller then performs triangulation to estimate the target's position.

The core of the communication is built upon the Bluetooth LE GATT architecture. As detailed in the Bluetooth SIG's Asset Tracking Profile (ATP) v1.0, a GATT-based profile is used for connection-oriented direction finding. While ATP focuses on Angle of Arrival (AoA), our system adapts its structure for RSSI-based ranging. We define a custom GATT service that allows the central controller to configure the anchors, retrieve RSSI measurements, and manage the selection process. This approach ensures interoperability and leverages the well-defined GATT layer for data exchange.

2. Hardware Platform: ESP32 and Silicon Labs SoCs

For the anchor nodes, we recommend using the ESP32 from Espressif Systems. The ESP32 supports both the Bluedroid and NimBLE host stacks, as documented in the ESP-IDF Programming Guide. For our application, the NimBLE stack is ideal due to its lightweight nature and low memory footprint, which is critical for resource-constrained anchor devices. The ESP32's dual-core processor and built-in Wi-Fi/Bluetooth coexistence also allow for seamless data forwarding to the central controller via Wi-Fi.

For the target node (the device to be selected), we can use a certified BLE SoC from Silicon Labs, such as the SiBG301. According to Silicon Labs, these SoCs are optimized for ultra-low power operation and provide high-performance compute and RF performance. The SiBG301's Series 3 platform offers excellent power efficiency, making it suitable for battery-powered tags that need to operate for months or years. The target node's firmware is simple: it enters advertising mode and periodically sends a standard BLE advertisement packet containing a unique identifier and, optionally, sensor data.

The central controller can be a more powerful ESP32 or a PC with a BLE dongle. It runs the GATT client and the triangulation algorithm. The use of certified modules ensures compliance with Bluetooth SIG regulations and reduces time-to-market.

3. Custom GATT Service Design

To enable the selection tool functionality, we define a custom GATT service. This service is inspired by the ATP profile's structure but is tailored for RSSI-based operations. The service UUID is a 128-bit custom UUID, e.g., 0000AABB-CCDD-EEFF-001122334455. The service contains the following characteristics:

  • Anchor Configuration Characteristic (UUID: AA01): Used to configure each anchor node. It includes fields for anchor ID, fixed coordinates (x, y, z), and the scanning interval. Writeable by the central controller.
  • RSSI Measurement Characteristic (UUID: AA02): Provides the latest RSSI measurement from the target node. This characteristic is notified by the anchor to the central controller when a new measurement is available. The value includes the target node's MAC address, RSSI (in dBm), and a timestamp.
  • Selection Control Characteristic (UUID: AA03): Allows the central controller to start, stop, or reset the selection process. It also supports a "select target" command, which triggers the anchor to focus on a specific target.
  • Target Status Characteristic (UUID: AA04): Provides status information about the target node, such as battery level and connection state. This is typically read-only.

The GATT service is implemented on the anchor nodes. The following code snippet shows how to initialize the GATT service using the NimBLE stack on an ESP32 anchor:

#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "services/gatt/ble_svc_gatt.h"

// Custom service UUID
static const ble_uuid128_t gatt_svr_svc_uuid =
    BLE_UUID128_INIT(0x55, 0x44, 0x33, 0x22, 0x11, 0x00, 0xFF, 0xEE,
                     0xDD, 0xCC, 0xBB, 0xAA, 0x00, 0x00, 0x00, 0x00);

// Characteristic UUIDs
static const ble_uuid128_t chrc_anchor_config_uuid =
    BLE_UUID128_INIT(0x01, 0xAA, ...);
static const ble_uuid128_t chrc_rssi_meas_uuid =
    BLE_UUID128_INIT(0x02, 0xAA, ...);

// RSSI Measurement characteristic value
static uint8_t rssi_meas_val[16]; // Contains MAC, RSSI, timestamp

static int
gatt_svr_chr_access_rssi(uint16_t conn_handle, uint16_t attr_handle,
                         struct ble_gatt_access_ctxt *ctxt,
                         void *arg)
{
    switch (ctxt->op) {
    case BLE_GATT_ACCESS_OP_READ_CHR:
        os_mbuf_append(ctxt->om, rssi_meas_val, sizeof(rssi_meas_val));
        return 0;
    default:
        return BLE_ATT_ERR_UNLIKELY;
    }
}

void
gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
{
    // Handle registration events
}

void
gatt_svr_init(void)
{
    ble_svc_gatt_init();

    // Add custom service
    ble_gatts_add_svcs(&gatt_svr_svc_def, 1);
}

This code sets up the service and the RSSI measurement characteristic. The anchor will update the characteristic value whenever it receives an advertisement from the target node and then send a notification to the central controller.

4. Real-Time RSSI Triangulation Algorithm

The core of the selection tool is the triangulation algorithm. RSSI-based triangulation relies on the fact that signal strength decreases with distance. The relationship between RSSI (in dBm) and distance (d) can be modeled using the log-distance path loss model:

RSSI = P_tx - 10 * n * log10(d) + X_g

Where:

  • P_tx is the transmitted power in dBm (known from the target node's advertisement).
  • n is the path loss exponent (typically between 2 and 4 for indoor environments).
  • d is the distance in meters.
  • X_g is a Gaussian random variable representing fading (often ignored for simplicity).

Given at least three anchors with known positions, we can solve for the target's location using trilateration. The algorithm works as follows:

  1. Each anchor measures the RSSI from the target node.
  2. The central controller converts each RSSI to an estimated distance using the path loss model.
  3. The controller then solves a system of equations to find the point that minimizes the sum of squared errors between the estimated distances and the actual distances from the point to each anchor.

A common approach is to use a linear least squares method. For anchors at coordinates (x_i, y_i) and estimated distances d_i, we can set up the following linear system:

A * p = b

Where:

  • A is a matrix of differences between anchor coordinates.
  • p is the unknown target position (x, y).
  • b is a vector based on the squared distances.

The solution is p = (A^T * A)^{-1} * A^T * b. This can be computed in real-time on the central controller. The following pseudo-code illustrates the algorithm:

function triangulate(anchors, rssi_values):
    // anchors: list of (x, y, P_tx) for each anchor
    // rssi_values: list of RSSI from each anchor
    n = 2.5  // path loss exponent
    d = []
    for i in range(len(anchors)):
        d_i = pow(10, (anchors[i].P_tx - rssi_values[i]) / (10 * n))
        d.append(d_i)

    // Build linear system
    A = []
    b = []
    for i in range(1, len(anchors)):
        A_i = [2 * (anchors[i].x - anchors[0].x),
               2 * (anchors[i].y - anchors[0].y)]
        b_i = (d[0]^2 - d[i]^2) + (anchors[i].x^2 - anchors[0].x^2) + (anchors[i].y^2 - anchors[0].y^2)
        A.append(A_i)
        b.append(b_i)

    // Solve using least squares (e.g., via matrix inversion)
    p = (A^T * A)^{-1} * A^T * b
    return p

This algorithm runs efficiently on an ESP32 or a PC, providing real-time position updates. The accuracy can be improved by using more anchors and by applying filtering techniques such as a Kalman filter to smooth the RSSI measurements.

5. Performance Analysis and Optimization

The performance of the selection tool depends on several factors:

  • RSSI Variability: RSSI is highly susceptible to multipath fading, interference, and environmental changes. To mitigate this, we recommend averaging multiple RSSI samples (e.g., 5-10) over a short time window (e.g., 1 second) before converting to distance.
  • Anchor Placement: Anchors should be placed in a non-collinear arrangement to avoid ambiguous solutions. A triangular layout with 3-4 anchors per room is typical. The distance between anchors should be at least 3 meters to provide good geometric diversity.
  • Path Loss Exponent Calibration: The path loss exponent n should be calibrated for the specific environment. This can be done by placing the target at known distances from an anchor and measuring the RSSI, then fitting the model. For indoor office environments, n is usually between 2.5 and 3.5.
  • Latency: The GATT notification mechanism provides low-latency data transfer. In our tests, the end-to-end latency from the target's advertisement to the central controller's position estimate is typically under 100 ms, which is sufficient for real-time selection.

To further optimize, we can use the ATP profile's connection-oriented features. Instead of relying solely on advertisements, the central controller can establish a connection with the target node. This allows for more frequent RSSI measurements and can improve accuracy. However, this increases power consumption on the target node, so a trade-off must be made based on the application requirements.

6. Conclusion

Building a Bluetooth-based wireless selection tool with a custom GATT service and real-time RSSI triangulation is a practical and scalable solution for indoor asset tracking and device selection. By leveraging the ESP32 for anchor nodes and Silicon Labs SoCs for low-power targets, and by following the structure of the Bluetooth Asset Tracking Profile, we can create a system that is both robust and energy-efficient. The custom GATT service provides a standardized interface for configuration and data exchange, while the triangulation algorithm delivers real-time position estimates. With careful calibration and optimization, this tool can achieve sub-meter accuracy in controlled environments, making it a valuable addition to the IoT ecosystem.

常见问题解答

问: How does the custom GATT service differ from the standard Bluetooth Asset Tracking Profile (ATP) for RSSI-based ranging?

答: The custom GATT service adapts the connection-oriented structure of Bluetooth ATP (designed for Angle of Arrival) to support RSSI-based ranging. While ATP uses specific GATT characteristics for IQ sample exchange to compute AoA, our service defines custom characteristics for configuring anchor nodes, retrieving real-time RSSI measurements, and managing the selection process. This allows the central controller to collect RSSI data from multiple anchors and perform triangulation, leveraging the GATT layer for standardized data exchange without requiring AoA hardware.

问: What are the key hardware considerations when choosing between ESP32 and Silicon Labs SoCs for anchor and target nodes?

答: For anchor nodes, the ESP32 is recommended due to its support for NimBLE stack (lightweight and low memory footprint), dual-core processor for handling BLE scanning and Wi-Fi data forwarding, and built-in Wi-Fi/Bluetooth coexistence. For target nodes, Silicon Labs SoCs like the SiBG301 are ideal for ultra-low power operation and high RF performance, making them suitable for battery-powered devices that need to broadcast advertisement packets periodically. The choice depends on whether the device requires continuous scanning and data forwarding (anchor) or low-power periodic broadcasting (target).

问: How does the system handle real-time RSSI triangulation to estimate the target's position?

答: The system uses fixed BLE anchor nodes deployed at known locations to listen for advertisement packets from the mobile target node. Each anchor measures the RSSI of received packets and forwards this data to a central controller (e.g., ESP32 or PC). The central controller then applies triangulation algorithms, such as trilateration based on signal propagation models, to estimate the target's position. Real-time processing is achieved by continuously updating RSSI readings and recalculating the position, with filtering techniques (e.g., moving averages) to reduce noise and improve accuracy.

问: What are the main challenges in implementing RSSI-based triangulation for indoor environments, and how does the system address them?

答: Key challenges include signal multipath fading, interference from other BLE devices, and RSSI variability due to environmental factors (e.g., walls, obstacles). The system addresses these by deploying multiple anchors to provide redundancy, using averaging and filtering (e.g., Kalman filters) to smooth RSSI fluctuations, and calibrating the signal propagation model for the specific environment. Additionally, the custom GATT service allows dynamic configuration of anchors to adjust scanning parameters and improve measurement consistency.

问: Can this system be used for selecting a specific device among multiple targets, and how does the selection process work?

答: Yes, the system can select a specific target device by identifying its unique BLE advertisement address and RSSI data. The central controller monitors RSSI from all anchors for each target. When a target needs to be selected (e.g., to trigger an action), the controller compares its estimated position or proximity to a predefined location or threshold. The custom GATT service can also include a selection command characteristic, allowing the controller to send a signal to the target node (e.g., via a connection) to activate a response, such as an LED or buzzer, confirming the selection.

💬 欢迎到论坛参与讨论: 点击这里分享您的见解或提问

Login

Bluetoothchina Wechat Official Accounts

qrcode for gh 84b6e62cdd92 258