Scenario: DHT Temp/Humidity Sensor + Relay Linkage
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
| Module | Model | Qty | Wiring |
|---|---|---|---|
| Temp/Humidity Sensor | DHT22 | 1 | DATA→GPIO4 |
| Relay Module | 5V single channel | 1 | IN→GPIO15 |
| OLED Display | SSD1306 128x64 | 1 | SDA→GPIO21, SCL→GPIO22 |
| (Optional) Buzzer | Active buzzer | 1 | IN→GPIO25 |
Complete Configuration Process
Method 1: Web Interface Configuration (Recommended)
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
- Open a browser and navigate to the ESP32 IP address
- After logging in, click Peripheral Configuration in the left menu
Step 2: Add DHT22 Temperature/Humidity Sensor
Click the Add Peripheral button
Fill in the configuration:
Field Value Description Peripheral ID dht22_roomRoom temp/humidity Name Room Temp/HumidityDisplay name Peripheral Type Universal Sensor (type: 38) DHT driver Pin Configuration 4DATA connected to GPIO4 Sensor Category DHTDHT series Sensor Model DHT22DHT22 has higher accuracy Collection Interval 50005 seconds Click Save
Step 3: Add Fan Relay
Click the Add Peripheral button again
Fill in the configuration:
Field Value Description Peripheral ID relay_fanFan relay Name Fan RelayDisplay name Peripheral Type GPIO Digital Output (type: 12) Relay control Pin Configuration 15IN connected to GPIO15 Initial State 0Default off Click Save
Step 4: Add Alarm Buzzer (Optional)
Click the Add Peripheral button a third time
Fill in the configuration:
Field Value Description Peripheral ID buzzer_alarmAlarm buzzer Name Alarm BuzzerDisplay name Peripheral Type GPIO Digital Output (type: 12) Buzzer control Pin Configuration 25IN connected to GPIO25 Initial State 0Default off Click Save
Step 2: Configure Peripheral Execution Rules
Rule 1: Timed Temperature/Humidity Data Collection
Switch to Peripheral Execution Management tab
Click the Add Rule button
Fill in basic configuration:
- Rule Name:
Timed Temp/Humidity Collection - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Configure trigger:
- Trigger Type: Select Timer Trigger
- Timer Mode: Select Fixed Interval
- Interval Time:
30(30 seconds)
Configure actions (3 actions needed):
- Action 1: Select Sensor Read, target
dht22_room, fieldtemperature - Action 2: Select Sensor Read, target
dht22_room, fieldhumidity - Action 3: Select OLED Display, target
oled1(must be configured in advance), template:# Env Monitor Temp: ${dht22_room.temperature}°C Humidity: ${dht22_room.humidity}%
- Action 1: Select Sensor Read, target
Click Save
Rule 2: Temperature Exceeds Threshold - Turn On Fan
Create rule, name:
High Temp Turn On FanTrigger configuration:
- Trigger Type: Select Event Trigger
- Event ID: Enter
ds:dht22_room_temperature - Operator: Select
Greater Than (>) - Threshold:
30(30°C)
Action configuration:
- Action Type: Select High Level
- Target Peripheral: Select
relay_fan
Click Save
Rule 3: Temperature Recovers - Turn Off Fan
Create rule, name:
Temp Normal Turn Off FanTrigger configuration:
- Trigger Type: Select Event Trigger
- Event ID: Enter
ds:dht22_room_temperature - Operator: Select
Less Than or Equal (<=) - Threshold:
26(26°C)
Action configuration:
- Action Type: Select Low Level
- Target Peripheral: Select
relay_fan
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)
Create rule, name:
High Temperature AlarmTrigger configuration:
- Trigger Type: Select Event Trigger
- Event ID: Enter
ds:dht22_room_temperature - Operator: Select
Greater Than (>) - Threshold:
35(35°C emergency alarm)
Action configuration (2 actions needed):
- Action 1: Select Flash, target
buzzer_alarm, interval500(500ms) - Action 2: Select Trigger Event, event ID
high_temperature
- Action 1: Select Flash, target
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°CParameter Adjustment Recommendations
| Parameter | Recommended Value | Description |
|---|---|---|
| Collection Interval | 30s | Indoor environment changes slowly; 30s is sufficient |
| Fan ON Threshold | 30°C | Adjust based on actual needs |
| Fan OFF Threshold | 26°C | 4°C below fan ON threshold, forming hysteresis anti-jitter |
| Alarm Threshold | 35°C | Emergency alarm, above normal control threshold |
| Buzzer Flash Interval | 500ms | 0.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
- Multi-level Temperature Control: Add intermediate temperature ranges, use PWM to control fan speed
- Humidity Linkage: Turn on dehumidification equipment when humidity is too high
- Time-based Control: Use different thresholds for day/night (via daily time point trigger to switch rule enable states)
- Remote Override: Keep platform trigger to allow IoT platform remote forced control
