Skip to main content

CO2 Monitoring with ESPHome

Help! My Head Hurts and My Nose is Burning: A CO2 Wake-Up Call πŸ˜΅πŸ’¨

For the past few years, I've been working from my small home office. This winter has been colder than usual, so I've been keeping the windows closed with little thought to the air around me. But then I started noticing a pattern β€” by the afternoon, headaches would creep in, and sometimes, upon reentering my office, I’d be hit with a strange, burning sensation in my nose. It smelled like iron, reminiscent of a bloody nose or the sharp tang of sticking my head into my fermentation chamber while brewing beer. Little did I know, these were all warning signs that something was off with my air quality.

Monitoring indoor air quality has become increasingly important, especially with the growing awareness of CO2 levels and their impact on health and productivity.

I'm a big fan of DIY solutions whenever possible, so I researched options available for hobbyist home electronics and found a great solution.

ESPHome: A Smart DIY Approach to Air Quality Monitoring πŸ”§

ESPHome is a powerful open-source framework that allows you to easily create custom firmware for ESP8266 and ESP32-based devices. It enables seamless integration with smart home platforms like Home Assistant and provides a user-friendly YAML-based configuration system. Whether you're a beginner or an experienced hobbyist, ESPHome simplifies the process of building and automating IoT devices.

Using an ESP8266 board, an SCD41 CO2 sensor, and a simple buzzer, I created a CO2 monitor for my office. This system integrates with Home Assistant and generates an audible alarm when CO2 levels rise too high.

Setting up ESPHome

Installation

Install ESPHome using the following command:

pip install esphome

Then create a configuration file for the sensor, e.g., co2.yaml. My configuration looks something like this:

esphome:
  name: co2

esp8266:
  board: nodemcuv2

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true

web_server:
  port: 80
  ota: false

logger:
  hardware_uart: uart1

api:
  encryption:
    key: !secret api_key
  actions:
    - action: rtttl_play
      variables:
        song_str: string
      then:
        - rtttl.play:
            rtttl: !lambda 'return song_str;'

ota:
  platform: esphome
  password: !secret ota_password

i2c:
  sda: D1
  scl: D2

output:
  - platform: esp8266_pwm
    pin: D3
    id: rtttl_out

rtttl:
  output: rtttl_out

sensor:
  - platform: scd4x
    co2:
      name: "CO2"
      id: co2
    temperature:
      name: "Temperature"
    humidity:
      name: "Humidity"

globals:
  - id: last_co2
    type: int
    restore_value: no
    initial_value: '0'

time:
  - platform: sntp
    on_time:
      - seconds: 0
        then: 
        - if:
            condition:
              lambda: |-
                int v = id(co2).state;
                return (v > 1000 && v > id(last_co2));
            then:
              - rtttl.play: 'siren:d=8,o=5,b=100:d,e,d,e,d,e,d,e'
              - output.turn_on: buzzer
              - delay: 5s
              - output.turn_off: buzzer
        - globals.set:
            id: last_co2
            value: !lambda return id(co2).state;

Put your WiFi credentials and other sensitive information in secrets.yaml.

Wiring πŸ”Œ

Connect the components as follows:

  • SCD41 VCC β†’ 3.3V
  • SCD41 GND β†’ GND
  • SCD41 SDA β†’ ESP8266 D1 (GPIO5)
  • SCD41 SCL β†’ ESP8266 D2 (GPIO4)
  • Buzzer Positive β†’ ESP8266 D3 (GPIO2)
  • Buzzer Negative β†’ GND

Uploading the Code πŸš€

  1. Connect the ESP8266 to your computer via USB.
  2. Run the following command to upload the firmware: sh esphome run co2.yaml
  3. Once uploaded, the ESP8266 will connect to your Wi-Fi network and start transmitting CO2 data.

Integration with Home Assistant 🏑

If you have Home Assistant running, simply add the ESPHome integration, and the sensor should automatically appear in your dashboard.

Conclusion πŸŽ‰

With just a few components and ESPHome, you can easily build a smart CO2 monitor for your home or office. This setup allows for real-time monitoring and integration with home automation platforms, providing valuable insights into indoor air quality.

Building this project not only improved my awareness of indoor air quality but also gave me peace of mind knowing that I have an early warning system when CO2 levels rise too high. If you've ever experienced similar symptoms or want to improve your home’s air quality, I highly recommend giving this project a try!

Comments