# Example 19: DHT11 Temp/Humidity

## Experiment Overview

DHT11 is a commonly used digital temperature and humidity sensor, measurement range: temperature 0~50°C (±2°C), humidity 20~90%RH (±5%RH). FastBee has a built-in `SENSOR_GENERIC` generic sensor type (type: 38) that directly supports DHT11/DHT22.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|-----------------|
| DATA | GPIO27 | DHT11 data pin |

> DHT11 three-wire connection: VCC→3.3V/5V, GND→GND, DATA→GPIO (module has built-in pull-up resistor).

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "dht_01",
      "name": "DHT11 Temp/Humidity",
      "type": 38,
      "enabled": false,
      "pins": [27],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario 1: Periodic Temp/Humidity Acquisition (Timer Trigger)

**Function**: Read temperature and humidity data every 5 seconds

#### Web Interface Configuration Steps

**Step 1: 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**: `DHT Temp/Humidity Acquisition`
   * **Report Data**: ✅ Enable (report to cloud platform after reading)
   * **Enabled**: ✅ Enable

**Step 2: Configure Trigger**

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Timer Trigger** | Execute at time intervals |
   | **Timer Mode** | Select **Interval Timer** | Execute every X seconds |
   | **Interval Seconds** | `5` | Acquire every 5 seconds |

**Step 3: Configure Action**

1. Click **Add Action** button

2. Fill in action configuration:

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Action Type** | Select **Sensor Read** | Read sensor data |
   | **Target Peripheral** | Select `dht_room` | Sensor to read |
   | **Data Field** | `temperature` | Read temperature (or humidity) |

3. Click **Save** button

**Execution Flow**:

```
Every 5 seconds
  ↓
Read dht_room sensor
  ↓
Get temperature value
  ↓
Report to cloud platform (if reporting enabled)
```

> 💡 **Tip**: DHT11 minimum sampling interval is 1 second, recommend 2 seconds or more to avoid read failures

***

### Scenario 2: Low Humidity Alarm (Poll Trigger)

**Function**: When humidity drops below 30%, start humidifier (relay control)

#### Web Interface Configuration Steps

**Step 1: Create Rule**

1. Click **<i class="fas fa-plus"></i> New Rule**
2. Fill in basic configuration:
   * **Rule Name**: `Low Humidity Alarm`
   * **Report Data**: ✅ Enable
   * **Enabled**: ✅ Enable

**Step 2: Configure Trigger**

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Poll Trigger** | Periodically detect humidity |
   | **Target Peripheral** | Select `dht_room` | Temp/humidity sensor |
   | **Poll Interval** | `10` seconds | Check every 10 seconds |
   | **Comparison Operator** | Select **Less Than** | Lower limit alarm |
   | **Comparison Value** | `30` | Humidity threshold 30% |

**Step 3: Configure Action**

1. Click **Add Action** button

2. Fill in action configuration:

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Action Type** | Select **HIGH Level** | GPIO output HIGH |
   | **Target Peripheral** | Select `relay_humidifier` | Humidifier relay |

3. Click **Save** button

**Execution Flow**:

```
Every 10 seconds
  ↓
Read dht_room humidity value
  ↓
Check: humidity < 30 ?
  ↓ Yes
Start humidifier (relay closes)
```

> ⚠️ **Note**: This rule only has start function, no stop function. Recommend adding another rule: turn off humidifier when humidity > 50%

***

### Scenario 3: Display Temp/Humidity on OLED (Timer Trigger + Multi-Action)

**Function**: Display current temperature and humidity on OLED screen every 3 seconds

#### Prerequisites

Need to configure OLED display peripheral (type: 36) first, assuming ID is `oled_display`

#### Web Interface Configuration Steps

**Step 1: Create Rule**

1. Click **<i class="fas fa-plus"></i> New Rule**
2. Fill in basic configuration:
   * **Rule Name**: `Temp/Humidity Display`
   * **Report Data**: ❌ Disable
   * **Enabled**: ✅ Enable

**Step 2: Configure Trigger**

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Timer Trigger** | Periodically refresh display |
   | **Timer Mode** | Select **Interval Timer** | Execute every X seconds |
   | **Interval Seconds** | `3` | Refresh every 3 seconds |

**Step 3: Configure Actions (3 actions needed)**

**Action 1: Read Temperature**

1. Click **Add Action** button
2. Fill in:
   * **Action Type**: Select **Sensor Read**
   * **Target Peripheral**: Select `dht_room`
   * **Data Field**: `temperature`

**Action 2: Read Humidity**

1. Click **Add Action** button again
2. Fill in:
   * **Action Type**: Select **Sensor Read**
   * **Target Peripheral**: Select `dht_room`
   * **Data Field**: `humidity`

**Action 3: Display on OLED**

1. Click **Add Action** button a third time

2. Fill in:
   * **Action Type**: Select **OLED Display**
   * **Target Peripheral**: Select `oled_display`
   * **Display Content**:
     ```
     Temp: ${dht_room.temperature}°C
     Humidity: ${dht_room.humidity}%
     ```
   * **Execution Mode**: Select **Synchronous**

3. Click **Save** button

**Execution Flow**:

```
Every 3 seconds
  ↓
Read temperature value
  ↓
Read humidity value
  ↓
Display two lines on OLED:
  Line 1: Temp: 25.3°C
  Line 2: Humidity: 65%
```

> 💡 **Variable Substitution**: `${dht_room.temperature}` will be automatically replaced with the actual temperature value read

## Notes

1. **Sampling Interval**: DHT11 minimum sampling interval is 1 second, recommend 2 seconds or more
2. **DHT22 Compatible**: DHT22 (AM2302) uses the same configuration, with higher precision
3. **Power Supply**: Both 3.3V and 5V work, 5V for longer transmission distance
4. **First Read**: First read after power-on may fail, second read is normal
5. **Wire Length**: Data line should not exceed 20m, longer distances need increased pull-up resistance
