This project implements a complete distributed MQTT system with Java and Python components communicating through an MQTT broker. The system includes multiple sensor publishers, subscribers, a dedicated MQTT broker, visualization features with data processing and logging, as well as comprehensive monitoring with Prometheus and Node Exporter.
- MQTT Broker: Eclipse Mosquitto broker for message routing
- Java Publisher: Multiple Java-based sine wave publishers
- Python Sine Wave Publisher: Python-based sine wave publisher
- Temperature Publisher: Simulates temperature measurements with daily cycles
- Humidity Publisher: Simulates humidity measurements with daily cycles
- Data Processor: Combines temperature and humidity to calculate heat index and dew point
- Python Subscriber: Python-based subscriber listening to all sensor topics
- MQTT Logger: Records all MQTT messages with rotating logs
- Grafana: Visualization tool for real-time sensor data
- MQTT CLI: Command-line interface for testing and debugging
- Prometheus: Time-series database for metric collection and monitoring
- Node Exporter: System metric collector for host-level monitoring
- MQTT Exporter: Metric exporter for Mosquitto broker statistics
- Grafana Dashboards: Pre-configured dashboards for system and MQTT metrics
- Docker and Docker Compose
- (Only for local development) Java 11+, Maven, Python 3.9+
- Multiple sensor types publishing data to configurable topics
- Subscribers receiving and processing data from multiple sources
- Data processing combining inputs to generate derived metrics
- Complete message logging with persistent storage
- Stop command handling for clean component shutdown
- Containerized deployment with Docker and Docker Compose
- Persistent storage for MQTT messages, logs, and Grafana dashboards
- Network isolation between components for increased security
- Visualization capabilities through Grafana
- Comprehensive monitoring with Prometheus, Node Exporter, and MQTT Exporter
- Test framework to verify system functionality
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Java Sine β β Python Sine β β Temperature β
β Publisher β β Publisher β β Publisher β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
β β β
β βΌ β
β βββββββββββββββββββ β
ββββββββββββββββΊβ MQTT Broker ββββββββββββββ
β (Mosquitto) β
ββββββββββββββββΊβ ββββββββββββββ
β βββββββββββββββββββ β
β β² β
β β β
β β β
ββββββββββΌβββββββββββ ββββββββΌββββββββββββ βββββββΌββββββββββββ
β Subscriber β β Data Processor β β Humidity β
β β β β β Publisher β
βββββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
β
βββββββΌβββββββββββ βββββββββββββββββββ
β MQTT Logger β β Grafana β
β β β Visualization β
ββββββββββββββββββ βββββββββββββββββββ
β²
β
ββββββββββββββββββ β
β MQTT Exporter βββββββββββββββββ€
ββββββββββββββββββ β
β² β
β β
ββββββββββββββββ΄ββββββββββ β
β β β
ββββββββΌβββββββ ββββββββΌβββββββ β
β Prometheus βββββββββββΊNode Exporterββββββββββ
βββββββββββββββ βββββββββββββββ
.
βββ broker/ # MQTT broker configuration
β βββ Dockerfile
β βββ mosquitto.conf
βββ python-publisher/ # Python sine wave publisher
β βββ Dockerfile
β βββ publisher.py
β βββ requirements.txt
βββ python-subscriber/ # Python subscriber component
β βββ Dockerfile
β βββ subscriber.py
β βββ requirements.txt
βββ python-temp-publisher/ # Temperature publisher
β βββ Dockerfile
β βββ temp_publisher.py
β βββ requirements.txt
βββ python-humidity-publisher/ # Humidity publisher
β βββ Dockerfile
β βββ humidity_publisher.py
β βββ requirements.txt
βββ python-processor/ # Data processor
β βββ Dockerfile
β βββ processor.py
β βββ requirements.txt
βββ python-logger/ # MQTT message logger
β βββ Dockerfile
β βββ logger.py
β βββ requirements.txt
βββ mqtt-exporter/ # MQTT metrics exporter
β βββ Dockerfile
βββ grafana/ # Grafana configuration
β βββ provisioning/
β βββ datasources/ # Grafana data sources configuration
β βββ prometheus.yml # Prometheus data source configuration
βββ src/ # Java publisher source code
βββ Dockerfile # Java publisher Dockerfile
βββ pom.xml # Maven configuration
βββ docker-compose.yml # Docker Compose configuration
βββ prometheus.yml # Prometheus configuration
βββ test-system.sh # System test script
βββ README.md
-
Sensor Data Publishers:
- Java publisher generates sine wave data
- Python sine publisher generates sine wave data
- Temperature publisher simulates temperature measurements with daily cycles
- Humidity publisher simulates humidity measurements with daily cycles
-
Message Broker:
- All messages flow through the MQTT broker
- Mosquitto handles message routing between components
-
Data Consumers:
- Subscriber listens to sensor topics and displays data
- Data processor combines temperature and humidity data to calculate:
- Heat index (perceived temperature)
- Dew point
- Logger records all MQTT messages with timestamps
-
Visualization:
- Grafana connects to the MQTT broker to display real-time data
- Multiple dashboards can be created for different data types
-
Monitoring:
- Node Exporter collects system metrics (CPU, memory, network, disk)
- MQTT Exporter collects Mosquitto broker metrics
- Prometheus stores all metrics with timestamps
- Grafana visualizes system and MQTT metrics
The entire system can be built and started with Docker Compose:
# Build and start all services
docker-compose up -d --build
# Check container status
docker-compose ps
# Stop all services
docker-compose down
# Stop all services and delete volumes
docker-compose down -vA test script is provided to verify if the system is functioning correctly:
# Ensure the script is executable
chmod +x test-system.sh
# Run system tests
./test-system.shThe test script checks:
- All services are running
- MQTT broker connectivity
- Sensor data publishing
- Grafana availability
- Logger functionality
- Stop command handling
# View logs for a specific service
docker-compose logs java-publisher-1
docker-compose logs python-subscriber
# Restart a specific service
docker-compose restart python-publisher
# Scale a service (add more instances)
docker-compose up -d --scale python-publisher=3- MQTT Broker: Available on port 1883 for MQTT and 9001 for WebSockets
- Grafana: Available at http://localhost:3000 (default credentials: admin/admin)
- Prometheus: Available at http://localhost:9090
- Node Exporter Metrics: Available at http://localhost:9100/metrics
- MQTT Exporter Metrics: Available at http://localhost:9234/metrics
- MQTT CLI: Access via
docker-compose exec mqtt-cli sh
# Connect to the CLI container
docker-compose exec mqtt-cli sh
# Subscribe to all sensor topics
mqtt sub -h mqtt-broker -t "sensors/#" -v
# Send a message to a specific topic
mqtt pub -h mqtt-broker -t "sensors/test" -m "0.123456"
# Send stop command to stop a specific publisher
mqtt pub -h mqtt-broker -t "feedback/java1" -m "stop"You can also connect to the broker from outside Docker using tools like MQTT Explorer, MQTT.fx, or Mosquitto clients:
# Subscribe using mosquitto_sub
mosquitto_sub -h localhost -t "sensors/#" -v
# Publish using mosquitto_pub
mosquitto_pub -h localhost -t "feedback/python1" -m "stop"- Access Grafana at http://localhost:3000
- Log in with default credentials (admin/admin)
- Add MQTT data source:
- Configuration > Data Sources > Add data source
- Select "MQTT" (install plugin if needed)
- Set broker URL to "mqtt-broker:1883" or "localhost:1883" (for external access)
- Create a new dashboard:
- Add panels to visualize sensor data
- Configure subscriptions for "sensors/+"
- Set refresh rate for real-time updates
- Access Prometheus at http://localhost:9090
- View targets at http://localhost:9090/targets to ensure all exporters are active
- Use PromQL for direct queries
- Use pre-configured Grafana dashboards for system monitoring:
- Node Exporter dashboard for system metrics
- MQTT dashboard for broker metrics
- Create custom dashboards:
- Combine system metrics with MQTT metrics
- Set up alerts for critical thresholds
- Configure annotations for important events
The system includes persistent volumes for:
mosquitto-data: Stores persistent MQTT messagesmosquitto-log: Stores MQTT broker logsgrafana-storage: Stores Grafana dashboards and configurationsmqtt-logs: Stores MQTT message logsprometheus-data: Stores Prometheus time-series data
These volumes ensure data persists even when containers are restarted.
The default configuration allows anonymous access to the MQTT broker, which is suitable for development but not for production. For production use:
- Configure MQTT authentication (edit mosquitto.conf)
- Use secure connections (TLS)
- Apply network isolation for containers
- Update Docker security options
- Set secure passwords for Grafana and Prometheus
- Restrict access to monitoring endpoints
This architecture can be scaled in various ways:
-
Horizontal Scaling: Add more publishers or subscribers by scaling services
docker-compose up -d --scale python-publisher=3
-
Broker Clustering: Set up multiple MQTT brokers in a cluster for high availability
-
Load Balancing: Place load balancers in front of critical services
-
Persistent Storage: Use external databases for long-term data storage
# Build with Maven
mvn clean package
# Run locally
java -jar target/mqtt-sinus-publisher-1.0-SNAPSHOT-jar-with-dependencies.jar sensors/local feedback/local# Install dependencies
pip install -r python-publisher/requirements.txt
# Run publisher
python python-publisher/publisher.py
# Run subscriber
python python-subscriber/subscriber.pyAll components support configuration through environment variables:
MQTT_BROKER: MQTT broker address (default: mqtt-broker or localhost:1883)MQTT_PUB_TOPIC: Topic to publish toMQTT_SUB_TOPIC: Topic to subscribe toMQTT_FEEDBACK_TOPIC: Topic for control commandsMQTT_CLIENT_ID: Unique client identifier
- Prometheus and exporters use their respective configuration files for settings
-
Container won't start:
- Check logs with
docker-compose logs <service-name> - Verify network connectivity between containers
- Ensure required volumes exist
- Check logs with
-
MQTT communication issues:
- Check if the broker is running with
docker-compose ps - Check broker logs with
docker-compose logs mqtt-broker - Test connectivity with MQTT CLI
- Check if the broker is running with
-
Monitoring issues:
- Check Prometheus targets at http://localhost:9090/targets
- Verify exporter endpoints are reachable
- Check Prometheus logs with
docker-compose logs prometheus
Contributions to MQTT-ScaleNet are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch
- Add your changes
- Test thoroughly
- Submit a pull request
This project is licensed under the MIT License.
- Eclipse Mosquitto for the MQTT broker
- Prometheus for monitoring
- Grafana for visualization
- All Docker image maintainers