Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

237 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🩺 Smart IoT-Based Healthcare Monitoring & Management System

🌍 Overview

A production-grade, real-time healthcare IoT platform that continuously monitors critical vital signsβ€”including ECG, blood oxygen saturation (SpOβ‚‚), and heart rateβ€”through wearable ESP32-based sensors. The system features:

  • Real-time biosignal streaming from wearable patches via WebSocket and REST APIs
  • LSTM-based machine learning inference for ECG arrhythmia detection and SpOβ‚‚ trend prediction
  • Multi-role web dashboard for doctors, nursing staff, and system administrators
  • Comprehensive patient management with medical history, prescriptions, and clinical notes
  • AI-powered risk scoring (Digital Twin) combining multiple biomarkers with explainability
  • Secure role-based access control with patient-doctor-staff hierarchies

By combining edge computing (ESP32), cloud analytics (FastAPI backend), and real-time visualization (React frontend), the platform enables clinicians to make data-driven decisions and detect critical health events instantaneously.


βš™οΈ Core Components

🧠 1. Smart Wearable IoT Patch

Hardware: ESP32 microcontroller + multi-sensor array

Sensors Integrated:

  • ECG Sensor (AD8232) – 250 Hz sampling, lead detection, arrhythmia-ready
  • SpOβ‚‚ Sensor (MAX30102) – Pulse oximetry with Maxim algorithm integration
  • WiFi Connectivity – 802.11 for real-time data transmission
  • Dual Transmission Mode:
    • WebSocket (port 81) – Real-time streaming (ECG @ 25 Hz, SpO2 @ 0.5 Hz)
    • HTTP REST API (port 80) – Persistent logging and device management

Data Acquisition:

  • High-fidelity analog-to-digital conversion
  • On-device finger detection and signal quality assessment
  • Automatic WiFi reconnection and data buffering
  • REST endpoints for debugging: /status, /health, /ecg-raw, /spo2-raw, /memory, /network

Firmware: WearablePatch.ino with modular sensor components


πŸ“Š 2. Real-Time Backend System

Framework: FastAPI (Python) with PostgreSQL + TimescaleDB

REST API Endpoints (19 route modules)

Authentication & Authorization:

  • JWT-based authentication for doctors, staff, and admins
  • Role-based permission delegation

Real-Time Monitoring (WebSocket):

  • /ws/ecg/{patient_id} – ECG streaming with live ML predictions
  • /ws/spo2/{patient_id} – SpOβ‚‚ streaming with trend analysis
  • /ws/vitals/{patient_id} – Combined vital signs feed

Clinical Management:

  • GET/POST /api/patients – Patient CRUD, medical history, allergies, medications
  • GET/POST /api/patients/{id}/vitals – Vital signs history and ranges
  • POST /api/prescriptions – Prescription creation, tracking, expiry alerts
  • POST /api/clinical-notes – Doctor/staff note creation and retrieval
  • GET /api/ai-insights/{patient_id} – Digital-twin risk scores with explanations
  • GET/POST /api/staff-tasks – Task assignment for nursing staff

Device Management:

  • POST /api/devices/register – Auto-registration of ESP32 devices (no auth required)
  • GET /api/admin/devices – Device monitoring, firmware versions, debug info
  • POST /api/admin/devices/{id}/command – Remote command execution (JSON-based)

Telemedicine:

  • /api/telemedicine/consultations – Video consultation infrastructure (scheduling/management)

Machine Learning Inference Services

ECG Arrhythmia Detection (ecg_ml_inference.py)

  • Model: LSTM-based CNN-LSTM hybrid
  • Input: 15-second ECG windows @ 360 Hz (5,400 samples)
  • Preprocessing: Notch filter (50Hz rejection), bandpass (0.5-45 Hz), resampling, z-score normalization
  • Output: Binary classification (Normal/Abnormal) + confidence score + heart rate
  • Buffer Manager: Sliding 15-second window with 2-second updates for real-time performance
  • Fallback: Mock predictions on model version mismatch

