HeartStream: Real-Time ECG Monitoring and AI Analysis Using ESP8266 & Deep Learning

Ayushi LathiyaAyushi Lathiya
3 min read

πŸš€ Introduction

In today’s healthcare landscape, early detection of heart anomalies can save lives. With that vision, I created HeartStream, a real-time ECG monitoring system powered by ESP8266, AD8232 ECG sensor, and an AI-driven backend built using PyTorch. The model is trained on the MIT-BIH Arrhythmia Database and achieves an impressive 99.7% accuracy in detecting 5 classes of heartbeats.

Try the live app here: heartstream.streamlit.app

πŸ› Project Architecture

Hardware:

  • ESP8266 NodeMCU

  • AD8232 ECG Sensor (single-lead)

  • ECG Electrodes (Red-Yellow-Green placement)

Software:

  • ECGNet Model (PyTorch)

  • ThingSpeak (Cloud Sync)

  • Streamlit (Frontend Web App)

  • FastAPI Websocket Server

GitHub Repo

🧠 ECGNet: Custom Deep Learning Model

HeartStream uses a 1D Convolutional Neural Network called ECGNet. It has been trained on the gold-standard MIT-BIH Arrhythmia Database and identifies 5 different heartbeat types.

πŸ“ˆ Accuracy and Results

  • Overall Accuracy: 99.7%

  • Test Samples: 22,530

  • Macro Avg: Precision: 98%, Recall: 98%, F1: 98%

  • Per-Class Breakdown:

    • Normal: 100% precision & recall

    • LBBB/RBBB: 100% precision, 99% recall

    • APB: 91% precision, 93% recall

    • PVC: 97% precision, 98% recall

  • Confusion Matrix:

Model saved as: best_model.pth

πŸ“‚ Dataset: MIT-BIH Arrhythmia

  • Records Used: 100-124, 200-234

  • Sampling Rate: 250Hz

  • Preprocessing: Standardized with StandardScaler

  • Beat Segment Size: 250 samples (1 second)

Classes:

  1. Normal (N)

  2. LBBB (L)

  3. RBBB (R)

  4. APB (A)

  5. PVC (V)

More info: MIT-BIH on PhysioNet

πŸš€ Real-Time Monitoring with ESP8266

The ESP8266 sends ECG values from the AD8232 sensor to the ThingSpeak cloud every second. The data is visualized and analyzed live on the web app.

Workflow:

  1. Sensor collects ECG signal (analog)

  2. ESP8266 transmits to ThingSpeak

  3. Streamlit app fetches recent data

  4. AI backend predicts the ECG beat class

  5. Visual + Diagnostic report is shown to user

πŸ“œ Features of HeartStream

βœ… Real-time ECG waveform from Arduino Serial Plotter

βœ… Live ECG + BPM chart from ThingSpeak

βœ… AI-powered Arrhythmia classification

βœ… Diagnostic PDF Report

βœ… AI chatbot for basic explanation (on roadmap)

πŸ”Œ Hardware Setup

  • AD8232 connected to ESP8266 A0

  • GND and 3.3V supply

  • ECG electrodes: Red (RA), Yellow (LA), Green (LL)

View the hardware setup and demonstration: Real-Time ECG Monitoring


πŸ“Š Performance Metrics Snapshot

Accuracy          : 99.7%
Precision         : 99%
Recall            : 99%
F1-Score          : 99%
Total Test Samples: 22,530

🌐 Try It Yourself


πŸš€ Future Improvements

  • Add multi-lead ECG support (12-lead mapping)

  • Use RNN/Attention-based architectures (e.g., AttentionECG)

  • Alert system via Telegram or WhatsApp

  • Upload ECG PDF to cloud


πŸ‘‹ Let's Connect!

Thanks for reading! Stay healthy β™₯ and keep building πŸš€

0
Subscribe to my newsletter

Read articles from Ayushi Lathiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ayushi Lathiya
Ayushi Lathiya