# 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**: 0 - **Created**: 2026-04-11 - **Last Updated**: 2026-04-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: ESP32, DTU, Arduino, Cpp ## README Let me check if there's an existing README in the repository: Based on my analysis of the code map you've provided, this is a **WT32 Modbus-to-MQTT Data Transmission Unit (DTU)** project. Let me create a comprehensive README for this IoT gateway device. --- # WT32-Arduino-DTU Modbus RTU 转 MQTT 网关 | 基于 WT32 (ESP32) 硬件平台 ## 项目简介 本项目实现了一个功能强大的 Modbus RTU to MQTT 数据传输单元 (DTU),可将工业现场的 Modbus RTU 设备数据采集并通过 MQTT 协议上传到物联网平台。支持多通道数据转发、以太网连接、OTA 远程固件升级等功能。 ### 主要特性 - **Modbus RTU 主站**:通过 HardwareSerial 接口读取从站设备数据 - **MQTT 上报**:支持多通道 (3通道) MQTT 数据上报 - **以太网连接**:内置 Etherne 模块 (LAN8720) - **自动发现**:UDP 广播设备发现功能 - **远程管理**:支持 MQTT 命令远程控制写线圈 - **OTA 升级**:通过 MQTT 触发远程固件更新 - **时间同步**:NTP 自动校时 - **自动化规则**:支持本地自动化触发逻辑 ## 硬件配置 - **主控芯片**: WT32 (ESP32-based) - **Modbus 接口**: UART1 (RS485) - **以太网**: LAN8720 - **LED 指示**: 2 个呼吸灯 ## 项目结构 ``` src/ ├── main.cpp # 程序入口 ├── ModbusManager.cpp # Modbus 通信管理 ├── ModbusManager.h ├── MqttManager.cpp # MQTT 客户端管理 ├── MqttManager.h ├── MqttSimpleClient.cpp # 轻量级 MQTT 客户端 ├── MqttSimpleClient.h ├── MQTTProtocol.cpp # MQTT 协议处理 ├── MQTTProtocol.h ├── TCPProtocol.cpp # TCP 协议处理 ├── TCPProtocol.h ├── NetManager.cpp # 网络管理 ├── NetManager.h ├── NetworkManager.cpp # 多通道网络管理 ├── NetworkManager.h ├── ConfigManager.cpp # 配置管理 (Preferences) ├── ConfigManager.h ├── DiscoveryManager.cpp # 设备发现 (UDP广播) ├── DiscoveryManager.h ├── ChannelManager.cpp # 通道管理 ├── ChannelManager.h ├── OtaManager.cpp # OTA 远程升级 ├── OtaManager.h ├── TimeManager.cpp # 时间管理 (NTP) ├── TimeManager.h ├── SerialManager.cpp # 串口管理 ├── SerialManager.h ├── ResourceManager.cpp # 资源序列化 ├── ResourceManager.h ├── BreathingLED.cpp # 呼吸灯效果 ├── BreathingLED.h ├── AutomationManager.cpp # 自动化规则 ├── AutomationManager.h └── BaseProtocol.h # 基础协议定义 ``` ## MQTT 主题格式 ### 上报主题 ``` /device/{device_id}/channel/{channel}/modbus/{slave_id}/{reg_addr} ``` ### 命令主题 ``` /device/{device_id}/cmd ``` ### 响应主题 ``` /device/{device_id}/response ``` ## 配置说明 ### 设备配置 (ConfigManager) | 参数 | 说明 | |------|------| | device_id | 设备唯一标识 | | mqtt_server | MQTT 服务器地址 | | mqtt_port | MQTT 端口 | | mqtt_username | MQTT 用户名 | | mqtt_password | MQTT 密码 | | wifi_ssid | WiFi SSID (备用) | | wifi_password | WiFi 密码 | ### Modbus 配置 | 参数 | 说明 | |------|------| | slave_id | 从站地址 | | reg_addr | 寄存器地址 | | reg_count | 寄存器数量 | | interval | 轮询间隔 (ms) | ## 使用方法 ### 1. 环境搭建 使用 PlatformIO 开发: ```bash # 克隆项目 git clone https://gitee.com/shp-java/wt32-arduino-dtu # 进入目录 cd wt32-arduino-dtu # 编译上传 pio run upload ``` ### 2. 硬件连接 - **RS485 模块**: 连接到 UART1 (GPIO9=GPIO_RX, GPIO10=GPIO_TX) - **以太网**: LAN8720 连接到 EPS32 RMII 接口 ### 3. MQTT 命令 #### 读取寄存器 发送 JSON 消息到 `/device/{device_id}/cmd`: ```json {"action": "read", "slave": 1, "addr": 0, "count": 2} ``` #### 写入线圈 ```json {"action": "write", "slave": 1, "addr": 0, "value": 1} ``` #### OTA 升级 ```json {"action": "ota", "url": "http://example.com/firmware.bin"} ``` ## 依赖库 - [PubSubClient](https://github.com/knolleary/pubSubClient) - MQTT 客户端 - [ModbusMaster](https://github.com/4-20ma/ModbusMaster) - Modbus 主站 - [ArduinoJson](https://github.com/bblanchon/ArduinoJson) - JSON 处理 - [Preferences](https://github.com/vsmedia/esp32-preferences) - 配置存储 ## 固件版本 **当前版本**: v1.0.7 ## 许可证 MIT License ## 参考资料 - [WT32 模块文档](http://www.wt32.com) - [ESP32 Arduino 核心](https://github.com/espressif/arduino-esp32) - [Modbus RTU 协议规范](http://www.modbus.org) - [MQTT 协议规范](http://mqtt.org) --- ## 注意事项 1. 确保 RS485 转换模块的 DE/RE 控制引脚正确连接 2. 以太网使用 LAN8720 模块,需确保晶振频率正确 3. MQTT 服务器需支持 WebSocket 或 TCP 连接 4. 首次使用请通过 Discovery 功能发现设备 IP --- *此 README 由 Gitee 自动生成*