SpOβ‚‚ Trend Prediction (spo2_ml_inference.py)

  • Model: LSTM (64 units) with BatchNorm and cost-sensitive loss
  • Input: Variable-length SpOβ‚‚ sequences with 19 engineered features
  • Loss Function: Weighted cross-entropy (FN weight = 10.0) to prioritize hypoxia detection
  • Threshold Optimization: F2-score maximization for high recall on critical events
  • Output: Trend classification + decline alerts + confidence metrics

Risk Scoring Engine (patient_risk_scoring.py)

  • Digital Twin approach combining biomarkers:
    • ECG prediction: 42% weight
    • SpOβ‚‚ prediction: 40% weight
    • Signal stability/motion artifact: 18% weight
  • Rolling 1-minute average to dampen transient artifacts
  • Returns: Risk level (low/medium/high/critical), contributing factors, explanation

Database Design

TimescaleDB Time-Series Storage:

  • Hypertable: vitals_timeseries (partitioned by time)
  • Efficient storage for millions of vital sign readings
  • Indexed on: time, patient_id, device_id for fast range queries

Relational Models:

  • patients – Full patient profiles with demographics, medical history, emergency contacts, insurance
  • users – Doctors, nurses, admins with role-based permissions
  • devices – ESP32 device registration, WiFi credentials, firmware versions
  • prescriptions – Medication orders with dosage schedules, renewal dates, patient compliance
  • clinical_notes – Free-text medical notes from doctors/staff
  • ai_insights – AI-generated health recommendations with timestamps and confidence
  • telemedicine_consultations – Video call scheduling and metadata
  • staff_tasks – Task queue for nursing staff (vitals check, medication delivery, incident reports)
  • system_logs – Audit trail and error logging

πŸ’» 3. Real-Time Frontend Dashboard

Technology Stack: React + Vite + Tailwind CSS + WebSocket

Doctor Dashboard (src/pages/DoctorDashboard.jsx)

  • Patient list with risk indicators and alerts
  • Real-time vital signs trending
  • Quick access to recent clinical notes
  • AI insight recommendations with explainability
  • Activity feed (new prescriptions, consultation requests, staff updates)

Patient Detail View (src/pages/PatientDetail.jsx)

  • Live ECG waveform visualization with WebSocket streaming
  • SpOβ‚‚ monitoring with historical charts
  • Heart rate trends and variability analysis
  • Prescription history and active medications
  • AI insights panel with digital-twin risk breakdown
  • Medical history sidebar with allergies, conditions, past hospitalizations

Key Pages:

  • PatientsPage – Patient list filtering, search, and batch operations
  • PrescriptionsPage – Medication management, dosage tracking, expiry alerts
  • TelemedicinePage – Video consultation interface (in development)
  • AIInsightsPage – Explainable AI recommendations dashboard
  • AdminDashboard – System health, user management, device monitoring

Real-Time Updates:

  • ECG chart updates @ 25 Hz via WebSocket
  • SpOβ‚‚ and vitals @ 0.5 Hz
  • Auto-refresh on patient/prescription changes
  • Alert notifications for critical events

Admin Panel (src/AdminApp.jsx)

  • System status and performance metrics
  • Device debugging and firmware monitoring
  • User role management and permission delegation
  • System configuration and settings

πŸ”¬ 4. Machine Learning Models

All models trained on real clinical datasets and production-ready.

ECG Analysis Model (ml-models/ecg-analysis/)

Architecture:

15-second window (5,400 samples @ 360 Hz)
    ↓
Conv1D (32 filters) + MaxPool
    ↓
Conv1D (64 filters) + MaxPool
    ↓
LSTM (128 units)
    ↓
Dense (64) + Dropout
    ↓
Output: Binary classification (Normal/Abnormal)

Training Data: MIT-BIH Arrhythmia Database (~100,000+ annotated beats) Model Storage:

  • SavedModel format: ecg_lstm_model_savedmodel/
  • Keras format: best_ecg_model.keras

SpOβ‚‚ Prediction Model (ml-models/spo2-prediction/model/)

Architecture:

Variable-length sequence (19 features)
    ↓
LSTM (64 units) + L2 regularization
    ↓
BatchNorm + Dropout
    ↓
Dense (32) + BatchNorm
    ↓
Output: Probability of critical decline

Key Innovation: Cost-sensitive learning with 10x penalty for false negatives (missed hypoxia events) Model Variants: Small, Medium, Large for different computational constraints Training Optimization: F2-score threshold for high recall on critical thresholds


