Scenario: Smoke Alarm + Buzzer + MQTT Report
Scenario: Smoke Alarm + Buzzer + MQTT Report
Scenario Description
Use an MQ-2 smoke sensor to collect smoke concentration via ADC. When the concentration exceeds a set threshold, trigger a buzzer alarm and report device alarm events to the IoT platform via MQTT, achieving both local and remote dual alarm.
Hardware List
| Module | Model | Qty | Wiring |
|---|---|---|---|
| Smoke Sensor | MQ-2 | 1 | AO→GPIO34 |
| Buzzer | Active buzzer module | 1 | IN→GPIO25 |
| LED Indicator | Red LED | 1 | →GPIO2 (onboard) |
Complete Configuration Process
Method 1: Web Interface Configuration (Recommended)
This scenario requires first adding the smoke ADC, buzzer, and indicator LED, then using peripheral execution rules for threshold alarm and MQTT reporting.
The smoke alarm scenario is broken down in a closed loop: ADC collects concentration, rule evaluates threshold, buzzer and indicator execute actions, MQTT reports alarm event.
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 MQ-2 Smoke Sensor
Click the Add Peripheral button
Fill in the configuration:
Field Value Description Peripheral ID smoke_sensorSmoke sensor MQ2 Name Smoke Sensor MQ2Display name Peripheral Type GPIO Analog Input (type: 15) ADC collection Pin Configuration 34AO connected to GPIO34 (ADC1) Attenuation 11dB(3)0-3.3V range Resolution 12-bit0-4095 Sample Rate 55Hz Click Save
Step 3: Add Alarm Buzzer
Click the Add Peripheral button again
Fill in the configuration:
Field Value Description Peripheral ID buzzer_01Alarm 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 4: Add Alarm LED
Click the Add Peripheral button a third time
Fill in the configuration:
Field Value Description Peripheral ID led_alarmAlarm LED Name Alarm LEDDisplay name Peripheral Type GPIO Digital Output (type: 12) LED control Pin Configuration 2Onboard LED Initial State 0Default off Click Save
Step 2: Configure Peripheral Execution Rules
Rule 1: Timed Smoke Concentration Collection
Switch to Peripheral Execution Management tab
Click the Add Rule button
Fill in basic configuration:
- Rule Name:
Smoke Concentration Collection - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Configure trigger:
- Trigger Type: Select Timer Trigger
- Timer Mode: Select Fixed Interval
- Interval Time:
5(5 seconds; smoke detection requires high response speed)
Configure action:
- Action Type: Select Sensor Read
- Target Peripheral: Select
smoke_sensor - Data Field:
analog
Click Save
Rule 2: Smoke Exceedance Alarm
Create rule, name:
Smoke Exceedance AlarmTrigger configuration:
- Trigger Type: Select Event Trigger
- Event ID: Enter
ds:smoke_sensor_analog - Operator: Select
Greater Than (>) - Threshold:
2000(ADC value, approximately medium smoke concentration)
Action configuration (3 actions needed):
- Action 1: Select Flash, target
buzzer_01, interval300(300ms rapid beeping) - Action 2: Select Flash, target
led_alarm, interval500(500ms flashing) - Action 3: Select Trigger Event, event ID
smoke_alarm
- Action 1: Select Flash, target
Click Save
Rule 3: Smoke Back to Normal
Create rule, name:
Smoke Back to NormalTrigger configuration:
- Trigger Type: Select Event Trigger
- Event ID: Enter
ds:smoke_sensor_analog - Operator: Select
Less Than (<) - Threshold:
1000(below alarm threshold, forming hysteresis)
Action configuration (2 actions needed):
- Action 1: Select Low Level, target
buzzer_01 - Action 2: Select Low Level, target
led_alarm
- Action 1: Select Low Level, target
Click Save
💡 Tip: Alarm threshold 2000, recovery threshold 1000, forming a 1000 hysteresis band to prevent repeated alarming
Method 2: JSON Configuration File Import
Working Principle
- MQ-2 sensor outputs analog voltage; higher smoke concentration = higher voltage
- ADC reading range 0~4095 (corresponding to 0~3.3V)
- Threshold 2000 approximately corresponds to medium smoke concentration (needs calibration based on actual environment)
- Recovery threshold 1000 forms hysteresis to avoid repeated alarming near the threshold
Parameter Adjustment
| Parameter | Recommended Value | Description |
|---|---|---|
| Collection Interval | 5s | Smoke detection requires higher response speed |
| Alarm Threshold | 2000 | Raw ADC value; needs calibration based on environment |
| Recovery Threshold | 1000 | Below alarm threshold, forming hysteresis band |
| Buzzer Flash Interval | 300ms | Rapid beeping to indicate danger |
Precautions
- MQ-2 sensor requires 5~10 minutes preheat time to stabilize
- New sensor should be preheated for over 24 hours before first use
- Alarm threshold needs calibration based on actual environment and application scenario
- Regular sensor maintenance is recommended (dust accumulation affects sensitivity)
