Skip to content

Scenario: DHT Temp/Humidity Sensor + Relay Linkage ​

Scenario Description ​

Configure DHT11/DHT22 temperature/humidity sensor for periodic environmental data collection. When temperature exceeds a set threshold, automatically turn on the relay (controlling fan/AC); when temperature returns to normal, automatically turn off. Also display real-time temperature/humidity data on an OLED screen.

Hardware List ​

ModuleModelQtyWiring
Temp/Humidity SensorDHT221DATA→GPIO4
Relay Module5V single channel1IN→GPIO15
OLED DisplaySSD1306 128x641SDA→GPIO21, SCL→GPIO22
(Optional) BuzzerActive buzzer1IN→GPIO25

Complete Configuration Process ​

This scenario requires first adding the temperature/humidity sensor, relay, and display, then using peripheral execution rules for collection, threshold evaluation, control, and display.

The temperature control scenario is broken down in a closed loop: DHT collects temperature/humidity, rule evaluates threshold, relay or display executes actions, MQTT or log records results.

Step 1: Configure Peripherals ​

Step 1: Open Peripheral Management Page

  1. Open a browser and navigate to the ESP32 IP address
  2. After logging in, click Peripheral Configuration in the left menu

Step 2: Add DHT22 Temperature/Humidity Sensor

  1. Click the Add Peripheral button

  2. Fill in the configuration:

    FieldValueDescription
    Peripheral IDdht22_roomRoom temp/humidity
    NameRoom Temp/HumidityDisplay name
    Peripheral TypeUniversal Sensor (type: 38)DHT driver
    Pin Configuration4DATA connected to GPIO4
    Sensor CategoryDHTDHT series
    Sensor ModelDHT22DHT22 has higher accuracy
    Collection Interval50005 seconds
  3. Click Save

Step 3: Add Fan Relay

  1. Click the Add Peripheral button again

  2. Fill in the configuration:

    FieldValueDescription
    Peripheral IDrelay_fanFan relay
    NameFan RelayDisplay name
    Peripheral TypeGPIO Digital Output (type: 12)Relay control
    Pin Configuration15IN connected to GPIO15
    Initial State0Default off
  3. Click Save

Step 4: Add Alarm Buzzer (Optional)

  1. Click the Add Peripheral button a third time

  2. Fill in the configuration:

    FieldValueDescription
    Peripheral IDbuzzer_alarmAlarm buzzer
    NameAlarm BuzzerDisplay name
    Peripheral TypeGPIO Digital Output (type: 12)Buzzer control
    Pin Configuration25IN connected to GPIO25
    Initial State0Default off
  3. Click Save


Step 2: Configure Peripheral Execution Rules ​

Rule 1: Timed Temperature/Humidity Data Collection

  1. Click Peripheral Execution in the left menu

  2. Click the Add Rule button

  3. Fill in basic configuration:

    • Rule Name: Timed Temp/Humidity Collection
    • Report Data: ✅ Enabled
    • Enable: ✅ Enabled
  4. Configure trigger:

    • Trigger Type: Select Timer Trigger
    • Timer Mode: Select Fixed Interval
    • Interval Time: 30 (30 seconds)
  5. Configure actions (3 actions needed):

    • Action 1: Select Sensor Read, target dht22_room, field temperature
    • Action 2: Select Sensor Read, target dht22_room, field humidity
    • Action 3: Select OLED Display, target oled1 (must be configured in advance), template:
      # Env Monitor
      Temp: ${dht22_room.temperature}°C
      Humidity: ${dht22_room.humidity}%
  6. Click Save


Rule 2: Temperature Exceeds Threshold - Turn On Fan

  1. Create rule, name: High Temp Turn On Fan

  2. Trigger configuration:

    • Trigger Type: Select Event Trigger
    • Event ID: Enter ds:dht22_room_temperature
    • Operator: Select Greater Than (>)
    • Threshold: 30 (30°C)
  3. Action configuration:

    • Action Type: Select High Level
    • Target Peripheral: Select relay_fan
  4. Click Save


Rule 3: Temperature Recovers - Turn Off Fan

  1. Create rule, name: Temp Normal Turn Off Fan

  2. Trigger configuration:

    • Trigger Type: Select Event Trigger
    • Event ID: Enter ds:dht22_room_temperature
    • Operator: Select Less Than or Equal (<=)
    • Threshold: 26 (26°C)
  3. Action configuration:

    • Action Type: Select Low Level
    • Target Peripheral: Select relay_fan
  4. Click Save

💡 Tip: Turn on at 30°C, turn off at 26°C, forming a 4°C hysteresis band to prevent frequent switching


Rule 4: High Temperature Alarm (Exceeds 35°C)

  1. Create rule, name: High Temperature Alarm

  2. Trigger configuration:

    • Trigger Type: Select Event Trigger
    • Event ID: Enter ds:dht22_room_temperature
    • Operator: Select Greater Than (>)
    • Threshold: 35 (35°C emergency alarm)
  3. Action configuration (2 actions needed):

    • Action 1: Select Flash, target buzzer_alarm, interval 500 (500ms)
    • Action 2: Select Trigger Event, event ID high_temperature
  4. Click Save


Method 2: JSON Configuration File Import ​

Workflow Diagram ​

┌──────────────┐    Every 30s   ┌────────────────┐
│ Timer Trigger │──────────────>│ Read DHT22 Temp │
└──────────────┘               │ Read DHT22 Humi │
                               │ OLED Display    │
                               └───────┬────────┘
                                       │ Collection results cached as
                                       │ ds:dht22_room_temperature
                                       │ ds:dht22_room_humidity
                                       ▼
                               ┌────────────────┐
                               │ Event Trigger Rule Matching │
                               └───┬────┬───┬───┘
                                   │    │   │
                     Temp>30°C ────┘    │   └───── Temp>35°C
                          │             │              │
                          ▼             ▼              ▼
                   ┌──────────┐  ┌──────────┐  ┌──────────────┐
                   │ Fan ON   │  │ Fan OFF  │  │ Buzzer Alarm │
                   │ (Relay HIGH)│ │ (Relay LOW)│ │ + Event Report│
                   └──────────┘  └──────────┘  └──────────────┘
                                 Temp≤26°C

Parameter Adjustment Recommendations ​

ParameterRecommended ValueDescription
Collection Interval30sIndoor environment changes slowly; 30s is sufficient
Fan ON Threshold30°CAdjust based on actual needs
Fan OFF Threshold26°C4°C below fan ON threshold, forming hysteresis anti-jitter
Alarm Threshold35°CEmergency alarm, above normal control threshold
Buzzer Flash Interval500ms0.5s interval alert tone

Hysteresis Control Explanation ​

The 4°C difference between the ON threshold (30°C) and OFF threshold (26°C) forms a hysteresis band, preventing rapid relay switching (jitter) when temperature fluctuates near the threshold, extending relay lifespan.

Extension Suggestions ​

  1. Multi-level Temperature Control: Add intermediate temperature ranges, use PWM to control fan speed
  2. Humidity Linkage: Turn on dehumidification equipment when humidity is too high
  3. Time-based Control: Use different thresholds for day/night (via daily time point trigger to switch rule enable states)
  4. Remote Override: Keep platform trigger to allow IoT platform remote forced control

FastBee | fastbee.cn