# Seebscribe ECG BLE Gateway

This is the first wireless step:

```text
XIAO ESP32-C3 + AD8232
  -> BLE notifications
  -> computer gateway
  -> local dashboard + optional MQTT
```

The XIAO does not publish MQTT. The computer receives compact BLE packets and
converts them into the same JSON/MQTT structure as the USB gateway.

## Files

Upload to the XIAO:

```text
XIAO_AD8232_BLE/XIAO_AD8232_BLE.ino
```

Run on the computer:

```text
ble_ecg_gateway.py
```

## BLE GATT

Device name:

```text
Seebscribe ECG
```

Service UUID:

```text
7b7d1000-6f5d-4fd0-9f7b-2e7a2a8a0001
```

Notify characteristic UUID:

```text
7b7d1001-6f5d-4fd0-9f7b-2e7a2a8a0001
```

## Binary Packet

The XIAO sends 20-byte BLE notifications:

```text
byte 0      magic: 0x45 ('E')
byte 1      version: 1
byte 2      flags: bit0 = LO+, bit1 = LO-
byte 3      sample count, usually 5
bytes 4-7   start sample index, uint32 little-endian
bytes 8-9   sample rate, uint16 little-endian
bytes 10-19 five serial-style ECG samples, uint16 little-endian
```

Five samples per packet means:

```text
250 samples/sec / 5 samples = 50 BLE notifications/sec
```

This is much better than sending JSON directly from the XIAO over BLE.

The current BLE firmware mirrors the stable serial sketch: it uses a 4-sample
moving average and samples at 250 Hz. It always sends ECG samples and sends
AD8232 LO+/LO- only as metadata, matching the clearer serial diagnostic style.
BLE/MQTT/dashboard transport stays wireless, but the sensor harvesting is kept
close to the USB serial version.

## Run

Install the BLE Python dependency:

```bash
pip3 install bleak
```

Upload the BLE Arduino sketch, then run:

```bash
cd "/Users/yaro/Library/CloudStorage/OneDrive-Personal/!FabAcademy 26/Notes/Final Project/Files/ECG"
python3 ble_ecg_gateway.py
```

With MQTT:

```bash
python3 ble_ecg_gateway.py --mqtt --mqtt-host 127.0.0.1
```

If scanning finds the device but connection is unstable, use its address:

```bash
python3 ble_ecg_gateway.py --ble-address XX:XX:XX:XX:XX:XX
```
