# Example 45: Modbus Temp/Humidity

## 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

```json
{
  "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:

```json
[{"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**

1. Click **Peripheral Execution** in the left menu
2. Click **<i class="fas fa-plus"></i> New Rule** button
3. Fill in basic configuration:
   * **Rule Name**: `Temp/Humidity Alarm`
   * **Report Data**: ✅ Enabled
   * **Enable**: ✅ Enabled

**Step 3: Configure Trigger (Polling Trigger)**

1. Click **Add Trigger** button
2. Fill in trigger configuration:

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Polling Trigger** | Periodic condition check |
   | **Target Peripheral** | Select `modbus_th` | Modbus temp/humidity |
   | **Data Field** | `temperature` | Temperature field |
   | **Operator** | `>` | Greater than |
   | **Threshold** | `35` | 35°C |
   | **Polling Interval** | `5000` | 5 seconds |

**Step 4: Configure Action**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **HIGH**
   * **Target Peripheral**: Select `buzzer_gpio`

3. Click **Save** button

***

### Scenario 2: OLED Display Linkage (Timer Trigger)

**Feature**: Periodically display temperature and humidity data on OLED

**Configuration Steps**:

1. Create rule, name: `Display Temp/Humidity`
2. Trigger configuration:
   * **Trigger Type**: Select **Timer Trigger**
   * **Interval**: `5000` (5 seconds)
3. Action configuration (2 actions required):
   * **Action 1**: Select **Sensor Read**, target `modbus_th`
   * **Action 2**: Select **Display Control**, target `oled1`, template `Temp:{modbus_th.temperature}C\nHumi:{modbus_th.humidity}%`
4. Click **Save**

## Notes

1. **Address Confirmation**: Use the system Modbus scan feature to confirm slave address; factory address may not be 1
2. **Register Format**: Temperature register may be signed integer (requires scale=0.1) or float, depending on manufacturer
3. **Baud Rate Matching**: Sensor and ESP32 baud rates must match; common values are 4800/9600
4. **Wiring Polarity**: A+/B- must not be reversed, otherwise communication will fail
5. **Power Requirements**: RS485 sensors typically require 12-24V DC power; independent power supply needed
6. **Bus Length**: Longer bus = worse interference resistance; use shielded twisted pair for distances over 50m
