Water Potability Backend is a prototype system designed for real-time water quality monitoring and potability prediction. The system consumes water sensor data from IoT devices via MQTT, processes the data, sends it to a Machine Learning model for potability inference via gRPC, and stores the results in InfluxDB for visualization.
This backend service acts as a bridge between IoT water sensors and ML inference services, providing a pipeline for water quality monitoring.
graph LR
A[IoT Water Sensors] -->|MQTT| B(Water Potability Backend)
B -->|gRPC| C[ML Inference Service]
C --> B
B --> D[InfluxDB]
E[Grafana] --> D
The system components:
- IoT Sensors: Collect water quality metrics (pH, turbidity, total dissolved solids)
- MQTT Broker: Message queue for sensor data streaming
- Backend Service: Core service that processes sensor data
- ML Service: Machine learning model for water potability prediction
- InfluxDB: Time-series database for storing sensor data and predictions
- Grafana: Visualization dashboard
- Real-time MQTT message consumption from water quality sensors
- AES-256 decryption of sensor payloads
- gRPC communication with ML inference service
- Water potability prediction storage in InfluxDB
- Structured logging with zerolog
- Docker containerization support
- Configuration via JSON files or environment variables
- Go 1.25+
- Docker (for containerization)
- MQTT Broker (e.g., Mosquitto, EMQX)
- InfluxDB 2.x
- gRPC-based ML Inference Service
- Grafana (for visualization)
The service can be configured using either a JSON configuration file or environment variables.
-
Clone the repository:
git clone https://github.com/lab-icn/water-potability-sensor-service.git cd water-potability-sensor-service -
Build the service:
go build -o water-potability-backend cmd/subscriber/main.go
-
Run the service:
CONFIG_FILEPATH=config.json ./water-potability-backend
- IoT sensors publish water quality data to MQTT topics
- Backend service subscribes to configured MQTT topics
- Incoming messages are decrypted using AES-256
- Data is parsed into
WaterPotabilitystructures:- pH level
- Total Dissolved Solids (TDS)
- Turbidity
- Data is sent to ML service via gRPC for potability prediction
- Prediction results are stored in InfluxDB with the original metrics
- Grafana dashboard visualizes the data for monitoring
type WaterPotability struct {
Node string
PH float64 `json:"ph"`
TotalDissolvedSolids float64 `json:"totalDissolvedSolids"`
Turbidity float64 `json:"turbidity"`
}type WaterPotabilityWithPrediction struct {
Node string `json:"node"`
PH float64 `json:"ph"`
TotalDissolvedSolids float64 `json:"totalDissolvedSolids"`
Turbidity float64 `json:"turbidity"`
Prediction float64 `json:"prediction"`
}.
βββ cmd/
β βββ grpc/ # gRPC server implementation
β βββ subscriber/ # Main subscriber service
βββ internal/
β βββ aes256/ # AES encryption/decryption utilities
β βββ config/ # Configuration structures
β βββ domain/ # Domain models
β βββ grpc/ # gRPC client utilities
β βββ influxdb/ # InfluxDB client utilities
β βββ interface/ # Interfaces and delivery mechanisms
β βββ mqtt/ # MQTT client and subscription handling
β βββ repository/ # Data access layer
β βββ service/ # Business logic layer
βββ proto/ # Protocol buffer definitions
βββ deployment/ # Docker Compose configurations
βββ tmp/ # Temporary files
The service uses gRPC to communicate with the ML inference service:
syntax = "proto3";
package water_potability;
service WaterPotabilityService {
rpc PredictWaterPotability (PredictWaterPotabilityRequest) returns (PredictWaterPotabilityResponse);
}
message PredictWaterPotabilityRequest {
double ph = 1;
double totalDissolveSolids = 2;
double turbidity = 3;
}
message PredictWaterPotabilityResponse {
double prediction = 1;
}The service uses zerolog for structured logging. Logs include:
- Service startup and shutdown events
- MQTT connection status
- gRPC communication status
- InfluxDB write operations
- Error conditions and debugging information
For production deployment:
- Prepare your configuration file
- Build the Docker image
- Deploy to your VM with Docker
- Configure your MQTT broker, InfluxDB, and ML service
- Start the service with proper resource allocation
This is an internal prototype project. Contributions are managed internally by the development team.
This project is licensed under the MIT License - see the LICENSE file for details.