# wt32-arduino-dtu **Repository Path**: shp-java/wt32-arduino-dtu ## Basic Information - **Project Name**: wt32-arduino-dtu - **Description**: 使用wt32-eth01(esp32)开发板为基础,实现数据采集,协议转换,逻辑控制,远程升级等功能。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2026-04-11 - **Last Updated**: 2026-06-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: ESP32, DTU, Arduino, Cpp ## README # WT32-Arduino-DTU Modbus RTU to MQTT Gateway | Based on WT32 (ESP32) Hardware Platform ## Project Overview This project implements a powerful Modbus RTU to MQTT Data Transmission Unit (DTU) that collects data from industrial Modbus RTU devices and uploads it to IoT platforms via the MQTT protocol. It supports multi-channel data forwarding, Ethernet connectivity, and OTA remote firmware updates. ### Key Features - **Modbus RTU Master**: Reads data from slave devices via HardwareSerial interface - **MQTT Reporting**: Supports multi-channel (3 channels) MQTT data reporting - **Ethernet Connectivity**: Built-in Ethernet module (LAN8720) - **Auto-Discovery**: UDP broadcast device discovery functionality - **Remote Management**: Supports MQTT command-based remote coil writing - **OTA Updates**: Triggers remote firmware updates via MQTT - **Time Synchronization**: Automatic NTP time calibration - **Automation Rules**: Supports local automation trigger logic ## Hardware Configuration - **Main Controller**: WT32 (ESP32-based) - **Modbus Interface**: UART1 (RS485) - **Ethernet**: LAN8720 - **LED Indicators**: 2 breathing LEDs ## Project Structure ``` src/ ├── main.cpp # Program entry point ├── ModbusManager.cpp # Modbus communication manager ├── ModbusManager.h ├── MqttManager.cpp # MQTT client manager ├── MqttManager.h ├── MqttSimpleClient.cpp # Lightweight MQTT client ├── MqttSimpleClient.h ├── MQTTProtocol.cpp # MQTT protocol handler ├── MQTTProtocol.h ├── TCPProtocol.cpp # TCP protocol handler ├── TCPProtocol.h ├── NetManager.cpp # Network manager ├── NetManager.h ├── NetworkManager.cpp # Multi-channel network manager ├── NetworkManager.h ├── ConfigManager.cpp # Configuration manager (Preferences) ├── ConfigManager.h ├── DiscoveryManager.cpp # Device discovery (UDP broadcast) ├── DiscoveryManager.h ├── ChannelManager.cpp # Channel manager ├── ChannelManager.h ├── OtaManager.cpp # OTA remote update ├── OtaManager.h ├── TimeManager.cpp # Time manager (NTP) ├── TimeManager.h ├── SerialManager.cpp # Serial port manager ├── SerialManager.h ├── ResourceManager.cpp # Resource serialization ├── ResourceManager.h ├── BreathingLED.cpp # Breathing LED effect ├── BreathingLED.h ├── AutomationManager.cpp # Automation rules ├── AutomationManager.h └── BaseProtocol.h # Base protocol definitions ``` ## MQTT Topic Format ### Reporting Topics ``` /device/{device_id}/channel/{channel}/modbus/{slave_id}/{reg_addr} ``` ### Command Topics ``` /device/{device_id}/cmd ``` ### Response Topics ``` /device/{device_id}/response ``` ## Configuration Details ### Device Configuration (ConfigManager) | Parameter | Description | |-----------|-------------| | device_id | Unique device identifier | | mqtt_server | MQTT server address | | mqtt_port | MQTT port | | mqtt_username | MQTT username | | mqtt_password | MQTT password | | wifi_ssid | WiFi SSID (backup) | | wifi_password | WiFi password | ### Modbus Configuration | Parameter | Description | |-----------|-------------| | slave_id | Slave device address | | reg_addr | Register address | | reg_count | Number of registers | | interval | Polling interval (ms) | ## Usage Instructions ### 1. Environment Setup Use PlatformIO for development: ```bash # Clone the project git clone https://gitee.com/shp-java/wt32-arduino-dtu # Enter directory cd wt32-arduino-dtu # Compile and upload pio run upload ``` ### 2. Hardware Connections - **RS485 Module**: Connect to UART1 (GPIO9 = RX, GPIO10 = TX) - **Ethernet**: Connect LAN8720 to ESP32 RMII interface ### 3. MQTT Commands #### Read Registers Send JSON message to `/device/{device_id}/cmd`: ```json {"action": "read", "slave": 1, "addr": 0, "count": 2} ``` #### Write Coil ```json {"action": "write", "slave": 1, "addr": 0, "value": 1} ``` #### OTA Update ```json {"action": "ota", "url": "http://example.com/firmware.bin"} ``` ## Dependencies - [PubSubClient](https://github.com/knolleary/pubSubClient) - MQTT client - [ModbusMaster](https://github.com/4-20ma/ModbusMaster) - Modbus master - [ArduinoJson](https://github.com/bblanchon/ArduinoJson) - JSON processing - [Preferences](https://github.com/vsmedia/esp32-preferences) - Configuration storage ## Firmware Version **Current Version**: v1.0.7 ## License MIT License ## References - [WT32 Module Documentation](http://www.wt32.com) - [ESP32 Arduino Core](https://github.com/espressif/arduino-esp32) - [Modbus RTU Protocol Specification](http://www.modbus.org) - [MQTT Protocol Specification](http://mqtt.org) --- ## Notes 1. Ensure the RS485 transceiver's DE/RE control pins are properly connected. 2. The LAN8720 Ethernet module requires correct crystal oscillator frequency. 3. MQTT server must support WebSocket or TCP connections. 4. On first use, use the Discovery feature to detect the device IP. --- *This README is auto-generated by Gitee*