# Seebscribe ECG Wi-Fi UDP Gateway

This is the Wi-Fi transport experiment:

```text
XIAO ESP32-C3 + AD8232
  -> Wi-Fi UDP packets
  -> computer gateway
  -> local dashboard + optional MQTT
```

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

## Files

Upload to the XIAO:

```text
XIAO_AD8232_WIFI_UDP/XIAO_AD8232_WIFI_UDP.ino
```

Run on the computer:

```text
wifi_ecg_gateway.py
```

## Arduino Settings

Edit these before upload:

```cpp
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
```

By default, the XIAO sends to local broadcast:

```cpp
IPAddress DESTINATION_IP(255, 255, 255, 255);
const uint16_t DESTINATION_PORT = 5005;
```

If broadcast does not arrive, replace `DESTINATION_IP` with your Mac's Wi-Fi IP
address, for example:

```cpp
IPAddress DESTINATION_IP(192, 168, 50, 150);
```

## Binary UDP Packet

The XIAO sends compact UDP packets:

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

Twenty-five samples per packet means:

```text
250 samples/sec / 25 samples = 10 UDP packets/sec
```

The firmware mirrors the stable serial diagnostic sketch: 250 Hz sampling,
4-sample moving average, and LO+/LO- sent only as metadata.

## Run

Start the Wi-Fi gateway:

```bash
python3 wifi_ecg_gateway.py --public-server --ignore-leads
```

With MQTT:

```bash
python3 wifi_ecg_gateway.py --mqtt --mqtt-host 127.0.0.1 --mqtt-port 1883 --public-server --ignore-leads
```

Open:

```text
http://127.0.0.1:8780/ecg_live_dashboard.html?ws=/ws
```
