Natural Gas leak/Air quality sensor, ESPhome and MQ2 sensor

This a simple gas leak/smoke sensor that will beep an alarm when triggered and send a notification to android based on ESP8266 nodeMCU. The alarm will sound every 2 seconds and its the same tone as microwave oven. I got a speaker from an old motherboard but any small speaker will do. I have one esp8266 in the basement between the furnace and the water heater and one esp8266 in the kitchen above the stove. Natural gas (mostly methane) is less dense than air.



image

I use the kitchen one for other things as well


I dont have a picture with the 3d printed case

esphome:
  name: esphome-web-0cd631
  friendly_name: Furnace gas detector

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxx"

ota:
  password: "xxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: 192.168.2.xxx

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-0Cd631"
    password: "xxxxxxxxxx"

captive_portal:
    
#Define sensors
sensor:
  - platform: adc
    pin: A0
    name: "Smoke sensor"
    id: smk
    update_interval: 1s
    filters:
      - multiply: 100
    unit_of_measurement: "%"
    icon: "mdi:percent"


#Speaker output
output:
  - platform: esp8266_pwm
    pin: D1
    id: buzzer_output

rtttl:
  output: buzzer_output

interval:                                                                               
  - interval: 2s                                                                        
    then:                                                                               
      if:                                                                               
        condition:
          and:
          - switch.is_on: buzzerswitch
          - sensor.in_range:                                                              
             id: smk
             above: 30                                                                   
        then:                                                                           
          - rtttl.play: 'siren:d=16,o=6,b=800:b'

switch:
  - platform: gpio
    pin: D4 #Internal LED switching
    name: "NodeMCU Internal LED"
    id: internal_led
    inverted: true
    restore_mode: ALWAYS_ON

  - platform: gpio
    pin: D2 #choose a free pin here, just to make ESPHome happy. we do not need it for anything
    name: "Buzzer"
    id: buzzerswitch
    restore_mode: ALWAYS_ON
1 Like

Good project. Thanks for sharing the connections and the code. I am trying to implement similar one for my home as well, waiting for the parts.

Code to test speakers. I activate it once a month.

button:
  - platform: template
    name: Buzzer Test
    id: buzzer_test
    on_press:
      - rtttl.play: 'mario:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6'

Awesome info, thanks for sharing. Iā€™m also curious what the adc platform is. Looking into it.

Thanks for the test code. Its really useful.

To save the internal LED being burnt out with extended power on, I have slightly modified the code you have shared as given below. I was testing this with esp32 based board, now I have moved this to d1 mini.

interval:                                                                               
  - interval: 2s                                                                        
    then:                                                                               
      if:                                                                               
        condition:
          and:
          - switch.is_on: buzzerswitch
          - sensor.in_range:                                                              
             id: gassensor
             above: 30                                                                   
        then:                                                                           
          - rtttl.play: 'siren:d=16,o=6,b=800:b'
          - switch.turn_on: internal_led
        else: 
          - switch.turn_off: internal_led