A professional water pump automation system with predictive control, historical pattern analysis, and intelligent scheduling capabilities.
Wilo Water Pump Automation/
โโโ config/ # Configuration files
โ โโโ settings.py # Main configuration settings
โโโ data/ # Data storage
โ โโโ raw/ # Raw data files (CSV, logs)
โ โโโ processed/ # Processed data outputs
โโโ docs/ # Documentation
โ โโโ api/ # API documentation
โ โโโ user/ # User guides
โโโ logs/ # Application logs
โ โโโ pump/ # Pump operation logs
โ โโโ simulation/ # Simulation logs
โโโ models/ # Machine learning models
โ โโโ trained/ # Trained model files (.pkl)
โโโ scripts/ # Utility scripts
โ โโโ deploy/ # Deployment scripts
โ โโโ maintenance/ # Maintenance utilities
โโโ src/ # Source code
โ โโโ core/ # Core application logic
โ โ โโโ main.py # Main application file
โ โโโ dashboard/ # Terminal UI components
โ โ โโโ terminal_ui.py # Professional dashboard styling
โ โโโ models/ # ML model handlers
โ โ โโโ prediction.py # Prediction algorithms
โ โโโ simulation/ # Simulation modules
โ โ โโโ run_simulation.py # Basic simulation
โ โ โโโ simulation_30days.py # Extended simulation
โ โโโ utils/ # Utility modules
โ โโโ data_handler.py # Data processing utilities
โ โโโ sensors.py # Sensor data handling
โโโ tests/ # Test files
โ โโโ unit/ # Unit tests
โ โโโ integration/ # Integration tests
โโโ .gitignore # Git ignore rules
โโโ requirements.txt # Python dependencies
โโโ run.py # Main entry point
- Predictive Pump Control: AI-powered prediction of optimal pump operation times
- Historical Pattern Analysis: 2-year historical data analysis for pattern recognition
- Intelligent Scheduling: Smart scheduling based on usage patterns and environmental factors
- Professional Dashboard: Beautiful terminal-based monitoring interface
- Fallback Mechanisms: Robust fallback systems for sensor failures
- Environmental Adaptation: Adaptive algorithms based on temperature, humidity, and seasonal patterns
- Real-time Monitoring: Continuous sensor data monitoring and analysis
- Comprehensive Logging: Detailed operation logging for trend analysis
For direct ESP32-to-ESP32 LoRa validation, use:
For the production path where the ESP32 sender transmits pressure packets and the Raspberry Pi receives them and appends them to a CSV, use:
Run on the Pi:
python3 src/controller/lora_csv_receiver.pyTo auto-start on boot, install:
sudo cp src/controller/wilo-lora-csv-receiver.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable wilo-lora-csv-receiver
sudo systemctl start wilo-lora-csv-receiverThe CSV is created automatically at:
logs/lora/esp32_pressure_packets.csv
- Fast-Forward Simulation: 30-day simulation with 60x speed
- Basic Simulation: Real-time simulation for testing
- Pattern Validation: Historical pattern validation through simulation
- Python 3.8 or higher
- Windows/Linux/macOS
- Minimum 4GB RAM
- 1GB free disk space
joblib>=1.3.0
numpy>=1.21.0
pandas>=1.3.0
scikit-learn>=1.0.0
-
Clone the repository
git clone <repository-url> cd "Wilo Water Pump Automation"
-
Create virtual environment
python -m venv env source env/bin/activate # Linux/macOS # or env\Scripts\activate # Windows
-
Install dependencies
pip install -r requirements.txt
-
Verify installation
python run.py --help
This is a multi-device system. Each component runs on a specific device โ read the table first, then jump to the section for the device you're setting up.
| # | Device | Component | Path | Command | Port |
|---|---|---|---|---|---|
| 1 | ESP32 (sender) | Sensor firmware | firmware/esp32_sender/ |
Flash via Arduino IDE | LoRa 433 MHz |
| 2 | Raspberry Pi | Pump controller (the brain) | src/controller/pump_controller.py |
python3 pump_controller.py |
โ (GPIO) |
| 3 | Raspberry Pi | Backend API + live dashboard | src/dashboard/server.py |
python3 server.py |
5050 |
| 4 | Any PC / the Pi | Frontend web dashboard (React) | dashboard/ |
bun dev / npm run dev |
8080 |
| 5 | Any PC (optional) | Terminal control panel (TUI) | tui/ |
npm run dev (over SSH) |
โ |
| 6 | Any PC (optional) | ML simulation / offline app | run.py |
python run.py |
โ |
Data flow: ESP32 โ (LoRa) โ Pi controller โ (status JSON + /api) โ Backend server.py โ (HTTP :5050) โ Frontend dashboard.
The backend (
server.py) both serves the built-in HTML dashboard on port5050and exposes the/api/*endpoints that the React frontend talks to. The React app (port8080) proxies/api,/latest,/streamto127.0.0.1:5050.
The ESP32 reads the PR12-P210 pressure sensor on the upper tank and broadcasts JSON packets over LoRa.
- Open
firmware/esp32_sender/esp32_sender.inoin the Arduino IDE (orarduino-cli). - Install the ESP32 board package and a LoRa (SX127x) library.
- Select your ESP32 board + port, then Upload.
- Wire per
docs/HARDWARE_GUIDE.md(sensor on GPIO34, LoRa on the SPI pins defined at the top of the sketch).
Test sketches also live in firmware/: esp32_relay_test/, esp32_lora_hello/, esp32_receiver/.
The Pi is the backend. It needs two processes running: the controller (owns the relay/GPIO) and the dashboard server (serves the UI + API).
Install dependencies (on the Pi):
cd Wilo-Water-Pump-Automation
python3 -m venv env && source env/bin/activate
# ML + core
pip install -r requirements.txt
# Pi hardware drivers (GPIO, SPI, ADC)
pip install -r src/controller/requirements.txt
# Backend web server
pip install flask pyseriala) Pump controller โ the decision engine + relay control:
cd src/controller
python3 pump_controller.py # LIVE โ drives real GPIO/relay
python3 pump_controller.py --dry-run # no GPIO, safe to test off-Pi
python3 pump_controller.py --verbose # extra debug loggingb) Backend / dashboard server โ Flask API + Server-Sent-Events feed:
cd src/dashboard
python3 server.py # serves on http://<pi-ip>:5050
python3 server.py --port 5050 --freshOpen http://<pi-ip>:5050 for the simple built-in dashboard, or point the React frontend (below) at it for the full UI.
Run on boot (systemd, recommended for production):
sudo cp src/controller/wilo-pump.service /etc/systemd/system/
sudo cp src/controller/wilo-lora-csv-receiver.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now wilo-pump wilo-lora-csv-receiver
# check status / logs
systemctl status wilo-pump
journalctl -u wilo-pump -f
โ ๏ธ The.servicefiles hardcodeUser=andWorkingDirectory=(pi/wilopi). Edit them to match your Pi's username and clone path before installing.
The full UI lives in dashboard/ (Vite + React + shadcn/ui). It can run on the Pi or any PC on the same network as the backend.
cd dashboard
bun install # or: npm install
bun dev # or: npm run dev โ http://localhost:8080-
In dev, Vite (port
8080) proxies/api,/stream,/latesttohttp://127.0.0.1:5050โ so run it on the Pi, or setVITE_API_BASE_URLto the Pi's address:VITE_API_BASE_URL="http://<pi-ip>:5050" bun dev -
For a production build:
bun run build # outputs to dashboard/dist bun run preview # or serve dist/ with any static host
Controls the Pi's live controller from your laptop without racing GPIO (it writes override commands the controller consumes):
cd tui
npm install
WILO_PI_HOST=wilopi.local WILO_PI_USER=wilopi npm run devRuns anywhere (no hardware needed) โ the predictive dashboard, simulations, and tests:
python run.py # predictive terminal app
python src/simulation/run_simulation.py # basic simulation
python src/simulation/simulation_30days.py # 30-day fast-forward simulation
python -m pytest tests/unit/ # testsTwo separate config files โ don't mix them up:
config/settings.pyโ the ML / simulation app (model paths, fallbacks, dashboard widths).src/controller/tank_config.pyโ the Pi hardware (GPIO pins, tank thresholds, LoRa/sensor calibration, safety guards).
- Date: YYYY-MM-DD format
- Hour: Hour of operation (0-23)
- Duration: Operation duration in minutes
- TopTankLevel: Water level percentage (0-100)
- Voltage: System voltage (V)
- Current: System current (A)
- Temperature: Ambient temperature (ยฐC)
- Humidity: Relative humidity (%)
- date: Operation date
- start_hour: Predicted start hour
- duration: Predicted duration
- sensor_data: Real-time sensor readings
The professional terminal dashboard provides:
- Color-coded status indicators
- Real-time sensor data display
- Historical analysis summaries
- Prediction results
- System alerts and notifications
- System Header: Application title and version
- Configuration Panel: Current settings and file paths
- Historical Analysis: 2-year pattern analysis
- Real-time Monitoring: Live sensor data and predictions
- Status Updates: Operation logs and alerts
- Start Hour Prediction: Predicts optimal pump start time
- Duration Prediction: Predicts optimal operation duration
- Pattern Recognition: Identifies historical usage patterns
- Historical Pattern Matching: Uses similar historical conditions
- Statistical Averages: Falls back to statistical patterns
- Default Parameters: Final fallback with safe defaults
# Run all unit tests
python -m pytest tests/unit/
# Run specific test
python tests/unit/test_analysis.py# Run integration tests
python -m pytest tests/integration/- Pump Operations:
logs/pump/pump_usage_log.csv - System Events: Console output with timestamps
- Simulation Results:
data/processed/simulation_results.csv
[INFO]: General information[SUCCESS]: Successful operations[WARNING]: Warning messages[ERROR]: Error conditions[CONFIG]: Configuration information[CONTROL]: Pump control operations[SENSORS]: Sensor data[PREDICT]: Prediction results
- Modify
src/utils/sensors.py - Update data structure in
src/utils/data_handler.py - Adjust prediction models if needed
- Create new module in
src/models/ - Implement prediction interface
- Update main application to use new algorithm
- Modify
src/dashboard/terminal_ui.py - Adjust colors, layouts, and formatting
- Add new display components
Model Loading Errors
- Ensure model files exist in
models/trained/ - Check file permissions
- Verify Python dependencies
Data Loading Issues
- Verify CSV file format
- Check file paths in configuration
- Ensure sufficient disk space
Permission Errors
- Run with appropriate permissions
- Check directory write access
- Verify log directory exists
Set LOG_LEVEL = 'DEBUG' in config/settings.py for detailed logging.
The Pi runs headless โ you control it from your laptop over SSH. In the field there's usually no router, so the simplest setup is a phone hotspot that both the Pi and the laptop join. Both devices must be on the same hotspot.
๐ฑ Phone Hotspot (e.g. "Sarthak-iPhone")
โ
โโโโโโดโโโโโโ
โ โ
๐ป Laptop ๐ Raspberry Pi
(headless โ SSH target)
- Raspberry Pi Imager: enable SSH (and set hostname/user/password) in the advanced options before flashing, or
- On a running Pi:
sudo raspi-configโ Interface Options โ SSH โ Enable, or - Headless: drop an empty file named
sshinto the boot partition of the SD card.
Default project user/hostname (from the systemd + TUI config): user wilopi, hostname wilopi.local.
Since the Pi is headless, it must already know the hotspot's Wi-Fi so it connects on boot. Set a fixed SSID + password on the phone hotspot, then tell the Pi about it:
-
Easiest (before flashing): in Raspberry Pi Imager advanced options, set the Wi-Fi SSID/password to your hotspot's name and password.
-
Headless SD-card edit: create
wpa_supplicant.confin the SD card's boot partition:country=IN ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="YOUR_HOTSPOT_NAME" psk="YOUR_HOTSPOT_PASSWORD" } -
On a Pi you can already reach:
sudo raspi-configโ System Options โ Wireless LAN, orsudo nmcli dev wifi connect "YOUR_HOTSPOT_NAME" password "YOUR_HOTSPOT_PASSWORD".
๐ก Turn the phone hotspot on first, then power the Pi โ it joins automatically. Keep the SSID/password the same every time so the Pi always reconnects.
Join the laptop to the same phone hotspot. Phone hotspots often don't resolve wilopi.local (no mDNS), so find the Pi's IP address:
-
On the phone: open Hotspot / Connected Devices โ it lists each connected device's name and IP (the Pi shows up as
wilopiorraspberrypi). -
From the laptop: scan the hotspot subnet (commonly
172.20.10.xon iPhone,192.168.43.xon Android):# try the hostname first (sometimes works) ping wilopi.local # otherwise scan the subnet (install nmap, or use arp) nmap -sn 172.20.10.0/24 arp -a | grep -iE 'b8:27:eb|dc:a6:32|e4:5f:01' # Raspberry Pi MAC prefixes
# macOS / Linux / Windows (PowerShell or Terminal)
# use the IP you found on the hotspot in step 3
ssh [email protected]
# โฆor by hostname if mDNS works on your hotspot
ssh [email protected]First connection asks to confirm the host fingerprint โ type yes. Then enter the Pi password.
Generate a key on the laptop (skip if you already have ~/.ssh/id_ed25519.pub), then copy it to the Pi:
ssh-keygen -t ed25519 # press Enter through the prompts
ssh-copy-id [email protected] # copies your public key to the PiNow ssh [email protected] logs in with no password. Add a shortcut in ~/.ssh/config on the laptop:
Host wilopi
HostName wilopi.local
User wilopi
โฆthen just ssh wilopi.
On the same hotspot you can usually just open the Pi's IP directly โ start server.py on the Pi and browse to http://172.20.10.5:5050 from the laptop.
If a port isn't reachable, tunnel it over SSH instead:
# Forward Pi's :5050 to laptop's localhost:5050
ssh -L 5050:localhost:5050 [email protected]
# Then open http://localhost:5050 in the laptop browserssh [email protected]
cd Wilo-Water-Pump-Automation
# start the pieces (see the "How to Run" section for details)
python3 src/controller/pump_controller.py &
python3 src/dashboard/server.py &
# or manage the systemd services
sudo systemctl status wilo-pump
journalctl -u wilo-pump -fTo control a running controller from the laptop without racing GPIO, use the terminal UI in
tui/(it sends override commands over SSH) โ setWILO_PI_HOST=wilopi.localandWILO_PI_USER=wilopi.
- Pi never appears on the hotspot โ the hotspot SSID/password changed, or the Pi booted before the hotspot was on. Fix the Wi-Fi in step 2 and power-cycle the Pi with the hotspot already running.
ssh: connect to host โฆ port 22: Connection refusedโ SSH isn't enabled on the Pi (see step 1).wilopi.localnot found โ phone hotspots usually don't do mDNS; use the raw IP from step 3 instead.Connection timed outโ Pi and laptop aren't on the same hotspot, or the Pi is off. Some hotspots also enable client isolation (AP isolation) which blocks device-to-device traffic โ turn it off in the phone's hotspot settings if available.REMOTE HOST IDENTIFICATION HAS CHANGEDโ the Pi was reimaged or got a new IP; clear the old key withssh-keygen -R 172.20.10.5.