286ef3e7fea9660fc15421193d7402a84c6d489c
Modbus to MQTT Bridge Service
A Python service that reads temperature and humidity data from a Modbus TCP server and publishes the data to an MQTT broker.
📁 Project Structure
POE/
├── config.py # Configuration constants
├── sensor_bridge.py # Core logic for Modbus and MQTT operations
├── main.py # Entry point
├── requirements.txt # Python dependencies
├── README.md # This file
├── poe.py # Original single file (backup)
└── memory-bank/ # Memory bank directory
🚀 Quick Start
1. Install Dependencies
pip install -r requirements.txt
2. Configure Settings
Edit config.py
to match your environment:
# Modbus configuration
MODBUS_HOST = "10.84.48.153"
MODBUS_PORT = 505
# MQTT configuration
MQTT_BROKER = "mqtt.service.mesh"
MQTT_TOPIC = "Temperature_Humidity"
3. Run the Service
python main.py
📊 Features
- Modbus TCP Client: Reads data from Modbus holding registers
- MQTT Publisher: Publishes sensor data to MQTT broker
- Data Processing: Converts raw sensor values to calibrated readings
- Error Handling: Robust error handling and retry mechanisms
- Logging: Comprehensive logging for monitoring and debugging
📈 Data Format
The service publishes JSON data to the MQTT topic:
{
"time": "2024-01-15 10:30:25",
"location": "Office",
"temperature": 23.5,
"humidity": 65.2
}
🔧 Configuration
All configuration is centralized in config.py
:
Parameter | Description | Default |
---|---|---|
MODBUS_HOST |
Modbus TCP server IP | "10.84.48.153" |
MODBUS_PORT |
Modbus TCP port | 505 |
MQTT_BROKER |
MQTT broker address | "mqtt.service.mesh" |
MQTT_TOPIC |
MQTT publish topic | "Temperature_Humidity" |
PUBLISH_INTERVAL |
Data publish interval (seconds) | 10 |
LOCATION |
Sensor location identifier | "Office" |
📡 Sensor Mapping
- Register 0: Temperature (raw value × 0.1 - 40)
- Register 1: Humidity (raw value × 0.1)
🛠️ Maintenance
Running as a Service
For production deployment, consider running as a system service:
# Example systemd service file
[Unit]
Description=Modbus MQTT Bridge
After=network.target
[Service]
Type=simple
User=pi
WorkingDirectory=/path/to/POE
ExecStart=/usr/bin/python3 main.py
Restart=always
[Install]
WantedBy=multi-user.target
Monitoring
The service provides comprehensive logging. Monitor the logs for:
- Connection status
- Data reading success/failure
- MQTT publish status
- Error conditions
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
📝 License
This project is licensed under the MIT License.
Description
Languages
Python
90.7%
HCL
9.3%