πŸ—οΈ Complete System Architecture

Data Flow Pipeline

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Wearable Patch     β”‚
β”‚  - ECG Sensor       β”‚
β”‚  - SpOβ‚‚ Sensor      β”‚
β”‚  - Lead Detection   β”‚
β”‚  - Finger Detection β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚ WiFi
           β”œβ”€ WebSocket (port 81): Real-time streaming
           └─ HTTP REST (port 80): Persistent logging
           β”‚
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    FastAPI Backend Services      β”‚
β”‚  - Device auto-registration      β”‚
β”‚  - Real-time WebSocket handlers  β”‚
β”‚  - Buffer management (15-sec)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”
    β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ ML Models  β”‚ β”‚  PostgreSQL  β”‚
β”‚ ECG LSTM   β”‚ β”‚  + Timescale β”‚
β”‚ SpO2 LSTM  β”‚ β”‚  DB (vitals) β”‚
β”‚ Risk Score β”‚ β”‚  relations   β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚              β”‚
       β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
              β–Ό
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚   Backend Services  β”‚
       β”‚  - AI Insights      β”‚
       β”‚  - Predictions      β”‚
       β”‚  - Risk Scoring     β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β–Ό             β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ WebSocketβ”‚ β”‚ REST API β”‚ β”‚Telemedicine
β”‚ Streamingβ”‚ β”‚ Polling  β”‚ β”‚ Interface
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚             β”‚             β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β–Ό
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚  React Frontend      β”‚
       β”‚  - Doctor Dashboard  β”‚
       β”‚  - Patient Details   β”‚
       β”‚  - Real-time Charts  β”‚
       β”‚  - AI Insights View  β”‚
       β”‚  - Admin Panel       β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‘ Technology Stack

Layer Technology
Sensors & Hardware ESP32, AD8232 (ECG), MAX30102 (SpOβ‚‚)
Sensor Firmware Arduino C++ (WearablePatch.ino)
Real-Time Communication WebSocket (WSv1), HTTP/1.1 REST, JSON
Backend Framework FastAPI (Python 3.8+)
Databases PostgreSQL 13+, TimescaleDB 2.0+
ML & Inference TensorFlow/Keras, NumPy, SciPy, Scikit-learn
Frontend Framework React 18+, Vite, React Router
Styling Tailwind CSS 3+, Chart.js
HTTP Client Axios
Container Orchestration Docker Compose
Authentication JWT tokens, bcrypt hashing
Monitoring System logs, audit trails

πŸ“ Project Structure

