# Example 54: Modbus Sensor Devices

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

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

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

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

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Poll Trigger** | Timed condition detection |
   | **Target Peripheral** | Select `modbus_temp_humi` | Modbus Temp/Humidity |
   | **Data Field** | `temperature` | Temperature field |
   | **Operator** | `>` | Greater than |
   | **Threshold** | `30` | 30°C |
   | **Poll Interval** | `5000` | 5 seconds |

**Step 4: Configure Action**

1. Click **Add Action** button

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

3. Click **Save** button

***

### Scenario 2: CO2 Exceeds Limit Alarm (Poll Trigger)

**Function**: Trigger buzzer alarm when CO2 concentration exceeds 1000ppm

**Configuration Steps**:

1. Create a rule, name: `CO2 Exceed Alarm`
2. Trigger configuration:
   * **Trigger Type**: Select **Poll Trigger**
   * **Target Peripheral**: Select `modbus_air`
   * **Data Field**: `co2`
   * **Operator**: `>`
   * **Threshold**: `1000`
   * **Poll Interval**: `10000`
3. Action configuration:
   * **Action Type**: Select **HIGH**
   * **Target Peripheral**: Select `buzzer1` (buzzer must be configured in advance)
4. Click **Save**

## Debugging Tips

1. **Address Scan**: Use the built-in Modbus scan feature to confirm slave addresses first
2. **Baud Rate Matching**: Ensure all devices use the same baud rate (common: 9600/4800)
3. **Register Confirmation**: Consult device manual to confirm register start address and data format
4. **Bus Isolation**: When debugging, connect devices one by one; connect all only after confirming communication is normal

## Notes

1. **Bus Length**: RS485 bus maximum 1200m, but recommend not exceeding 100m in practice
2. **Device Count**: Maximum 32 slaves per bus; add repeater if exceeded
3. **Polling Conflict**: Stagger the interval of multiple devices to avoid simultaneous requests
4. **Data Format**: Register formats may differ by manufacturer (big-endian/little-endian, integer/float); adjust scale per manual
5. **Power Isolation**: In industrial environments, recommend electrical isolation for RS485 interface to avoid ground loop interference
