Spaces:
Running
Running
| title: Water Quality Monitor | |
| emoji: π§ | |
| colorFrom: blue | |
| colorTo: green | |
| sdk: docker | |
| app_port: 3000 | |
| # 1Water Quality Monitoring System | |
| A full-stack Next.js application for monitoring and visualizing water quality measurements from geographically distributed sensors worldwide. | |
| ## π Features | |
| - **Real-time Monitoring**: MQTT-based ingestion of sensor data | |
| - **Interactive Global Map**: Visualize sensor locations with Leaflet.js | |
| - **Historical Analysis**: Time-series charts for water quality metrics | |
| - **Responsive Dashboard**: Detailed sensor views with customizable time ranges | |
| - **Scalable Architecture**: MongoDB storage with efficient indexing | |
| - **RESTful APIs**: Backend endpoints for data retrieval | |
| ## π Water Quality Metrics | |
| - **pH**: Acidity or alkalinity of water (0-14) | |
| - **Turbidity**: Water clarity measurement (NTU) | |
| - **Temperature**: Water temperature (Β°C) | |
| - **Hardness**: Mineral concentration in water (mg/L) | |
| ## π οΈ Tech Stack | |
| - **Frontend**: Next.js 14, React, TypeScript, Tailwind CSS | |
| - **Backend**: Next.js API Routes, Node.js | |
| - **Database**: MongoDB | |
| - **Mapping**: Leaflet.js, React-Leaflet | |
| - **Charts**: Recharts | |
| - **Messaging**: MQTT (HiveMQ) | |
| ## π Prerequisites | |
| - Node.js 18+ and npm | |
| - MongoDB (local or cloud instance) | |
| - MQTT Broker (HiveMQ public broker by default) | |
| ## π Installation | |
| 1. **Clone the repository** | |
| ```bash | |
| cd water-quality-monitoring | |
| ``` | |
| 2. **Install dependencies** | |
| ```bash | |
| npm install | |
| ``` | |
| 3. **Configure environment variables** | |
| Edit `.env.local` with your configuration: | |
| ```env | |
| MONGODB_URI=mongodb://localhost:27017/water_quality | |
| MQTT_BROKER=mqtt://broker.hivemq.com:1883 | |
| MQTT_TOPIC=sensors/water-quality/# | |
| ``` | |
| 4. **Start MongoDB** | |
| If running locally: | |
| ```bash | |
| # macOS with Homebrew | |
| brew services start mongodb-community | |
| # Linux | |
| sudo systemctl start mongod | |
| # Windows | |
| net start MongoDB | |
| ``` | |
| 5. **Seed the database with sample data** | |
| ```bash | |
| node scripts/seedDatabase.js | |
| ``` | |
| ## π Running the Application | |
| ### Development Mode | |
| Start the Next.js development server: | |
| ```bash | |
| npm run dev | |
| ``` | |
| The application will be available at `http://localhost:3000` | |
| ### MQTT Ingestion Service | |
| In a separate terminal, start the MQTT ingestion service: | |
| ```bash | |
| npm run mqtt:start | |
| ``` | |
| This service subscribes to MQTT topics and stores incoming sensor readings in MongoDB. | |
| ### Sensor Simulator (Optional) | |
| To simulate sensor data for testing: | |
| ```bash | |
| node scripts/simulateSensors.js | |
| ``` | |
| This will publish random sensor readings to the MQTT broker. | |
| ## π Project Structure | |
| ``` | |
| water-quality-monitoring/ | |
| βββ app/ # Next.js app directory | |
| β βββ api/ # API routes | |
| β β βββ sensors/ # Sensor endpoints | |
| β β βββ readings/ # Readings endpoints | |
| β βββ sensor/ # Dynamic sensor routes | |
| β β βββ [sensorId]/ # Individual sensor pages | |
| β β βββ page.tsx # Sensor detail page | |
| β β βββ not-found.tsx # 404 page | |
| β βββ globals.css # Global styles | |
| β βββ layout.tsx # Root layout | |
| β βββ page.tsx # Home page (map view) | |
| βββ components/ # React components | |
| β βββ SensorMap.tsx # Main map interface | |
| β βββ MapView.tsx # Leaflet map component | |
| β βββ SensorDashboard.tsx # Charts and statistics | |
| β βββ SensorDetailPage.tsx # Full sensor page | |
| β βββ MiniMap.tsx # Small location map | |
| βββ lib/ # Utilities and types | |
| β βββ mongodb.ts # Database connection | |
| β βββ types.ts # TypeScript types | |
| βββ services/ # Background services | |
| β βββ mqttIngestion.js # MQTT subscriber | |
| βββ scripts/ # Utility scripts | |
| β βββ seedDatabase.js # Database seeding | |
| β βββ simulateSensors.js # Sensor simulation | |
| βββ .env.local # Environment variables | |
| βββ package.json # Dependencies | |
| βββ tsconfig.json # TypeScript config | |
| βββ next.config.js # Next.js config | |
| ``` | |
| ## π API Endpoints | |
| ### Get All Sensors | |
| ``` | |
| GET /api/sensors | |
| ``` | |
| Returns all sensors with their latest readings. | |
| ### Create Sensor | |
| ``` | |
| POST /api/sensors | |
| Content-Type: application/json | |
| { | |
| "sensorId": "SENSOR_XXX_001", | |
| "latitude": 40.7128, | |
| "longitude": -74.0060, | |
| "locationName": "Location Name" | |
| } | |
| ``` | |
| ### Get Sensor Readings | |
| ``` | |
| GET /api/readings?sensorId=SENSOR_XXX_001&timeRange=1d | |
| ``` | |
| Query Parameters: | |
| - `sensorId` (required): Sensor identifier | |
| - `timeRange`: `1h`, `1d`, `1w`, `1m` | |
| - `startDate`: ISO date string (for custom range) | |
| - `endDate`: ISO date string (for custom range) | |
| ## π Database Schema | |
| ### Sensors Collection | |
| ```javascript | |
| { | |
| sensorId: String, // Unique identifier | |
| latitude: Number, // Geographic latitude | |
| longitude: Number, // Geographic longitude | |
| locationName: String, // Optional location name | |
| installedAt: Date // Installation timestamp | |
| } | |
| ``` | |
| ### Readings Collection | |
| ```javascript | |
| { | |
| sensorId: String, // Reference to sensor | |
| timestamp: Date, // Reading timestamp | |
| pH: Number, // pH level (0-14) | |
| turbidity: Number, // Turbidity (NTU) | |
| temperature: Number, // Temperature (Β°C) | |
| hardness: Number // Hardness (mg/L) | |
| } | |
| ``` | |
| ## π― Usage | |
| ### Routes | |
| - **`/`** - Main map view with all sensors | |
| - **`/sensor/[sensorId]`** - Individual sensor dashboard page | |
| - Example: `/sensor/SENSOR_NYC_001` | |
| - Direct link to specific sensor with full dashboard | |
| - Bookmarkable and shareable URLs | |
| ### Navigation | |
| 1. **View All Sensors**: Visit the home page to see the interactive map | |
| 2. **Quick Preview**: Hover over any sensor marker for current readings | |
| 3. **Overlay Dashboard**: Click a sensor marker to open side panel with charts | |
| 4. **Full Dashboard**: Click "Open Full Dashboard" or visit `/sensor/[sensorId]` directly | |
| 5. **Return to Map**: Click the back arrow on any sensor page | |
| ### Features by View | |
| **Map View (`/`):** | |
| - Interactive global map | |
| - All sensors displayed | |
| - Quick hover previews | |
| - Click for overlay dashboard | |
| **Sensor Detail Page (`/sensor/[sensorId]`):** | |
| - Dedicated page for each sensor | |
| - Current readings with icons | |
| - Mini location map | |
| - Full historical charts | |
| - Auto-refresh every 30 seconds | |
| - Shareable URL | |
| ## π Data Validation | |
| The MQTT ingestion service validates all incoming readings: | |
| - All required fields must be present | |
| - pH: 0-14 range | |
| - Turbidity: Non-negative | |
| - Temperature: -50Β°C to 100Β°C | |
| - Hardness: Non-negative | |
| Invalid messages are logged and rejected. | |
| ## π Performance Optimizations | |
| - **Database Indexes**: Optimized queries with compound indexes | |
| - **Caching**: Connection pooling for MongoDB | |
| - **Efficient Querying**: Time-based filtering with indexed timestamps | |
| - **Lazy Loading**: Dynamic imports for map components | |
| ## π§ Future Enhancements | |
| - Water quality safety alerts | |
| - Sensor offline detection | |
| - Data export functionality (CSV, PDF) | |
| - Predictive analytics using historical trends | |
| - Real-time WebSocket streaming | |
| - User authentication and access control | |
| - Mobile app integration | |
| - Alert notifications via email/SMS | |
| ## π Troubleshooting | |
| ### MongoDB Connection Issues | |
| ```bash | |
| # Check MongoDB status | |
| mongosh --eval "db.runCommand({ ping: 1 })" | |
| ``` | |
| ### MQTT Connection Issues | |
| - Verify broker URL is correct | |
| - Check network connectivity | |
| - Ensure no firewall blocking port 1883 | |
| ### Port Already in Use | |
| ```bash | |
| # Kill process on port 3000 | |
| lsof -ti:3000 | xargs kill -9 | |
| ``` | |
| ## π Environment Variables | |
| | Variable | Description | Default | | |
| |----------|-------------|---------| | |
| | `MONGODB_URI` | MongoDB connection string | `mongodb://localhost:27017/water_quality` | | |
| | `MQTT_BROKER` | MQTT broker URL | `mqtt://broker.hivemq.com:1883` | | |
| | `MQTT_TOPIC` | MQTT topic pattern | `sensors/water-quality/#` | | |
| ## π License | |
| This project is licensed under the MIT License. | |
| ## π€ Contributing | |
| Contributions are welcome! Please feel free to submit a Pull Request. | |
| ## π§ Support | |
| For issues and questions, please open an issue on the repository. | |
| --- | |
| Built with β€οΈ using Next.js and MongoDB | |