Cognivus-Labs-Dev/
β”‚
β”œβ”€β”€ hardware/                          # ESP32 firmware & sensor libraries
β”‚   β”œβ”€β”€ WearablePatch/
β”‚   β”‚   β”œβ”€β”€ WearablePatch.ino         # Main ESP32 sketch
β”‚   β”‚   β”œβ”€β”€ ECGSensor.h/cpp           # AD8232 integration (250 Hz)
β”‚   β”‚   β”œβ”€β”€ SpO2Sensor.h/cpp          # MAX30102 integration
β”‚   β”‚   β”œβ”€β”€ WebSocketServer.h/cpp     # Real-time streaming (port 81)
β”‚   β”‚   β”œβ”€β”€ APIServer.h/cpp           # REST API (port 80, JSON-only)
β”‚   β”‚   β”œβ”€β”€ WiFiManager.h/cpp         # WiFi handling & data transmission
β”‚   β”‚   β”œβ”€β”€ CommandHandler.h/cpp      # Remote troubleshooting
β”‚   β”‚   β”œβ”€β”€ SystemMonitor.h/cpp       # Memory & performance tracking
β”‚   β”‚   β”œβ”€β”€ Config.h/cpp              # Configuration parameters
β”‚   β”‚   └── python_clients/           # Test scripts (api_client, websocket_client)
β”‚   └── firmware_core/
β”‚       └── firmware_core.ino
β”‚
β”œβ”€β”€ web-app/
β”‚   β”œβ”€β”€ backend/                       # FastAPI REST API + ML inference
β”‚   β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”‚   β”œβ”€β”€ main.py               # FastAPI app setup (port 8000)
β”‚   β”‚   β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ config.py         # Environment variables & settings
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ database.py       # SQLAlchemy ORM setup
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ auth.py           # JWT authentication
β”‚   β”‚   β”‚   β”‚   └── dependencies.py   # Dependency injection
β”‚   β”‚   β”‚   β”œβ”€β”€ models/               # Database models (SQLAlchemy)
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ patient.py        # Patient profiles & medical history
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ user.py           # Doctors, staff, admins
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ device.py         # ESP32 device registration
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ vital_timeseries.py # Time-series data (TimescaleDB)
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ prescription.py   # Medication orders
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ clinical_note.py  # Medical notes
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ ai_insight.py     # AI predictions & recommendations
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ staff_task.py     # Nursing staff task queue
β”‚   β”‚   β”‚   β”‚   └── telemedicine.py   # Consultation infrastructure
β”‚   β”‚   β”‚   β”œβ”€β”€ api/routes/           # API endpoints (19 route files)
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ auth.py           # User login, JWT tokens
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ admin_auth.py     # Admin login
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ patients.py       # Patient CRUD, medical history
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ patient_vitals.py # Vital signs history
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ prescriptions.py  # Prescription management
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ clinical_notes.py # Doctor notes
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ ai_insights.py    # Digital-twin predictions
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ staff_tasks.py    # Task management
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ telemedicine.py   # Consultation scheduling
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ devices.py        # Device auto-registration
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ admin_devices.py  # Device debugging & monitoring
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ admin_users.py    # User management
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ admin_system.py   # System health metrics
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ ecg_websocket.py  # Real-time ECG with ML
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ spo2_websocket.py # Real-time SpOβ‚‚ with ML
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ vitals_websocket.py # Combined vitals streaming
β”‚   β”‚   β”‚   β”‚   └── live_vitals.py    # HTTP polling alternative
β”‚   β”‚   β”‚   └── services/             # Business logic & ML inference
β”‚   β”‚   β”‚       β”œβ”€β”€ ecg_ml_inference.py    # ECG arrhythmia detection
β”‚   β”‚   β”‚       β”œβ”€β”€ spo2_ml_inference.py   # SpOβ‚‚ trend prediction
β”‚   β”‚   β”‚       β”œβ”€β”€ patient_risk_scoring.py # Digital-twin risk engine
β”‚   β”‚   β”‚       β”œβ”€β”€ buffer_manager.py      # Sliding window buffer
β”‚   β”‚   β”‚       └── notification.py        # Alert systems
β”‚   β”‚   β”œβ”€β”€ requirements.txt           # Python dependencies
β”‚   β”‚   β”œβ”€β”€ run.py                     # Dev server runner
β”‚   β”‚   β”œβ”€β”€ run_ecg_feeder.py          # Simulation: ECG data feeder
β”‚   β”‚   β”œβ”€β”€ run_oxygen_feeder.py       # Simulation: SpOβ‚‚ data feeder
β”‚   β”‚   β”œβ”€β”€ docker-compose.yml         # Container orchestration
β”‚   β”‚   └── Dockerfile                 # Backend image definition
β”‚   β”‚
β”‚   └── frontend/                      # React SPA with Vite
β”‚       β”œβ”€β”€ src/
β”‚       β”‚   β”œβ”€β”€ pages/
β”‚       β”‚   β”‚   β”œβ”€β”€ DoctorDashboard.jsx      # Doctor main dashboard
β”‚       β”‚   β”‚   β”œβ”€β”€ PatientDetail.jsx        # Patient detailed view
β”‚       β”‚   β”‚   β”œβ”€β”€ PatientsPage.jsx         # Patient list & search
β”‚       β”‚   β”‚   β”œβ”€β”€ PrescriptionsPage.jsx    # Prescription management
β”‚       β”‚   β”‚   β”œβ”€β”€ AIInsightsPage.jsx       # AI recommendations
β”‚       β”‚   β”‚   β”œβ”€β”€ TelemedicinePage.jsx     # Video consultation
β”‚       β”‚   β”‚   β”œβ”€β”€ AdminDashboard.jsx       # System admin panel
β”‚       β”‚   β”‚   └── ... (more pages)
β”‚       β”‚   β”œβ”€β”€ components/            # Reusable UI components
β”‚       β”‚   β”‚   β”œβ”€β”€ ECGChart.jsx       # Live ECG visualization
β”‚       β”‚   β”‚   β”œβ”€β”€ SpO2Monitor.jsx    # SpOβ‚‚ trending
β”‚       β”‚   β”‚   β”œβ”€β”€ VitalsPanel.jsx    # Multi-vital display
β”‚       β”‚   β”‚   β”œβ”€β”€ AlertNotifications.jsx
β”‚       β”‚   β”‚   └── ... (40+ components)
β”‚       β”‚   β”œβ”€β”€ hooks/
β”‚       β”‚   β”‚   β”œβ”€β”€ useWebSocket.js    # WebSocket connection
β”‚       β”‚   β”‚   β”œβ”€β”€ useAPI.js          # REST API calls
β”‚       β”‚   β”‚   β”œβ”€β”€ useAuth.js         # Auth context
β”‚       β”‚   β”‚   └── usePatient.js      # Patient data management
β”‚       β”‚   β”œβ”€β”€ services/              # API client services
β”‚       β”‚   β”‚   β”œβ”€β”€ patientService.js
β”‚       β”‚   β”‚   β”œβ”€β”€ vitalService.js
β”‚       β”‚   β”‚   β”œβ”€β”€ prescriptionService.js
β”‚       β”‚   β”‚   β”œβ”€β”€ aiInsightService.js
β”‚       β”‚   β”‚   └── ... (more services)
β”‚       β”‚   β”œβ”€β”€ App.jsx                # Main router (doctor/staff/patient views)
β”‚       β”‚   β”œβ”€β”€ AdminApp.jsx           # Admin router (/sys/*)
β”‚       β”‚   └── index.css              # Global styles
β”‚       β”œβ”€β”€ vite.config.js             # Main app build config
β”‚       β”œβ”€β”€ vite.config.admin.js       # Admin panel build config
β”‚       β”œβ”€β”€ package.json               # Main app dependencies
β”‚       β”œβ”€β”€ package.admin.json         # Admin app dependencies
β”‚       β”œβ”€β”€ tailwind.config.js
β”‚       β”œβ”€β”€ postcss.config.js
β”‚       β”œβ”€β”€ index.html                 # Main app entry
β”‚       β”œβ”€β”€ index-admin.html           # Admin panel entry
β”‚       └── public/                    # Static assets
β”‚
β”œβ”€β”€ ml-models/
β”‚   β”œβ”€β”€ ecg-analysis/                  # ECG arrhythmia detection
β”‚   β”‚   β”œβ”€β”€ models/                    # Trained models
β”‚   β”‚   β”‚   β”œβ”€β”€ ecg_lstm_model_savedmodel/  # TensorFlow SavedModel format
β”‚   β”‚   β”‚   └── best_ecg_model.keras        # Keras H5 format
β”‚   β”‚   β”œβ”€β”€ data/
β”‚   β”‚   β”‚   └── MIT-BIH Arrhythmia Dataset (100-105.hea/atr/xws)
β”‚   β”‚   β”œβ”€β”€ main.py                    # Training pipeline
β”‚   β”‚   β”œβ”€β”€ model.py                   # Model architecture
β”‚   β”‚   └── test.py                    # Validation script
β”‚   β”‚
β”‚   └── spo2-prediction/               # SpOβ‚‚ trend forecasting
β”‚       β”œβ”€β”€ model/
β”‚       β”‚   β”œβ”€β”€ model_small.keras      # Lightweight variant
β”‚       β”‚   β”œβ”€β”€ model_medium.keras     # Balanced variant
β”‚       β”‚   β”œβ”€β”€ spo2model.py           # Model definition
β”‚       β”‚   └── training/              # Training notebooks
β”‚       β”œβ”€β”€ data/                      # Training datasets
β”‚       └── test/                      # Validation scripts
β”‚
β”œβ”€β”€ mobile-app/
β”‚   └── Test_Flutter_Project/          # Flutter patient app (in development)
β”‚       β”œβ”€β”€ lib/                       # Dart app code
β”‚       β”œβ”€β”€ pubspec.yaml               # Flutter dependencies
β”‚       └── ... (platform-specific code)
β”‚
β”œβ”€β”€ database/
β”‚   β”œβ”€β”€ init_timescaledb.sql           # TimescaleDB hypertable setup
β”‚   β”œβ”€β”€ create_test_patient.sql        # Test data scripts
β”‚   └── ... (migration files)
β”‚
β”œβ”€β”€ tests/
β”‚   └── ECG-Sensor-Test/               # Hardware integration tests
β”‚       β”œβ”€β”€ monitor.py, monitor-v2.py
β”‚       └── real-time.py
β”‚
β”œβ”€β”€ requirements.txt                   # Root Python dependencies
β”œβ”€β”€ convert_model_to_savedmodel.py     # Model format conversion utility
β”œβ”€β”€ test_ecg_flow.py                   # End-to-end ECG pipeline test
β”œβ”€β”€ ECG_QUICKSTART.md                  # Quick setup guide
└── README.md                          # This file

