Skip to content

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 ​

ModuleModelQtyWiring
Smoke SensorMQ-21AO→GPIO34
BuzzerActive buzzer module1IN→GPIO25
LED IndicatorRed LED1→GPIO2 (onboard)

Complete Configuration Process ​

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

  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 MQ-2 Smoke Sensor

  1. Click the Add Peripheral button

  2. Fill in the configuration:

    FieldValueDescription
    Peripheral IDsmoke_sensorSmoke sensor MQ2
    NameSmoke Sensor MQ2Display name
    Peripheral TypeGPIO Analog Input (type: 15)ADC collection
    Pin Configuration34AO connected to GPIO34 (ADC1)
    Attenuation11dB (3)0-3.3V range
    Resolution12-bit0-4095
    Sample Rate55Hz
  3. Click Save

Step 3: Add Alarm Buzzer

  1. Click the Add Peripheral button again

  2. Fill in the configuration:

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

Step 4: Add Alarm LED

  1. Click the Add Peripheral button a third time

  2. Fill in the configuration:

    FieldValueDescription
    Peripheral IDled_alarmAlarm LED
    NameAlarm LEDDisplay name
    Peripheral TypeGPIO Digital Output (type: 12)LED control
    Pin Configuration2Onboard LED
    Initial State0Default off
  3. Click Save


Step 2: Configure Peripheral Execution Rules ​

Rule 1: Timed Smoke Concentration Collection

  1. Click Peripheral Execution in the left menu

  2. Click the Add Rule button

  3. Fill in basic configuration:

    • Rule Name: Smoke Concentration Collection
    • Report Data: ✅ Enabled
    • Enable: ✅ Enabled
  4. Configure trigger:

    • Trigger Type: Select Timer Trigger
    • Timer Mode: Select Fixed Interval
    • Interval Time: 5 (5 seconds; smoke detection requires high response speed)
  5. Configure action:

    • Action Type: Select Sensor Read
    • Target Peripheral: Select smoke_sensor
    • Data Field: analog
  6. Click Save


Rule 2: Smoke Exceedance Alarm

  1. Create rule, name: Smoke Exceedance Alarm

  2. Trigger 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)
  3. Action configuration (3 actions needed):

    • Action 1: Select Flash, target buzzer_01, interval 300 (300ms rapid beeping)
    • Action 2: Select Flash, target led_alarm, interval 500 (500ms flashing)
    • Action 3: Select Trigger Event, event ID smoke_alarm
  4. Click Save


Rule 3: Smoke Back to Normal

  1. Create rule, name: Smoke Back to Normal

  2. Trigger configuration:

    • Trigger Type: Select Event Trigger
    • Event ID: Enter ds:smoke_sensor_analog
    • Operator: Select Less Than (<)
    • Threshold: 1000 (below alarm threshold, forming hysteresis)
  3. Action configuration (2 actions needed):

    • Action 1: Select Low Level, target buzzer_01
    • Action 2: Select Low Level, target led_alarm
  4. 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 ​

ParameterRecommended ValueDescription
Collection Interval5sSmoke detection requires higher response speed
Alarm Threshold2000Raw ADC value; needs calibration based on environment
Recovery Threshold1000Below alarm threshold, forming hysteresis band
Buzzer Flash Interval300msRapid beeping to indicate danger

Precautions ​

  1. MQ-2 sensor requires 5~10 minutes preheat time to stabilize
  2. New sensor should be preheated for over 24 hours before first use
  3. Alarm threshold needs calibration based on actual environment and application scenario
  4. Regular sensor maintenance is recommended (dust accumulation affects sensitivity)

FastBee | fastbee.cn