ESP32 - AirTag Documentation
Loading...
Searching...
No Matches
distance.h File Reference

Functions and variables for RSSI averaging and distance estimation. More...

#include <stdint.h>

Go to the source code of this file.

Functions

void updateRssiAvg (int rssi)
 Update the exponential moving average of the RSSI.
float estimateDistanceMeters (float rssi, float txPower, float n)
 Estimate distance from RSSI using the log-distance path loss model.

Variables

bool hasAvg
 Flag indicating whether an RSSI average has been initialized.
float rssiAvg
 Exponential moving average of the RSSI value.
float txPower
 Reference transmit power (measured RSSI at 1 meter distance).
float nFactor
 Path-loss exponent (environmental factor).

Detailed Description

Functions and variables for RSSI averaging and distance estimation.

Provides global parameters and helper functions for smoothing RSSI values and estimating distance based on the log-distance path loss model.

Function Documentation

◆ estimateDistanceMeters()

float estimateDistanceMeters ( float rssi,
float txPower,
float n )

Estimate distance from RSSI using the log-distance path loss model.

Calculates the approximate distance to a transmitter based on received signal strength, reference power, and environmental factor.

Formula:

\[  d = 10^{\frac{(txPower - rssi)}{10 \cdot n}}
\]

Parameters
[in]rssiCurrent RSSI reading (dBm).
[in]txPowerReference RSSI at 1 meter (dBm).
[in]nPath-loss exponent.
Returns
Estimated distance in meters.

Estimate distance from RSSI using the log-distance path loss model.

Computes an approximate distance to the transmitter based on the difference between the measured RSSI and the known transmit power at 1 meter.

Parameters
[in]rssiReceived signal strength indicator (in dBm).
[in]txPowerTransmitter power at 1 meter (in dBm).
[in]nPath-loss exponent (environmental factor).
Returns
Estimated distance in meters.

◆ updateRssiAvg()

void updateRssiAvg ( int rssi)

Update the exponential moving average of the RSSI.

Uses an exponential smoothing factor to stabilize the RSSI reading against sudden fluctuations.

Parameters
[in]rssiLatest RSSI reading (in dBm).

Update the exponential moving average of the RSSI.

Applies exponential smoothing with a fixed factor $\alpha = 0.2$. This reduces noise in instantaneous RSSI readings.

Parameters
[in]rssiCurrent measured RSSI value (in dBm).

Variable Documentation

◆ hasAvg

bool hasAvg
extern

Flag indicating whether an RSSI average has been initialized.

Flag indicating whether an RSSI average has been initialized.

◆ nFactor

float nFactor
extern

Path-loss exponent (environmental factor).

Represents how quickly the signal attenuates with distance. Common values:

  • Free space: ~2.0
  • Indoor: 2.7 – 4.0

Typical values:

  • ~2.0 in free space
  • 2.7–4.0 in indoor/obstructed environments

Should be tuned experimentally for best accuracy.

◆ rssiAvg

float rssiAvg
extern

Exponential moving average of the RSSI value.

Exponential moving average of the RSSI value.

◆ txPower

float txPower
extern

Reference transmit power (measured RSSI at 1 meter distance).

Typically determined through calibration, defaulting around -59 dBm for many BLE devices.

Reference transmit power (measured RSSI at 1 meter distance).

This value should be tuned based on calibration measurements.