Example 54: Modbus Sensor Device Configuration
About 2 min
Example 54: Modbus Sensor Device Configuration
Scenario Overview
Connect external sensor devices (such as temperature/humidity transmitters, air quality sensors, soil sensors, etc.) via Modbus RTU protocol to achieve industrial-grade data acquisition and remote monitoring.
Applicable Devices
| Device Type | Typical Model | Measured Parameters |
|---|---|---|
| Temp/Humidity Transmitter | SHT20/SHT30 RS485 | Temperature, Humidity |
| Air Quality Sensor | PM2.5/CO2 RS485 | PM2.5, CO2 concentration |
| Soil Sensor | Soil 3-in-1 RS485 | Temperature, Humidity, Conductivity |
| Light Sensor | BH1750 RS485 | Illuminance (Lux) |
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 |
RS485 bus connection:
ESP32 ──── TTL-to-RS485 ──── A/B Bus ──┬── Sensor 1 (Address 1)
├── Sensor 2 (Address 2)
└── Sensor 3 (Address 3)A 120Ω terminating resistor is recommended at the end of the RS485 bus.
Data Reporting Format
Each sensor data is reported independently:
[
{"id": "modbus_temp_humi", "value": "temperature:25.3,humidity:60.5"},
{"id": "modbus_air", "value": "pm25:35,co2:450"},
{"id": "modbus_soil", "value": "soil_temp:22.1,soil_moisture:45.3,soil_ec:120"}
]Peripheral Execution Linkage
Scenario 1: Temperature Exceeds Limit Triggers Fan (Poll Trigger)
Function: Turn on fan relay when temperature exceeds 30°C
Web Interface Configuration Steps
Step 1: Ensure Peripherals are Configured
- Modbus Temp/Humidity:
modbus_temp_humi - Fan Relay:
relay_fan(GPIO output)
Step 2: Create Rule
- Click Peripheral Configuration in the left menu → Switch to Peripheral Execution Management tab
- Click Add Rule button
- Fill in basic configuration:
- Rule Name:
High Temp Fan On - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Step 3: Configure Trigger (Poll Trigger)
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Poll Trigger Timed condition detection Target Peripheral Select modbus_temp_humiModbus Temp/Humidity Data Field temperatureTemperature field Operator >Greater than Threshold 3030°C Poll Interval 50005 seconds
Step 4: Configure Action
Click Add Action button
Fill in:
- Action Type: Select HIGH
- Target Peripheral: Select
relay_fan
Click Save button
Scenario 2: CO2 Exceeds Limit Alarm (Poll Trigger)
Function: Trigger buzzer alarm when CO2 concentration exceeds 1000ppm
Configuration Steps:
- Create a rule, name:
CO2 Exceed Alarm - Trigger configuration:
- Trigger Type: Select Poll Trigger
- Target Peripheral: Select
modbus_air - Data Field:
co2 - Operator:
> - Threshold:
1000 - Poll Interval:
10000
- Action configuration:
- Action Type: Select HIGH
- Target Peripheral: Select
buzzer1(buzzer must be configured in advance)
- Click Save
Debugging Tips
- Address Scan: Use the built-in Modbus scan feature to confirm slave addresses first
- Baud Rate Matching: Ensure all devices use the same baud rate (common: 9600/4800)
- Register Confirmation: Consult device manual to confirm register start address and data format
- Bus Isolation: When debugging, connect devices one by one; connect all only after confirming communication is normal
Notes
- Bus Length: RS485 bus maximum 1200m, but recommend not exceeding 100m in practice
- Device Count: Maximum 32 slaves per bus; add repeater if exceeded
- Polling Conflict: Stagger the interval of multiple devices to avoid simultaneous requests
- Data Format: Register formats may differ by manufacturer (big-endian/little-endian, integer/float); adjust scale per manual
- Power Isolation: In industrial environments, recommend electrical isolation for RS485 interface to avoid ground loop interference
