commit 286ef3e7fea9660fc15421193d7402a84c6d489c Author: naab Date: Thu May 29 11:05:45 2025 +0700 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..99b227f --- /dev/null +++ b/README.md @@ -0,0 +1,121 @@ +# 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 +```bash +pip install -r requirements.txt +``` + +### 2. Configure Settings +Edit `config.py` to match your environment: +```python +# 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 +```bash +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: +```json +{ + "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: + +```bash +# 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 + +1. Fork the repository +2. Create a feature branch +3. Make your changes +4. Test thoroughly +5. Submit a pull request + +## 📝 License + +This project is licensed under the MIT License. \ No newline at end of file