Skip to content

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 TypeTypical ModelControl Function
Multi-channel Relay4/8/16-ch RS485 RelayOn/Off control
Motor ControllerVFD RS485Speed/direction control
Dimming Module0-10V/PWM RS485Light brightness adjustment
Solenoid ValveRS485 Proportional ValveFlow control

Hardware Wiring ​

RS485 Interface Connection ​

ESP32 PinTTL-to-RS485 ModuleDescription
GPIO 16 (TX)TXDSerial transmit
GPIO 17 (RX)RXDSerial receive
GPIO 4 (optional)DE/REDirection control (half-duplex)
3.3VVCCPower
GNDGNDGround

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: 16 writes a Modbus coil (FC05); actionValue uses 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: 1 selects the daily time-point mode; timePoint runs 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 ​

  1. Function Code Selection: Use function code 5/15 for coil control, function code 6/16 for register control
  2. Write Confirmation: Recommend readback after writing to confirm successful execution
  3. Interlock Protection: Opposite actions (e.g., forward/reverse) must have interlock logic to prevent simultaneous activation
  4. Power-off Memory: Some relay modules support power-off memory, restoring last state after restart
  5. Response Timeout: Control devices respond quickly, recommended timeout setting 500-1000ms
  6. Safety Interlock: Critical device control should include hardware emergency stop buttons, not relying solely on software control

FastBee | fastbee.cn