✨ Key Features & Capabilities

Feature Status Details
Real-Time ECG Monitoring βœ… Full WebSocket streaming @ 25 Hz, 15-sec windows with LSTM inference
SpOβ‚‚ Trend Tracking βœ… Full WebSocket @ 0.5 Hz, LSTM predictions with decline alerts
Heart Rate Extraction βœ… Full Real-time HR from ECG with variability metrics
Patient Management βœ… Full CRUD, medical history, allergies, current medications, emergency contacts
Prescription Tracking βœ… Full Creation, expiry alerts, compliance monitoring
Clinical Notes βœ… Full Doctor/staff note creation with timestamps and patient linkage
AI Risk Scoring βœ… Full Digital-twin approach combining 3 biomarkers with explainability
ECG Arrhythmia Detection βœ… Full LSTM model trained on MIT-BIH database
SpOβ‚‚ Hypoxia Prediction βœ… Full Cost-sensitive LSTM with high recall on critical drops
Device Auto-Registration βœ… Full ESP32 devices register via REST API without manual setup
WebSocket Dual-Stream βœ… Full ECG + SpOβ‚‚ + vitals on separate channels with multiplexing
Role-Based Access Control βœ… Full Doctor, Nursing Staff, Admin with permission hierarchies
Admin Dashboard βœ… Full System metrics, user management, device debugging
Telemedicine Infrastructure 🟑 Partial Scheduling in place; video integration in development
Mobile App (Flutter) 🟑 In Dev Patient-facing monitoring interface
Automated Medicine Dispenser πŸ”΄ Not Impl. Planned for Phase 2
Body Temperature Tracking πŸ”΄ Not Impl. Hardware not integrated
Blood Pressure Monitoring πŸ”΄ Not Impl. Hardware not integrated

