A Flask-based REST API for real-time water quality monitoring, alerting, and prediction. Integrates MongoDB for data storage, Firebase Cloud Messaging and Slack for notifications, and a machine learning model for water safety prediction.
- User Registration & Login: Register and authenticate users with FCM tokens for push notifications.
- Sensor Data Ingestion: Accepts water quality sensor data (pH, turbidity, temperature, TDS).
- Data Storage: Persists all data in MongoDB.
- Real-Time Alerts: Sends alerts via Firebase and Slack if water quality is unsafe.
- Machine Learning Prediction: Predicts water safety using a Random Forest classifier.
- RESTful API: Provides endpoints for data submission, retrieval, and prediction.
-
Clone the repository and install dependencies:
pip install -r requirements.txt
-
Configure environment variables in a
.env
file:MONGO_URI=your_mongodb_uri FIREBASE_PROJECT_ID=... FIREBASE_PRIVATE_KEY_ID=... FIREBASE_PRIVATE_KEY=... FIREBASE_CLIENT_EMAIL=... FIREBASE_CLIENT_ID=... FIREBASE_AUTH_URI=... FIREBASE_TOKEN_URI=... FIREBASE_AUTH_PROVIDER_CERT_URL=... FIREBASE_CLIENT_CERT_URL=... FIREBASE_UNIVERSE_DOMAIN=... SLACK_WEBHOOK_URL=your_slack_webhook_url PORT=5000
-
Run the application:
python app.py
- Description: Health check endpoint.
- Response:
"Welcome to the Water Quality Monitoring API!"
-
Description: Register a new user.
-
Request Body:
{ "username": "user1", "password": "pass", "fcmtoken": "firebase_token" }
-
Response: User info or error.
-
Description: Authenticate a user.
-
Request Body:
{ "username": "user1", "password": "pass" }
-
Response: User info or error.
-
Description: Submit new sensor data.
-
Request Body:
{ "pH": 7.2, "turbidity": 2.5, "temp": 25.0, "tds": 500 }
-
Response: Status, prediction, confidence, and alerts.
- Description: Retrieve the 50 most recent sensor data records.
- Response: List of sensor data.
- Description: Get prediction and alerts for the latest sensor data.
- Response: Water quality status, confidence, and alerts.
- Algorithm: Random Forest Classifier (
sklearn
) - Features: pH, turbidity, temperature, TDS
- Label: Unsafe (1) if any parameter is out of WHO range, else Safe (0)
- Training: Retrained every 10 new records (if at least 10 records exist)
- Prediction: Returns "Safe" or "Unsafe" with confidence score
- Firebase: Sends push notifications to the first registered user's FCM token.
- Slack: Sends alerts to a configured Slack channel via webhook.
- Trigger: Alerts are sent if any parameter is out of the safe range.
Parameter | Safe Range |
---|---|
pH | 6.5 - 8.5 |
Turbidity | ≤ 5.0 NTU |
Temperature | ≤ 30.0 °C |
TDS | ≤ 1000.0 ppm |
- Flask App Initialization: Sets up Flask, logging, and loads environment variables.
- MongoDB Connection: Connects to MongoDB (cloud/local) and initializes collections.
- Firebase Initialization: Loads credentials and initializes Firebase Admin SDK.
- Notification Functions: Functions to send alerts via Firebase and Slack.
- Water Quality Check: Checks sensor data against WHO standards and triggers notifications.
- Model Training: Trains a Random Forest model on historical data.
- API Endpoints: Implements all REST endpoints for registration, login, data submission, retrieval, and prediction.
- Shutdown Hook: Closes MongoDB connection gracefully on shutdown.
- The application registers a shutdown hook to close the MongoDB connection when the app exits.
This project is licensed under the MIT License.
Note:
- Ensure all environment variables are set correctly for MongoDB, Firebase, and Slack integration.
- The API is designed for demonstration and prototyping; for production, enhance security (e.g., password hashing, authentication, validation).