Skip to content

Modbus Sub-Device ​

Overview ​

Modbus sub-device peripherals are used to control external Modbus slave devices via RS485 bus, such as relay modules, PWM controllers, PID controllers, motor drivers, etc. Supports coil write (FC05) and register write (FC06) control protocols.

Supported Peripheral Types ​

Typetype ValueDescription
MODBUS_DEVICE51Modbus RS485 sub-device

Communication Method ​

  • Physical Layer: RS485 half-duplex
  • Protocol: Modbus RTU
  • Control Method: Send commands through the RS485 port configured via UART peripheral

Modbus sub-devices do not occupy GPIO pins (communicate through already configured UART/RS485 ports)

Configuration ​

Modbus sub-devices also require first confirming Modbus RTU serial parameters, slave address, and baud rate on the communication protocol page.

AreaFieldExample Value / Status
Status OverviewRisk LevelLow
Status OverviewActive Tasks0
Status OverviewTimeout Rate0%
Status OverviewTotal Polls / Success / Failed / Timeout0 / 0 / 0 / 0
Basic ConfigEnable Modbus RTUEnabled
Basic ConfigPeripheral Config SelectionSelect configured RS485/UART peripheral
Basic ConfigTransfer TypeTransparent (raw HEX frames)
Basic ConfigDE Pin (RS485)14
Sub-Device NameTypeSlave AddressDevice InfoEnableActions
Temp/HumidityCollection2FC04 @0 x2 [2 mappings]ONEdit / Map / Delete
PM25-10Collection1FC03 @0 x2 [2 mappings]ONEdit / Map / Delete
Weather LouverCollection4FC03 @500 x8 [6 mappings]ONEdit / Map / Delete

The peripheral object for Modbus sub-devices only describes "how slave and register mapping works in the system"; the underlying serial port, baud rate, and bus wiring still follow the communication protocol page configuration.

Step 1: Navigate to Peripheral Management Page ​

  1. Open a browser and navigate to the ESP32 IP address
  2. After logging in, click Peripheral Configuration in the left menu

Step 2: Add Modbus Sub-Device Peripheral ​

  1. Click the Add Peripheral button

  2. Fill in the configuration:

    FieldValueDescription
    Peripheral IDmb_relay or mb_pwmUnique identifier
    Name4-Channel Relay or 4-Channel PWM DimmingDisplay name
    Peripheral TypeModbus Device (type: 51)RS485 control
    Slave Address1 or 2Modbus address (1-247)
    Channel Count4Number of relay channels or PWM channels
    Control ProtocolCoil(FC05) or Register(FC06)Based on device type
    Device Typerelay or pwmRelay or PWM
  3. Click Save

Step 3: Verify Configuration ​

  1. Find the newly added peripheral in the peripheral list
  2. Click the Enable toggle
  3. Use the control panel to test control

⚠️ Important: Modbus devices depend on UART peripherals to provide the RS485 physical channel; UART must be configured first


Method 2: JSON Configuration File Import ​

Add the following configuration to the peripherals array in data/config/peripherals.json:

4-Channel Relay Module ​

json
{
  "id": "mb_relay",
  "name": "4-Channel Relay",
  "type": 51,
  "enabled": false,
  "pins": [],
  "params": {
    "slaveAddress": 1,
    "channelCount": 4,
    "coilBase": 0,
    "ncMode": false,
    "controlProtocol": 0,
    "deviceType": 0,
    "deviceIndex": 0,
    "batchRegister": 0,
    "pwmRegBase": 0,
    "pwmResolution": 0,
    "motorRegs": [0, 0, 0, 0, 0],
    "motorDecimals": 0,
    "sensorId": ""
  }
}

PWM Dimming Module ​

json
{
  "id": "mb_pwm",
  "name": "4-Channel PWM Dimming",
  "type": 51,
  "enabled": false,
  "pins": [],
  "params": {
    "slaveAddress": 2,
    "channelCount": 4,
    "coilBase": 0,
    "ncMode": false,
    "controlProtocol": 1,
    "deviceType": 1,
    "deviceIndex": 1,
    "batchRegister": 0,
    "pwmRegBase": 100,
    "pwmResolution": 10,
    "motorRegs": [0, 0, 0, 0, 0],
    "motorDecimals": 0,
    "sensorId": ""
  }
}

