This project provides a real-time ECG monitoring system that connects to an ESP32-based ECG sensor via WebSocket. The system processes incoming ECG data, detects heartbeats, classifies heart rhythms, and provides visualization and reporting capabilities. It is an helping hand to doctors who are working day and night for patient service and helps them to easily read the ecg reports with abnormalities at specefic timestamps.
- Real-time ECG Monitoring: Connects to an ESP32 device via WebSocket to receive ECG data
- Signal Processing:
- Bandpass filtering (0.5-40 Hz)
- Notch filtering for 50/60Hz power line interference removal
- Baseline wander removal
- Heartbeat Detection: R-peak detection using prominence-based algorithm
- Heartbeat Classification: Uses a pre-trained CNN model to classify heartbeats into:
- Normal Sinus Rhythm
- Atrial Premature
- Premature Ventricular Contraction
- Fusion Beat
- Unknown Abnormality
- Visualization: Real-time display of raw and filtered ECG signals with R-peak markers
- Reporting: Generates PDF reports summarizing ECG findings and detected abnormalities
- Signal Quality Monitoring: Continuously assesses signal quality
- ESP32 microcontroller with ECG sensor (e.g., AD8232 ECG module)
- Computer with Python 3.7+
- Python packages:
websocketsnumpypandasmatplotlibscipytensorflow(for CNN model)keyboard(for keyboard input)winsound(Windows only, for beep sounds)
-
Clone this repository:
git clone https://github.com/yourusername/ecg-monitor.git cd ecg-monitor -
Install the required Python packages:
pip install -r requirements.txt
-
Place your trained CNN model file (
ecg_cnn_model.h5) in the project directory
-
Ensure your ESP32 is running the ECG WebSocket server and is connected to the same network as your computer.
-
Update the WebSocket URI in
main.pyto match your ESP32's IP address:websocket_uri = "ws://192.168.1.100:81" # Update with your ESP32's IP
-
Run the ECG monitor:
python main.py
-
The system will:
- Connect to the ESP32 WebSocket server
- Display real-time ECG visualization
- Play a beep sound for each detected heartbeat
- Show classification probabilities
-
Press
qat any time to quit and generate a report.
The system generates several output files in the ecg_logs directory:
ecg_session_YYYYMMDD_HHMMSS.csv: CSV log of all ECG data and classificationsecg_debug_YYYYMMDD_HHMMSS.log: Debug log file
Reports are generated in the Report Generated directory as PDF files.
You can modify these parameters in the ECGMonitor class initialization:
sampling_rate: ECG sampling rate (default: 125 Hz)chunk_size: Number of samples for CNN input (default: 187)visualization_buffer_size: Number of samples to display (default: 1000)sound_cooldown: Minimum time between beep sounds (default: 0.3s)condition_logging_delay: Delay before logging persistent conditions (default: 3s)
-
Connection Issues:
- Verify ESP32 is connected to the network
- Check firewall settings to allow WebSocket connections
- Ensure correct IP address in
websocket_uri
-
Model Loading Errors:
- Verify
ecg_cnn_model.h5exists in the project directory - Check TensorFlow version compatibility
- Verify
-
Performance Issues:
- Reduce
visualization_buffer_size - Increase
plot_update_interval
- Reduce
This project is licensed under the MIT License - see the LICENSE file for details.
- AD8232 ECG sensor
- ESP32 microcontroller
- TensorFlow for deep learning capabilities
- Various Python libraries used for signal processing and visualization