πŸš€ Getting Started

1. Hardware Setup

Required Components:

  • ESP32 DevKit
  • AD8232 ECG sensor module
  • MAX30102 pulse oximeter module
  • USB Serial adapter for programming

Flash Firmware:

# Arduino IDE or PlatformIO
# Open: hardware/WearablePatch/WearablePatch.ino
# Select Board: ESP32 Dev Module
# Configure pins in Config.h
# Flash to device

WiFi Configuration: Update Config.h with your network credentials:

const char* WIFI_SSID = "Your_SSID";
const char* WIFI_PASSWORD = "Your_Password";
const char* BACKEND_IP = "192.168.x.x";
const int BACKEND_PORT = 8000;

2. Backend Setup

Prerequisites:

  • Python 3.8+
  • PostgreSQL 13+ with TimescaleDB extension
  • pip/poetry

Installation:

cd web-app/backend
pip install -r requirements.txt

Environment Variables (.env file):

DATABASE_URL=postgresql://user:pass@localhost:5432/cognivus
TIMESCALEDB_ENABLED=true
JWT_SECRET=your-secret-key
MQTT_BROKER=localhost
MQTT_PORT=1883

Database Initialization:

python init_db.py
# Creates tables, hypertables, and indexes

Start Server:

python run.py
# Runs on http://localhost:8000
# Swagger UI: http://localhost:8000/docs

With Docker:

docker-compose -f docker-compose.yml up

3. Frontend Setup

