# Embedded XIAO Web ECG Fork

This is the next architecture after the stable computer-gateway version.

## Goal

Remove the Python gateway from the local workflow.

```text
XIAO ESP32-C3 + AD8232
  -> Wi-Fi
  -> web dashboard served directly by the XIAO
```

The XIAO samples the AD8232, computes tentative live metrics, hosts a dashboard, and streams live ECG packets to the browser with Server-Sent Events.

## Main File

```text
XIAO_AD8232_WIFI_WEB/XIAO_AD8232_WIFI_WEB.ino
```

## What The XIAO Serves

After upload, the board prints its IP address in Serial Monitor.

Open:

```text
http://XIAO_IP/
```

Useful endpoints:

```text
http://XIAO_IP/        dashboard
http://XIAO_IP/info    simple technical info page
http://XIAO_IP/status  JSON status and metrics
http://XIAO_IP/events  live ECG event stream
```

If mDNS works on your device, this may also work:

```text
http://seebscribe-ecg.local/
```

## Upload Steps

1. Open Arduino IDE.
2. Open `XIAO_AD8232_WIFI_WEB/XIAO_AD8232_WIFI_WEB.ino`.
3. Select the Seeed Studio XIAO ESP32-C3 board.
4. Confirm the Wi-Fi name and password at the top of the sketch.
5. Upload.
6. Open Serial Monitor at `115200` baud.
7. Copy the printed `Dashboard: http://.../` URL.
8. Open that URL on your computer or phone on the same Wi-Fi.

## R Peak Direction

The embedded sketch now defaults to upward R-peak detection:

```cpp
const int8_t PEAK_POLARITY = 1;
```

Use:

```cpp
1   // upward peaks
-1  // inverted/downward peaks
0   // auto polarity
```

If the orange R-peak dots appear on the wrong side of the curve after upload, change this value and upload again.

The browser also snaps the orange marker to the local top of the visible filtered curve, so the marker should sit on the cyan crest rather than floating above or below it.

## What Changed From The Stable Version

The stable version used this:

```text
XIAO -> Wi-Fi UDP -> Python gateway on Mac -> browser
```

The embedded fork uses this:

```text
XIAO -> browser
```

No `python3 wifi_ecg_gateway.py` is needed for local viewing.

## Cloudflare / Public Web Note

Cloudflare Tunnel cannot run directly on the XIAO ESP32-C3 in this simple Arduino setup. A tunnel still needs a computer, Raspberry Pi, server, or router-level service to run `cloudflared`.

For a quick public test while the XIAO hosts the dashboard locally:

```bash
cloudflared tunnel --url http://XIAO_IP
```

That computer is only a relay for the web page. It is not processing the ECG signal.

For the later dedicated-server architecture, the better path is:

```text
XIAO ESP32-C3
  -> HTTPS / MQTT / WebSocket
  -> cloud server
  -> public dashboard
```

In that architecture, the XIAO pushes JSON sample batches to the server, and the server handles authentication, history, public links, and multi-viewer dashboards.

## Current Embedded Limitations

- Designed for local same-Wi-Fi testing first.
- One live dashboard client is supported at a time in this first embedded fork.
- BPM/RR/RMSSD are tentative art/prototype metrics, not clinical measurements.
- The dashboard is a compact embedded version to fit inside the microcontroller firmware.
