Documentation
This firmware enables an ESP32 to:
- πΆ Measure distance using WiFi RSSI (Received Signal Strength Indicator)
- π± Communicate via Bluetooth Low Energy (BLE)
- π Provide audio feedback through a passive buzzer
- π Reconnect to WiFi automatically if disconnected
Key Features:
- BLE Notifications for distance data
- Melody playback when triggered (
iScreechflag) - WiFi-based distance estimation
- ESP32 (Tested on ESP32-WROOM-32)
- Passive Buzzer (Connected to GPIO 25)
- WiFi Network (For RSSI-based distance measurement)
| Library | Purpose |
|---|---|
BLEDevice.h |
BLE Core Functions |
BLEServer.h |
BLE Server Setup |
BLEUtils.h |
BLE Utilities |
BLE2902.h |
BLE Notifications |
WiFi.h |
WiFi Connectivity |
esp_pm.h |
Power Management |
| UUID | Type | Purpose |
|---|---|---|
0000abcd-... |
Service | Main BLE Service |
0000deaf-... |
TX Characteristic | Sends distance data |
0000beef-... |
RX Characteristic | Receives commands |
-
TX Characteristic:
- Properties:
READ+NOTIFY - Callback:
MyReadingCallBack(sends distance value)
- Properties:
-
RX Characteristic:
- Properties:
WRITE - Callback:
MyCallbacks(triggers buzzer oniScreech)
- Properties:
Initializes:
- Serial Monitor (115200 baud)
- Buzzer (GPIO 25)
- BLE (Server, Service, Characteristics)
- WiFi (Connects to
ssidandpassword)
- Measures distance (
calculateMediumDistance()) - Updates BLE (Sends distance via
pTxCharacteristic) - Plays melody if
iScreech == true - Reconnects WiFi if disconnected
Function: calculateMediumDistance()
- Uses WiFi RSSI to estimate distance
- Applies log-distance path loss model:
distance = 10^((txPower - RSSI) / (10 * n))
- Parameters:
txPower = -47(RSSI at 1 meter)n = 2.0(Path-loss exponent)
- Averages 100 samples for stability
Function: playMelody()
- Plays a 4-note melody (
C4, G4, A4, F4) - Uses
tone()andnoTone()for clean playback
| Callback | Trigger | Action |
|---|---|---|
onWrite() |
Data received | Toggles iScreech if 5 bytes sent |
onRead() |
Read request | Sends distMedium (2 decimal places) |
- Install Arduino IDE + ESP32 Core
- Upload the code
- Monitor Serial Output (115200 baud)
- Read Distance: Read
TX Characteristic(UUID:0000deaf...) - Trigger Buzzer: Write 5 bytes to
RX Characteristic(UUID:0000beef...)
Modify in code:
const char* ssid = "qrp"; // Your WiFi SSID
const char* password = "procoder31"; // Your WiFi password| Issue | Solution |
|---|---|
| No BLE Connection | Check UUIDs match in BLE client |
| Buzzer Not Working | Verify GPIO 25 connection |
| WiFi Disconnects | Ensure stable network |
| Inaccurate Distance | Calibrate txPower and n |
Modify playMelody():
int melody[] = {NOTE_C4, NOTE_E4, NOTE_G4}; // New notes
int durations[] = {300, 300, 300}; // New durations- Use BLE RSSI instead of WiFi (replace
WiFi.RSSI()with BLE RSSI) - Implement Kalman Filter for smoothing
This Flutter app connects to an ESP32 over Bluetooth Low Energy (BLE) to:
- π Scan and connect to an ESP32 running the companion firmware
- π Read distance measurements from the ESP32
- π² Send commands to trigger audio feedback on the ESP32
β
BLE Scanning - Discover nearby ESP32 devices
β
Distance Monitoring - Read real-time distance values
β
Command Control - Send "blink" command to ESP32
β
Auto-Reconnect - Handles connection drops gracefully
- Flutter SDK (v3.0+)
- flutter_blue_plus (BLE plugin)
- ESP32 Firmware (See companion code)
- Android/iOS Device (with BLE support)
| UUID | Type | Purpose |
|---|---|---|
0000abcd-... |
Service | Main BLE service |
0000deaf-... |
TX Characteristic | Receives distance data |
0000beef-... |
RX Characteristic | Accepts commands |
-
Clone the Repository
git clone https://github.com/your-repo/esp32_ble_flutter.git cd esp32_ble_flutter -
Install Dependencies
flutter pub get
-
Run on Device
flutter run
- Tap "Scan for Devices" to discover nearby ESP32s.
- Devices appear in a list below.
- Tap a device to connect.
- The app will:
- Send a "blink" command (triggers buzzer on ESP32).
- Enable notifications for distance updates.
- Distance values appear in a Card widget at the bottom.
- Updates automatically via BLE notifications.
- Connection status appears at the top.
- Errors are logged in the status bar.
| Function | Purpose |
|---|---|
_startScan() |
Starts BLE device discovery |
_connectToDevice() |
Handles BLE connection logic |
_sendMamaliga() |
Sends "blink" command |
_getDistData() |
Reads distance value |
| Issue | Solution |
|---|---|
| No devices found | Ensure ESP32 is advertising |
| Connection fails | Check UUIDs match ESP32 firmware |
| Distance not updating | Verify notifications are enabled |
π Companion ESP32 Code:
ESP32 BLE Distance Monitor Firmware
MIT License - Free for personal/commercial use.