Installation:

cd web-app/frontend
npm install

Development Server:

npm run dev
# Opens on http://localhost:5173

Admin Panel (Separate Build):

npm run dev:admin
# Opens on http://localhost:5174

Production Build:

npm run build
# Main app: dist/index.html
# Admin app: npm run build:admin

4. Test the Full Pipeline

Simulate Device Data:

# Terminal 1: ECG data feeder
python web-app/backend/run_ecg_feeder.py --patient-id 1 --device-id ECG_SIM_001

# Terminal 2: SpOβ‚‚ data feeder
python web-app/backend/run_oxygen_feeder.py --patient-id 1 --device-id SPO2_SIM_001

Monitor Real-Time Streams:

cd hardware/WearablePatch/python_clients
python websocket_client.py  # Connect to ESP32 WebSocket

πŸ”Œ API Endpoints Overview

Authentication

  • POST /api/auth/login – Doctor/staff login
  • POST /api/auth/admin-login – Admin login
  • POST /api/auth/logout – Logout

Patients

  • GET /api/patients – List all patients
  • GET /api/patients/{id} – Patient details
  • POST /api/patients – Create patient
  • PUT /api/patients/{id} – Update patient

Vital Signs

  • GET /api/patients/{id}/vitals?start=&end= – Vital history
  • POST /api/patients/{id}/vitals – Log manual vitals
  • GET /api/patients/{id}/vitals/latest – Last reading

Real-Time WebSocket

  • ws://localhost:8000/ws/ecg/{patient_id} – ECG stream + ML predictions
  • ws://localhost:8000/ws/spo2/{patient_id} – SpOβ‚‚ stream + trend analysis
  • ws://localhost:8000/ws/vitals/{patient_id} – Combined vitals

AI Insights

  • GET /api/patients/{id}/ai-insights – Risk scores and recommendations
  • GET /api/patients/{id}/ai-insights/latest – Most recent prediction

Prescriptions

  • GET /api/prescriptions?patient_id= – Patient medications
  • POST /api/prescriptions – Create prescription
  • PUT /api/prescriptions/{id} – Update prescription

Admin Routes

  • GET /api/admin/devices – All registered devices
  • POST /api/admin/devices/{id}/command – Send remote command
  • GET /api/admin/users – All users
  • GET /api/admin/system/health – System metrics

πŸ”§ ML Model Usage

Using ECG Model Directly:

from app.services.ecg_ml_inference import ECGMLInference

inference = ECGMLInference()
# Expects: 5400 samples (15 sec @ 360 Hz)
prediction = inference.predict(ecg_signal)
# Returns: {"is_abnormal": bool, "confidence": float, "heart_rate": float}

Using SpO2 Model:

from app.services.spo2_ml_inference import SpO2MLInference

inference = SpO2MLInference()
# Expects: variable-length sequence with 19 features
prediction = inference.predict(spo2_sequence)
# Returns: {"prediction": float, "trend": str, "confidence": float}

Retraining Models

cd ml-models/ecg-analysis
python main.py --epochs 50 --batch-size 32

πŸ“Š Database Schema Highlights

TimescaleDB Hypertable (vitals_timeseries):

  • Optimized for fast time-range queries
  • Automatic data compression on older records
  • Automatic data retention policies

Key Indexes:

  • (time, patient_id) – Fast patient vital retrieval
  • (patient_id, time DESC) – Latest vitals lookup
  • (device_id, time DESC) – Device data debugging

πŸ” Security Features

βœ… JWT Authentication – Secure API token generation and validation
βœ… Password Hashing – bcrypt with salt
βœ… Role-Based Access Control – Doctor/staff/admin hierarchies
βœ… CORS Configuration – Frontend-backend isolation
βœ… Input Validation – Pydantic models for all API inputs
βœ… SQL Injection Prevention – SQLAlchemy ORM parameterized queries
βœ… Audit Logging – All data access logged to system_logs
βœ… Device Auto-Registration – Cryptographic device tokens


πŸ§ͺ Testing

Test Real-Time ECG Flow:

python test_ecg_flow.py

Test Device Integration:

cd tests/ECG-Sensor-Test
python monitor.py

