Example 45: Modbus Temp/Humidity Sensor
Example 45: Modbus Temp/Humidity Sensor
Experiment Overview
Reads RS485 temperature and humidity transmitter data (e.g., SHT20/SHT30 RS485 type) via Modbus RTU protocol. This is the most common sensor integration method in industrial environments, suitable for greenhouses, server rooms, warehouses, and similar monitoring scenarios.
Hardware Wiring
RS485 Communication Wiring
| ESP32 Pin | TTL-to-RS485 Module | Description |
|---|---|---|
| GPIO 16 | TXD | Serial transmit |
| GPIO 17 | RXD | Serial receive |
| GPIO 4 (optional) | DE/RE | Direction control |
| 3.3V | VCC | Power |
| GND | GND | Ground |
RS485 Bus to Sensor
| RS485 Module | Temp/Humidity Transmitter | Description |
|---|---|---|
| A+ | A+ | Differential signal positive |
| B- | B- | Differential signal negative |
A 120Ω termination resistor is recommended at the end of the bus; multiple devices can be connected to the same bus with different slave addresses.
JSON Configuration Example
{
"id": "modbus_th",
"name": "Modbus Temp/Humidity Sensor",
"type": 1,
"enabled": false,
"pins": [16, 17],
"params": {
"baudRate": 9600,
"dataBits": 8,
"parity": "none",
"stopBits": 1,
"dePin": 4,
"protocol": "modbus_rtu",
"slaveId": 1,
"function": 3,
"startRegister": 0,
"registerCount": 2,
"interval": 5000,
"dataMap": [
{ "register": 0, "name": "temperature", "scale": 0.1, "unit": "°C" },
{ "register": 1, "name": "humidity", "scale": 0.1, "unit": "%" }
]
}
}Parameter Description
| Parameter | Description |
|---|---|
| slaveId | Slave address (factory default is usually 1; can be modified via DIP switch or command) |
| function | Modbus function code; 3 for reading holding registers |
| startRegister | Starting register address (varies by manufacturer; check manual) |
| registerCount | Number of registers to read |
| interval | Polling interval (ms) |
| dataMap | Mapping from registers to data fields; scale is the scaling factor |
Data Reporting
Sensor data is reported via MQTT:
[{"id": "modbus_th", "value": "temperature:25.3,humidity:60.5"}]Peripheral Execution Linkage
Scenario 1: High Temperature Alarm (Polling Trigger)
Feature: Trigger buzzer alarm when temperature exceeds 35°C
Web UI Configuration Steps
Step 1: Ensure Buzzer Peripheral is Configured
- Peripheral ID:
buzzer_gpio - Type: GPIO Output
Step 2: Create Rule
- Click Peripheral Config in the left menu → Switch to Peripheral Execution tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Temp/Humidity Alarm - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Step 3: Configure Trigger (Polling Trigger)
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Polling Trigger Periodic condition check Target Peripheral Select modbus_thModbus temp/humidity Data Field temperatureTemperature field Operator >Greater than Threshold 3535°C Polling Interval 50005 seconds
Step 4: Configure Action
Click Add Action button
Fill in:
- Action Type: Select HIGH
- Target Peripheral: Select
buzzer_gpio
Click Save button
Scenario 2: OLED Display Linkage (Timer Trigger)
Feature: Periodically display temperature and humidity data on OLED
Configuration Steps:
- Create rule, name:
Display Temp/Humidity - Trigger configuration:
- Trigger Type: Select Timer Trigger
- Interval:
5000(5 seconds)
- Action configuration (2 actions required):
- Action 1: Select Sensor Read, target
modbus_th - Action 2: Select Display Control, target
oled1, templateTemp:{modbus_th.temperature}C\nHumi:{modbus_th.humidity}%
- Action 1: Select Sensor Read, target
- Click Save
Notes
- Address Confirmation: Use the system Modbus scan feature to confirm slave address; factory address may not be 1
- Register Format: Temperature register may be signed integer (requires scale=0.1) or float, depending on manufacturer
- Baud Rate Matching: Sensor and ESP32 baud rates must match; common values are 4800/9600
- Wiring Polarity: A+/B- must not be reversed, otherwise communication will fail
- Power Requirements: RS485 sensors typically require 12-24V DC power; independent power supply needed
- Bus Length: Longer bus = worse interference resistance; use shielded twisted pair for distances over 50m
