System Overview
PumpPilot is a centralized motor control platform with web dashboard, mobile app, API backend, MongoDB storage, and ESP32 edge devices.
Example: User starts motor from dashboard -> API updates state -> ESP32 polls and runs motor.
PumpPilot
Quick technical documentation in simple language.
PumpPilot is a centralized motor control platform with web dashboard, mobile app, API backend, MongoDB storage, and ESP32 edge devices.
Example: User starts motor from dashboard -> API updates state -> ESP32 polls and runs motor.
One backend (Next.js App Router API) serves two clients (Web + React Native). MongoDB stores users, queue, wallet, logs, and system state.
Flow: Web/Mobile -> /api/* -> Queue/Timer engines -> MongoDB -> ESP32 /api/esp32/poll
Each Admin is a tenant. Users are strictly bound to adminId. Queue, minute requests, and runtime decisions are tenant-scoped.
Admin Rahim users cannot affect Admin Q users or queue.
Master Admin: system-wide control. Admin: tenant control. User: motor operation only. Mobile and web both enforce role checks.
User cannot call admin recharge API; request returns 403.
UI layer (pages/screens), API routes, engine layer (queue/timer/loadshedding), and model layer (mongoose schemas) are separated for maintainability.
Minutes are deducted on actual usage. Early stop returns unused minutes. Recharge updates availableMinutes with audit logs.
Set 10m, stop at 6m -> used 6, refund 4.
Per admin only one RUNNING entry is allowed. New requests become WAITING with position. Next user auto-starts when current ends.
User A RUNNING, User B starts -> WAITING #1.
Load shedding forces HOLD state, pauses decrement, and keeps remaining time intact for safe resume.
RUNNING 8m -> HOLD due to power loss -> resume from 8m on recovery.
ESP32 polling syncs physical state with backend state every 3-5 seconds. Local load-shedding pin can also be sent to server.
Firmware has WiFi/GPRS connect, poll loop, JSON parser, and motor GPIO decision logic using motorStatus + loadShedding.
RUNNING + no LS => GPIO HIGH, otherwise GPIO LOW.
Use relay/contactor isolation, proper fuse/MCB, optocoupler path, and interlock rule (single active motor per admin) for safety.
| Component | Pin/Terminal | Connect To | Purpose |
|---|---|---|---|
| ESP32 | GPIO5 | Optocoupler LED (+) via resistor | Motor ON/OFF signal |
| ESP32 | GND | Optocoupler LED (-) | Signal ground |
| Optocoupler Output | Collector | Transistor Base (via 1k resistor) | Isolation switching |
| Optocoupler Output | Emitter | GND | Ground reference |
| Transistor | Collector | Relay Coil (-) | Relay drive |
| Transistor | Emitter | GND | Ground |
| Relay Coil (+) | +12V | External power | Relay supply |
| Relay COM | AC Line | Motor input | Switching line |
| Relay NO | Motor Phase | Motor power control | - |
| ESP32 | GPIO18 (example) | Load Shedding input | Grid status detect |
Core collections: admins, users, queues, usage_history, minute_requests, mobile_sessions. Indexed by adminId/userId/date/position for fast reads.
Queue compound index adminId+position speeds waiting list reads.
REST endpoints are role-scoped: auth, motor, queue, admin actions, master actions, mobile endpoints, and ESP32 poll endpoint.
POST /api/motor/start, POST /api/motor/stop, GET /api/esp32/poll?adminId=...
Queue ordering and state transitions are controlled server-side. Optimistic UI is corrected by realtime polling to prevent drift.
Password hashing, JWT/session auth, role guards, suspend workflow, and protected mobile bearer token endpoints reduce abuse risk.
API routes use try/catch and explicit JSON errors. On failures, motor-safe fallback is OFF/HOLD depending on state.
ESP32 non-200 response => keep safe OFF output.
Tenant-scoped queries, indexed reads, lightweight polling payloads, and decoupled engines support scaling to 100+ motors.
Web/API deploy on Vercel, MongoDB Atlas as database, ESP32 devices configured with hosted API URL, mobile app via Expo/EAS.
Validate env vars, auth roles, suspend behavior, queue correctness, hold/resume, refund correctness, and ESP32 command response.
Planned: websocket/MQTT realtime, richer analytics, alerting, backup/restore tooling, and stronger device certificate security.