API Testing via Swagger UI: Navigate to http://localhost:8000/docs and test endpoints interactively.


πŸ“ˆ Performance Metrics

Component Metric Target
ECG Processing Latency (end-to-end) < 5 seconds
SpOβ‚‚ Inference Throughput 1 prediction / 2 sec
WebSocket Message rate 25 Hz (ECG), 0.5 Hz (SpOβ‚‚)
Database Query time (patient vitals) < 100 ms
ML Model Inference time (ECG) < 2 sec
ML Model Inference time (SpOβ‚‚) < 1 sec
Uptime System availability 99.9%

πŸ› Debugging & Troubleshooting

Device Connection Issues:

# Check device status
curl http://<ESP32_IP>/status

# Get real-time diagnostics
curl http://<ESP32_IP>/health

Backend Logs:

# Check FastAPI logs
docker-compose logs -f backend

# View database queries
export LOG_LEVEL=DEBUG; python run.py

Frontend Issues:

# Check browser console for WebSocket errors
# Inspect Network tab for API calls
# Verify backend is running on port 8000

πŸ“œ Project Roadmap

Phase 1 (Current): βœ…

  • Real-time ECG/SpOβ‚‚ monitoring
  • LSTM-based ML inference
  • Multi-role web dashboard
  • Patient/prescription/clinical note management

Phase 2 (Planned):

  • πŸ”΄ Automated medicine dispenser integration
  • 🟑 Telemedicine video consultation (in development)
  • 🟑 Mobile app (Flutter) patient interface
  • Temperature and blood pressure sensors

Phase 3 (Future):

  • Wearable health alerts (push notifications)
  • Advanced predictive modeling (multivariate forecasting)
  • Inter-hospital data exchange protocols
  • Wearable device firmware OTA updates

πŸ‘₯ Development Team

Core Team Members:

  • Wathsala Dewmina – Embedded Systems Engineer, Sensor Integration, Lead Backend Development, Security & Authentication
  • Rivindu Ashinsa – Lead AI/ML Engineer, Research & Development, Model Inference, All AI/ML Implementation
  • Dulina Samarathunga – Frontend Developer, Backend Assistant
  • Lakindu Minosha – Flutter App Development, Progressive Web App, Mobile App for Vitals Monitoring
  • Wooshan Gamage – Hardware Engineering Support, Backend Assistance

Institution: Computing School (Bachelor of Science in Computer Science)
IIT (Informatics Institute Of Technology), Sri Lanka

Official Website: 🌐 cognivusmed.com

Academic Project: Second Year SDGP (Software Development Group Project) at IIT


πŸ“„ License

This project is developed for academic research and educational purposes as a capstone project at IIT.

Use Restrictions:

  • Educational and research use only
  • Requires permission for commercial deployment
  • Not approved for clinical use without regulatory certification

Β© 2025-2026 Smart IoT Healthcare Monitoring System Team. All rights reserved.


πŸ“š Documentation Files

  • ECG_QUICKSTART.md – Quick start guide for ECG streaming
  • ECG_MONITORING_IMPLEMENTATION.md – Detailed ECG implementation guide
  • Swagger API Docs – Interactive at http://localhost:8000/docs
  • Frontend Component Library – Built-in Storybook integration

πŸ”— Related Resources


🎯 Citation

If you use this project for academic research, please cite:

@software{cognivus_2025,
  title   = {Smart IoT-Based Healthcare Monitoring System},
  author  = {Dewmina, W. and Ashinsa, R. and Gamage, W. and Samarathunga, D. and Minosha, L.},
  year    = {2025},
  school  = {Informatics Institute of Technology, Sri Lanka},
  type    = {Capstone Project}
}

πŸ“ž Support & Contributions

For Bug Reports: Open an issue with:

  • Reproduction steps
  • Screenshots/logs
  • ESP32 firmware version
  • Backend version

For Feature Requests: Describe the use case and expected behavior.

For Contributors:

  • Follow PEP 8 (Python) and Prettier (JavaScript) style guides
  • Write tests for new features
  • Document API changes in Swagger
  • Update this README for architecture changes

Made with ❀️ for advancing healthcare technology

About

AI-powered real-time health monitoring system that analyzes wearable sensor data at the edge for intelligent vital insights.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages