Example 53: Modbus Control Device Configuration
About 1 min
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:
[{"id": "modbus_relay4", "value": "relay1:1,relay2:0"}]Control motor:
[{"id": "modbus_motor", "value": "speed:500,direction:1"}]Adjust brightness:
[{"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)
{
"name": "High Temp Fan Relay",
"enabled": true,
"triggers": [
{
"type": "poll",
"params": {
"periphId": "modbus_temp_humi",
"field": "temperature",
"operator": ">",
"threshold": 30
}
}
],
"actions": [
{
"type": "modbus_write",
"params": {
"periphId": "modbus_relay4",
"register": 0,
"value": 65280
}
}
]
}Timed Light Control
{
"name": "Turn On Light on Schedule",
"enabled": true,
"triggers": [
{
"type": "timer",
"params": { "cron": "0 18 * * *" }
}
],
"actions": [
{
"type": "modbus_write",
"params": {
"periphId": "modbus_dimmer",
"register": 0,
"value": 80
}
}
]
}Button Manual Control
{
"name": "Button Control Relay",
"enabled": true,
"triggers": [
{
"type": "event",
"params": { "periphId": "btn1", "event": "pressed" }
}
],
"actions": [
{
"type": "modbus_write",
"params": {
"periphId": "modbus_relay4",
"register": 0,
"value": 65280
}
}
]
}Status Readback
Some control devices support status readback:
{
"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
