Physics-based drain blockage detection and flood early warning system for smart cities.
FlowGuard combines IoT sensors, hydraulic physics, machine learning, and network cascade modeling to detect drainage blockages in real-time and predict downstream flood impacts across urban drainage networks.
Urban flooding in cities like Nagpur causes:
- Infrastructure damage
- Traffic disruption
- Public health risks
- Economic losses
Root cause: Drain blockages from debris accumulation go undetected until flooding occurs. Current inspection methods (manual checks, CCTV) are reactive, labor-intensive, and cannot scale citywide.
FlowGuard deploys low-cost IoT sensors at strategic drainage points to:
- Detect blockages using calibrated orifice equation physics
- Confirm with ML (Isolation Forest) to filter sensor noise from genuine blockages
- Forecast trends to estimate days until critical blockage
- Model cascade effects using Muskingum routing to predict downstream flood timing and magnitude
- Maintain audit trail for civic accountability and compliance
- Physics-first approach: Orifice equation with field-calibrated discharge coefficient (Cd)
- ML confirmation layer: Isolation Forest distinguishes genuine blockage trends from sensor noise
- Network simulation: Muskingum hydrological routing models flood wave propagation through drainage network
- Real-time dashboard: Streamlit interface for monitoring, calibration, and network simulation
- Complete audit trail: SQLite database logs every calibration, reading, and confirmed event
- Low-cost hardware: ESP32 + HC-SR04 ultrasonic sensor (~$5-10 USD per node)
Hardware:
- ESP32 microcontroller
- HC-SR04 ultrasonic distance sensor
Core Application:
- Python 3.10+
- NumPy, Pandas (numerical computing)
- scikit-learn (ML anomaly detection)
- Streamlit (dashboard)
- SQLite (audit trail)
Analysis Tools:
- Rasterio (DEM processing)
- SciPy (hydrological modeling)
- Matplotlib (visualization)
- Flask + Folium (web mapping)
FlowGuard/
├── src/
│ └── flowguard/
│ ├── __init__.py
│ ├── blockage_detector.py # Orifice physics + ML + forecasting
│ ├── network_simulation.py # Muskingum routing for cascade modeling
│ ├── storage.py # SQLite audit trail
│ ├── flowguard_dashboard.py # Streamlit dashboard
│ └── webviewer/ # Flask web viewer for flood risk maps
├── scripts/
│ ├── hydro.py # DEM processing utilities
│ ├── flood_simulation.py # Grid-based flood simulation
│ ├── flood_risk_analysis.py # Citywide risk assessment
│ └── ...
├── data/
│ ├── nagpur_drainage/ # Nagpur drainage network data
│ └── flowguard_history.db # Audit trail database
├── docs/ # Project documentation
├── tests/ # Test suite
├── assets/ # Presentation materials
├── .venv/ # Python virtual environment (not in Git)
├── .gitignore
├── pyproject.toml # Project configuration
├── README.md # Setup Guide
└── CLAUDE.md # AI agent operating guide
- Python 3.10 or higher
- Git
- pip (Python package manager)
For hardware deployment:
- ESP32 development board
- HC-SR04 ultrasonic sensor
- Physical drainage channel for calibration
git clone https://github.com/nomadrai/FlowGuard.git
cd FlowGuardLinux/macOS:
python3 -m venv .venv
source .venv/bin/activateWindows:
python -m venv .venv
.venv\Scripts\Activate.ps1Core application:
pip install -e .With analysis tools:
pip install -e ".[analysis]"With web viewer:
pip install -e ".[webviewer]"Development (includes testing and linting):
pip install -e ".[dev]"streamlit run src/flowguard/flowguard_dashboard.pyThe dashboard opens the ESP32 serial port itself and streams readings live —
no separate terminal or data-entry step needed (see Live Demo Workflow
below). python src/flowguard/serial_reader.py still works standalone for a
terminal view of the same stream.
The dashboard provides:
- Calibration panel: Enter physical measurements to calculate discharge coefficient (Cd)
- Physical node monitoring: Submit real-time sensor readings
- Blockage detection: Physics-based calculation with ML confirmation
- Trend forecasting: Estimate days until critical blockage
- Network cascade simulation: Model flood propagation through drainage network
- Audit trail: Complete history of calibrations, readings, and events
Test blockage detector (physics + ML):
python src/flowguard/blockage_detector.pyTest network simulation:
python src/flowguard/network_simulation.pyRun flood simulation for Nagpur:
cd scripts
python flood_simulation.pyRun flood risk analysis:
cd scripts
python flood_risk_analysis.pyLaunch web viewer for flood maps:
python src/flowguard/webviewer/app.pyPhysical geometry (fixed — do not change these values):
- Drainage pipe: round, diameter 1.90 cm → clean area = π × (0.95)² ≈ 2.8353 cm²
- Inlet box: rectangular, base area 308 cm² (water collects here above the pipe exit)
-
No area measurement needed — the clean pipe area (2.8353 cm²) is pre-set in code.
-
Perform calibration pour:
- Pour a known volume (e.g., 200 mL) into the inlet box at a steady rate
- Time the pour duration
- Record steady-state water height from the sensor
- Repeat 3 times for accuracy
-
Set inflow rate and calculate Cd in the dashboard:
- Enter the rainfall inflow rate (mL/s) in the sidebar — this is your pour rate (volume ÷ time). Set this once per session before submitting any readings.
- Enter: pour volume, pour time, steady height (clean pipe area is auto-filled)
- Click "Calibrate Cd"
- System calculates your channel's discharge coefficient
-
Establish baseline (clean pipe):
- Set inflow rate in sidebar to match your current rainfall/pour rate
- Submit 10-15 readings with clean (unblocked) pipe
- Only enter water height per reading — inflow Q comes from the sidebar
-
Monitor for blockages:
- Continue submitting water-height readings during operation
- System calculates blockage % using sidebar inflow rate each time
- ML layer confirms genuine blockage trends
-
Respond to alerts:
- High blockage % + ML confirmation = maintenance needed
- Trend forecast shows days until critical threshold
- Network simulation predicts downstream flood impacts
- Select upstream rainfall intensity
- Click "Run network simulation"
- View flood wave propagation:
- Peak arrival delays (hydrograph lag)
- Peak attenuation (flood smoothing)
- Downstream impact timing
pytest tests/black src/ tests/ruff check src/ tests/mypy src/Suggested sequence for judging/presentation:
- Show hardware setup: ESP32, sensor, physical channel
- Demonstrate clean channel: Run the dashboard (it opens the ESP32 port itself), pour water, watch readings stream in and show ~0% blockage
- Live blockage insertion: Insert obstruction (e.g., sponge) in front of audience
- Show detection: Pour again, watch the verdict flip to BLOCKAGE DETECTED and the rise rate jump above the normal rate
- Network cascade: Run simulation to show citywide flood propagation
- Audit trail: Display compliance/accountability features
- Close with value proposition: Real-time detection + citywide prediction + civic accountability
Pro tip: Build 15-20 baseline readings (clean + blocked) before the presentation so ML confirmation works reliably during live demo.
See docs/ directory for detailed documentation:
- COMPLETE_BUILD_STEPS.md: Step-by-step hardware and software setup
- PS_AND_SOLUTION.md: Problem statement and solution approach
- HARDWARE_ASSEMBLY.md: Physical sensor wiring and assembly
- CALIBRATION_GUIDE.md: Detailed calibration procedures
- FULL_HARDWARE_BUILD.md: Complete hardware build instructions
- PROJECT_BRIEF_FOR_HANDOFF.md: Project handoff documentation
-
blockage_detector.py
- Orifice equation: Q = Cd × A × √(2gh)
- Field-calibrated Cd removes viscosity assumptions
- Isolation Forest ML detects genuine blockage trends
- Linear trend forecasting for maintenance scheduling
-
network_simulation.py
- Muskingum routing: cascade flood wave through network
- Models hydrograph delay and attenuation
- Simulates rainfall pulses and downstream impacts
-
storage.py
- SQLite audit trail for compliance
- Logs: calibrations, readings, confirmed events, simulations
- Supports civic accountability requirements
-
flowguard_dashboard.py
- Streamlit interface integrating all components
- Real-time monitoring and simulation
- Historical data visualization
Sensor → ESP32 → Serial Monitor → Dashboard Input
↓
Orifice Physics Calculation
↓
ML Confirmation Layer
↓
Trend Forecasting
↓
Network Cascade Model
↓
Audit Trail Log
Issue: Dashboard shows "ModuleNotFoundError"
- Ensure virtual environment is activated
- Run
pip install -e .from project root
Issue: Sensor readings unstable
- Check HC-SR04 wiring (VCC→5V, Trig→GPIO, Echo→GPIO, GND→GND)
- Verify sensor is perpendicular to water surface
- Calibrate with multiple pour trials for robust Cd
Issue: ML not confirming blockages
- Insufficient baseline: need 15-20 readings minimum
- Build clean + blocked history before expecting confirmation
- ML requires trend pattern, not single anomalous point
Issue: Network simulation shows unrealistic flood timing
- Check Muskingum K (travel time) and X (weighting) parameters
- Verify network topology (upstream → downstream ordering)
- Ensure rainfall intensity is realistic for local conditions
Issue: Analysis scripts fail
- Install analysis dependencies:
pip install -e ".[analysis]" - Ensure DEM data exists in
data/nagpur_drainage/ - Check file paths in script configuration
Contributions are welcome. Please:
- Fork the repository
- Create a feature branch
- Make focused, well-tested changes
- Run tests and linting before committing
- Submit a pull request with clear description
MIT License - see LICENSE file for details
- Nagpur Municipal Corporation for drainage network data
- OpenStreetMap contributors for urban infrastructure data
- SRTM/ASTER for DEM elevation data
- Ambazari Lake and Nag River hydrological systems as case study
For questions, issues, or collaboration inquiries:
- GitHub: nomadrai/FlowGuard
- Issues: GitHub Issues
FlowGuard — Detecting blockages before floods happen.