Example 53: Modbus Control Device Configuration
Scenario Overview
Connect external control devices (such as multi-channel relay modules, motor controllers, dimmers, etc.) via Modbus RTU protocol to achieve remote control and automation linkage.
Applicable Devices
| Device Type | Typical Model | Control Function |
|---|---|---|
| Multi-channel Relay | 4/8/16-ch RS485 Relay | On/Off control |
| Motor Controller | VFD RS485 | Speed/direction control |
| Dimming Module | 0-10V/PWM RS485 | Light brightness adjustment |
| Solenoid Valve | RS485 Proportional Valve | Flow control |
Hardware Wiring
RS485 Interface Connection
| ESP32 Pin | TTL-to-RS485 Module | Description |
|---|---|---|
| GPIO 16 (TX) | TXD | Serial transmit |
| GPIO 17 (RX) | RXD | Serial receive |
| GPIO 4 (optional) | DE/RE | Direction control (half-duplex) |
| 3.3V | VCC | Power |
| GND | GND | Ground |
Control Commands
Send Control via MQTT
Control relay:
json
[{"id": "modbus_relay4", "value": "relay1:1,relay2:0"}]Control motor:
json
[{"id": "modbus_motor", "value": "speed:500,direction:1"}]Adjust brightness:
json
[{"id": "modbus_dimmer", "value": "brightness:75"}]Control via Web Interface
In the peripheral management page, you can directly operate the on/off or value of each control channel.
Peripheral Execution Linkage
Temperature-Linked Relay (with sensor scenario)
json
{
"name": "High Temp Fan Relay",
"enabled": true,
"triggers": [
{
"triggerType": 5,
"triggerPeriphId": "modbus_temp_humi",
"intervalSec": 30,
"operatorType": 2,
"compareValue": "30"
}
],
"actions": [
{ "actionType": 16, "targetPeriphId": "modbus_relay4", "actionValue": "0:1" }
]
}
actionType: 16writes a Modbus coil (FC05);actionValueuses the"coil-address:value"format.
Timed Light Control
json
{
"name": "Turn On Light on Schedule",
"enabled": true,
"triggers": [
{ "triggerType": 1, "timerMode": 1, "timePoint": "18:00" }
],
"actions": [
{ "actionType": 17, "targetPeriphId": "modbus_dimmer", "actionValue": "0:80" }
]
}
timerMode: 1selects the daily time-point mode;timePointruns the rule at 18:00.
Button Manual Control
json
{
"name": "Button Control Relay",
"enabled": true,
"triggers": [
{ "triggerType": 4, "triggerPeriphId": "btn1", "eventId": "button_click" }
],
"actions": [
{ "actionType": 16, "targetPeriphId": "modbus_relay4", "actionValue": "0:1" }
]
}Status Readback
Some control devices support status readback:
json
{
"id": "modbus_relay4_status",
"name": "Relay Status",
"type": 1,
"enabled": false,
"pins": [16, 17],
"params": {
"protocol": "modbus_rtu",
"slaveId": 10,
"function": 1,
"startRegister": 0,
"registerCount": 4,
"interval": 2000
}
}Function code 1 (Read Coils) is used to read coil status.
Notes
- Function Code Selection: Use function code 5/15 for coil control, function code 6/16 for register control
- Write Confirmation: Recommend readback after writing to confirm successful execution
- Interlock Protection: Opposite actions (e.g., forward/reverse) must have interlock logic to prevent simultaneous activation
- Power-off Memory: Some relay modules support power-off memory, restoring last state after restart
- Response Timeout: Control devices respond quickly, recommended timeout setting 500-1000ms
- Safety Interlock: Critical device control should include hardware emergency stop buttons, not relying solely on software control

