# Example 51: OLED Temp/Humidity

## Scenario Overview

Use a DHT11/DHT22 temperature and humidity sensor to collect environmental data, and display real-time temperature and humidity values on an SSD1306 OLED screen. This is a typical "sensor read → screen display" linkage scenario.

## Required Hardware

| Module | Model | Purpose |
|--------|-------|---------|
| Temp & Humidity Sensor | DHT11 / DHT22 | Collect temperature and humidity |
| OLED Display | SSD1306 0.96" I2C | Display data |
| ESP32 Dev Board | Puzhong ESP32 | Main controller |

## Hardware Wiring

### DHT11 Wiring

| DHT11 Pin | ESP32 GPIO | Description |
|-----------|-----------|-------------|
| VCC | 3.3V | Power |
| DATA | GPIO 32 | Data pin |
| GND | GND | Ground |

### SSD1306 OLED Wiring

| OLED Pin | ESP32 GPIO | Description |
|----------|-----------|-------------|
| VCC | 3.3V | Power |
| GND | GND | Ground |
| SDA | GPIO 21 | I2C Data |
| SCL | GPIO 22 | I2C Clock |

## Peripheral Execution Linkage Configuration

### Scenario 1: Timed Read and Display Temp/Humidity (Timer Trigger)

**Function**: Read DHT11 data every 3 seconds and display on OLED

#### Web Interface Configuration Steps

**Step 1: 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**: `OLED Display Temp & Humidity`
   * **Report Data**: ✅ Enabled
   * **Enable**: ✅ Enabled

**Step 2: Configure Trigger (Timer Trigger)**

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Timer Trigger** | Timed collection |
   | **Timer Mode** | Select **Fixed Interval** | Millisecond interval |
   | **Interval** | `3000` | 3 seconds |

**Step 3: Configure Actions (2 actions required)**

**Action 1: Read Sensor Data**

1. Click **Add Action** button
2. Fill in:
   * **Action Type**: Select **Sensor Read**
   * **Target Peripheral**: Select `dht1`

**Action 2: OLED Display Data**

1. Click **Add Action** button again

2. Fill in:
   * **Action Type**: Select **OLED Display**
   * **Target Peripheral**: Select `oled1`
   * **Display Template**:
   ```
   === FastBee ===
   Temp: {dht1.temperature}C
   Humi: {dht1.humidity}%
   ===============
   ```

3. Click **Save** button

***

### Scenario 2: Display with Temperature Alarm (Poll Trigger)

**Function**: Display warning and trigger buzzer when temperature exceeds 35°C

**Configuration Steps**:

1. Create a rule, name: `Temperature Alarm Display`
2. Trigger configuration:
   * **Trigger Type**: Select **Poll Trigger**
   * **Target Peripheral**: Select `dht1`
   * **Data Field**: `temperature`
   * **Operator**: `>`
   * **Threshold**: `35`
   * **Poll Interval**: `5000`
3. Action configuration (2 actions required):
   * **Action 1**: Select **OLED Display**, target `oled1`, template:
     ```
     !! WARNING !!
     High Temp!
     {dht1.temperature}C
     Check cooling
     ```
   * **Action 2**: Select **HIGH**, target `buzzer1` (buzzer must be configured in advance)
4. Click **Save**

## Complete Configuration Import

Save the following as a JSON file and import it as `peripherals.json` from **Device Configuration > Advanced Configuration > Configuration Import/Export**:

```json
[
  {
    "id": "dht1",
    "name": "DHT11 Temp & Humidity",
    "type": 38,
    "enabled": false,
    "pins": [32],
    "params": {
      "driver": "DHT",
      "model": "DHT11",
      "interval": 3000
    }
  },
  {
    "id": "oled1",
    "name": "OLED Display",
    "type": 36,
    "enabled": false,
    "pins": [21, 22],
    "params": {
      "driver": "SSD1306",
      "width": 128,
      "height": 64,
      "i2cAddress": "0x3C"
    }
  }
]
```

## Display Output

OLED display example:

```
=== FastBee ===
Temp: 25.3C
Humi: 60.5%
===============
```

* 128x64 resolution supports 4 lines of text (16px font size)
* Supports Chinese and English characters (Chinese characters occupy two character widths)
* `{periphId.field}` in the template will be replaced with actual sensor values

## Extension Suggestions

1. **Multi-Sensor Display**: Add BMP280 barometric pressure data, alternate between multiple pages
2. **Graphical Display**: Use script action to draw temperature trend curves
3. **Time Display**: Combine NTP time to show clock on screen
4. **WiFi Status**: Display WiFi connection status and IP address

## Notes

1. **I2C Address**: SSD1306 default address is usually `0x3C`, some modules use `0x3D`
2. **Refresh Rate**: OLED refresh should not be too frequent (≥ 1000ms), otherwise flickering may occur
3. **Burn-in**: Long-duration static content may cause OLED burn-in. Recommend periodically switching display content
4. **DHT Sampling Rate**: DHT11 maximum 1 time/second, DHT22 maximum 1 time/2 seconds
5. **Shared I2C**: OLED can share I2C bus with BMP280 and other sensors