Parameter Description ​

ParameterDescription
slaveAddressModbus slave address (1-247)
channelCountNumber of channels
coilBaseCoil/register base address
ncModeNC normally-closed mode (true=normally closed, control logic inverted)
controlProtocolControl protocol: 0=coil(FC05), 1=register(FC06)
deviceTypeDevice type: 0=relay, 1=pwm, 2=pid, 3=motor
deviceIndexIndex in ModbusHandler config
batchRegisterBitmap batch register address (0=not used)
pwmRegBasePWM register base address
pwmResolutionPWM resolution (bits)
motorRegsMotor register addresses [forward, reverse, stop, speed, pulse count]
motorDecimalsMotor parameter decimal places
motorMinPositionMotor/slide soft limit minimum position, unit is steps
motorMaxPositionMotor/slide soft limit maximum position; soft limit enabled when motorMaxPosition > motorMinPosition
motorCurrentPositionCurrent estimated position; should be updated to corresponding steps after homing or manual calibration
motorMoveStepforward/reverse default relative move steps; uses most recent setPulse value when not set
motorLastPulseMost recent pulse count; updated at runtime
sensorIdSensor identifier (used for data collection reporting)

Modbus Motor Slide Soft Limit ​

When sub-device deviceType is motor, the following fields can limit slide travel:

json
{
  "deviceType": "motor",
  "motorRegs": [0, 1, 2, 5, 7],
  "motorMinPosition": 0,
  "motorMaxPosition": 10000,
  "motorCurrentPosition": 0,
  "motorMoveStep": 1600,
  "motorLastPulse": 1600
}

When enabled, forward moves toward motorMaxPosition, reverse moves toward motorMinPosition. If the current move would exceed the boundary, the system first reduces the pulse count to within bounds before sending the direction command; if already at the boundary or lacking available pulses, execution is rejected and MOTOR_SOFT_LIMIT is returned.

Soft limits depend on the current estimated position; after first power-on, manual movement, or homing, motorCurrentPosition must be calibrated to the actual position. For true limit protection, hardware limit switches or emergency stop are still recommended; software limits are for reducing misoperations and rule boundary violations.

Peripheral Execution Integration ​

Web Interface Configuration Steps ​

Create Modbus Relay Control Rule

  1. Click Peripheral Execution in the left menu
  2. Click the Add Rule button
  3. Configure trigger (e.g., platform trigger, event trigger)
  4. Add Modbus action:
    • Action Type: Modbus Coil Write
    • Target Peripheral: mb_relay
    • Action Value: {"ch":0,"val":1} (channel 0, ON)
  5. Click Save

💡 Tip: Coil write (FC05) is for relays; register write (FC06) is for PWM


JSON Configuration Example ​

Modbus Coil Write (ACTION_MODBUS_COIL_WRITE = 16) ​

json
{
  "targetPeriphId": "mb_relay",
  "actionType": 16,
  "actionValue": "{\"ch\":0,\"val\":1}",
  "useReceivedValue": false,
  "syncDelayMs": 0,
  "execMode": 0
}

Modbus Register Write (ACTION_MODBUS_REG_WRITE = 17) ​

json
{
  "targetPeriphId": "mb_pwm",
  "actionType": 17,
  "actionValue": "{\"reg\":100,\"val\":512}",
  "useReceivedValue": false,
  "syncDelayMs": 0,
  "execMode": 0
}

Modbus Poll Collection (ACTION_MODBUS_POLL = 18) ​

json
{
  "targetPeriphId": "modbus-task:0",
  "actionType": 18,
  "actionValue": "{\"poll\":[0]}",
  "useReceivedValue": false,
  "syncDelayMs": 0,
  "execMode": 0
}

Notes ​

  1. UART Prerequisite: Modbus devices depend on UART peripherals to provide the RS485 physical channel; UART must be configured first
  2. Unique Address: Each device address must be unique on the same bus
  3. Communication Interval: Recommend ≥50ms interval between consecutive commands (controlled by pollInterPollDelay parameter)
  4. Timeout Retry: Default response timeout is 1000ms, maximum retry is 2 times
  5. pinCount=0: Modbus devices do not use GPIO pins

FastBee | fastbee.cn