This project is a middleware solution designed for a smart bus monitoring system. The middleware interacts with multiple sensor emulators, processes real-time data, and stores it in MongoDB. It also integrates monitoring tools like Prometheus and Grafana for analytics and alerting. Alerts for high unvalidated passenger counts are sent via Discord webhook notifications to a dedicated server. This guide provides step-by-step instructions to set up and run the system using Docker.
- Prerequisites
- System Architecture
- Setup Instructions
- Key Components
- Testing and Validation
- Monitoring and Alerts
- Troubleshooting
- Team Contributions
Ensure you have the following installed on your system:
- Docker
- Docker Compose
- Python 3.8 or above (for local development and running standalone scripts)
The system consists of the following components:
- Sensor Emulators: Simulate GPS, environmental, and passenger count data from multiple buses.
- Middleware: Processes incoming data, stores it in MongoDB, and exposes APIs for interaction. Also sends high unvalidated passenger alerts to Discord.
- MongoDB: Stores all sensor data, alerts, and logs.
- Prometheus: Monitors real-time metrics, which are visualized in Grafana dashboards.
- Grafana: Visualizes metrics for system monitoring and analysis.
- Locust: Conducts performance testing for the system.
A system architecture diagram visually representing the interaction between components is essential. It should include:
- Sensor emulators (GPS, environmental, passenger count)
- MQTT broker for communication
- Middleware (data processing and Prometheus metrics exposure)
- MongoDB for storage
- Prometheus and Grafana for monitoring
- Discord webhook for alerts
# Clone the repository
git clone https://github.com/your-repo/smart-bus-monitoring.git
cd smart-bus-monitoringEnsure all required Docker configuration files are present:
docker-compose.ymlDockerfilefor middleware and emulatorsprometheus.ymlfor Prometheus configurationmosquitto.conffor MQTT broker configuration
Modify configurations as needed:
- Update environment variables in
config.pyfor database and broker settings. - Set up the Prometheus scrape targets in
prometheus.yml.
Build and start the system using Docker Compose:
# Build the Docker containers
docker-compose build
# Start the containers
docker-compose up -dVerify that the containers are running:
docker ps- Base URL:
http://localhost:5000 - Endpoints:
GET /vehicles/<vehicle_id>: Retrieve vehicle data.POST /passenger_counts: Insert passenger count data.POST /environment_data: Insert environmental data.POST /gps_data: Insert GPS data.
- Prometheus:
http://localhost:9090 - Grafana:
http://localhost:3000- Default username:
admin - Default password:
admin
- Default username:
- Locust Web Interface:
http://localhost:8089- Use this interface to configure and run load tests.
The middleware processes real-time data, stores it in MongoDB, updates Prometheus metrics, and sends alerts to Discord via webhook.
Code Snippet (Alert Logic Example):
if unvalidated_passengers > 10:
alert_message = {
"content": f"High unvalidated passenger count ({unvalidated_passengers}) on {vehicle_id}."
}
requests.post(DISCORD_WEBHOOK_URL, json=alert_message)Simulate GPS, environmental, and passenger count data for multiple buses.
Code Snippet (Simulating Bus Data):
def generate_gps_data(self):
self.gps["latitude"] += random.uniform(-0.001, 0.001)
self.gps["longitude"] += random.uniform(-0.001, 0.001)
return f"Latitude: {self.gps['latitude']}, Longitude: {self.gps['longitude']}"Stores data in the following collections:
passenger_counts: Passenger count data.environment_data: Environmental sensor data.gps_data: GPS location data.alerts: High unvalidated passenger alerts.
- Prometheus: Scrapes metrics exposed by the middleware.
- Grafana: Visualizes data and creates dashboards.
- Start the Locust container:
docker-compose up locust -d- Open the Locust Web Interface at
http://localhost:8089. - Configure the number of users and spawn rate, then start the test.
To view real-time logs:
docker logs -f <middleware-container-id>- Verify metrics using queries like
unvalidated_passengers_countandoccupancy_percentage.
- Import a pre-configured dashboard or create custom visualizations.
- Visualize metrics like passenger count, GPS locations, and environmental conditions.
High unvalidated passenger counts (>10) trigger alerts sent to a Discord channel:
- Example alert: "High unvalidated passenger count (12) on bus_5."
- Prometheus Not Scraping Data: Verify
prometheus.ymlconfiguration and ensure the middleware is exposing metrics onhttp://localhost:8000/metrics. - Grafana Dashboard Empty: Check Prometheus scrape targets and ensure data is being collected.
- Discord Alerts Not Working: Ensure the webhook URL is correctly set in the middleware.
- Containers Not Starting: Verify Docker Compose logs for errors and resolve any missing dependencies.
- Middleware Development: Real-time data processing and alerting logic.
- Sensor Emulators: Simulated multiple buses with GPS, environmental, and passenger count data.
- Monitoring Tools: Integrated Prometheus and Grafana for analytics.
- Alerts System: Configured Discord webhooks for high unvalidated passenger count alerts.
This project was developed by Felipe Meirelles Carvalho Orlando and Abbas Alawieh in October 2024.
This concludes the setup and usage guide for the smart bus monitoring system. For further assistance, refer to the documentation or contact the development team.