# System Patterns ## System Architecture - **Single-process, event-driven script**: The application runs as a single Python process, using a main loop to periodically read from Modbus and publish to MQTT. - **Polling pattern**: Data is acquired from the Modbus device at a fixed interval (configurable via `PUBLISH_INTERVAL`). - **Bridge pattern**: The script acts as a bridge between two protocols (Modbus TCP and MQTT), translating and forwarding data. ## Key Technical Decisions - **Direct variable configuration**: All connection and operational parameters are set as variables at the top of the script for simplicity. - **Error handling and reconnection**: The script checks and re-establishes connections to both Modbus and MQTT as needed, with logging for all failures. - **JSON formatting**: Data is published in pretty-printed JSON for readability and ease of integration. - **Separation of concerns**: Reading/publishing logic is encapsulated in a dedicated function (`read_and_publish_data`), while the main loop handles orchestration and lifecycle. ## Design Patterns - **Callback pattern**: Used for MQTT events (on_connect, on_publish). - **Try/except/finally**: Used for robust error handling and resource cleanup. - **Headless operation**: No UI; all feedback is via logs and MQTT messages. ## Component Relationships - The Modbus client and MQTT client are instantiated and managed independently, but coordinated within the main loop. - Data flows from Modbus -> Python processing -> MQTT publish.