Hello everyone!

Today, I’d like to introduce my new project — ESP32 Air Quality Station HOME, which lets you monitor indoor air quality in real time, including measuring CO₂ levels, temperature, humidity, and Volatile Organic Compounds (VOC).

The station not only displays data on an OLED screen but also sends it to the ThingSpeak cloud platform and allows you to manage everything through a Telegram bot. Essentially, it’s a complete solution for a smart home!

Why Do You Need an Air Quality Monitoring Station?

Clean air is key to feeling good and staying productive. High concentrations of carbon dioxide (CO₂) indoors can lead to headaches, fatigue, and decreased concentration. Controlling temperature and humidity is also crucial for health and comfort: dry air negatively affects mucous membranes, while excessive humidity can lead to mold.

Beyond CO₂, there are also Volatile Organic Compounds (VOCs) in the air. They can come from household chemicals, furniture, and building materials. Long-term exposure to elevated VOC levels may cause irritation of mucous membranes, allergies, and other unpleasant effects.

The idea is simple: build a compact device that constantly “keeps an eye” on the quality of the air in your home or office.

Key Features of the Project

ESP32 Controller

The main “brain” of the project is the ESP32 board, which has a built-in Wi-Fi module. This makes it easy to send data to the internet (ThingSpeak) and enables interaction with a Telegram bot.

Sensors

  • MH-Z19B for measuring CO₂ concentration. It’s a fairly accurate sensor that works on the principle of non-dispersive infrared (NDIR) spectroscopy.
  • BME680 for measuring temperature, humidity, and VOC (and also pressure if you need it). It’s an excellent all-purpose sensor by Bosch.

Data Display

  • OLED SSD1306: Displays current parameters right on the device itself.
  • RGB LED: An indicator of CO₂ level — green for low, blue for medium, and red for high.

Cloud Integration

The station sends data to ThingSpeak — a convenient platform for visualizing and analyzing IoT (Internet of Things) data. You can set up graphs there, observe long-term statistics, and even process data using MATLAB.

Telegram Bot

For remote management and data retrieval, you can use your Telegram bot: request the latest measurements, change settings (such as the data-upload interval to ThingSpeak), set CO₂ thresholds, and enable or disable debug information.

Wiring Diagram

Before you start assembling, make sure you have the following components:

  • ESP32 DevKit (or any other compatible board with Wi-Fi).
  • MH-Z19B (CO₂ sensor).
  • BME680 (sensor for temperature, humidity, pressure, and VOC).
  • OLED SSD1306 (usually a 0.96" or 1.3" display).
  • RGB LED and a few resistors (typically 220 Ω) for current limiting.
  • Wires for a breadboard or for soldering.
  • A 5 V power supply (you can use USB from a computer or an adapter).

Brief (Logical) Diagram

  1. MH-Z19B
    • Power: 5 V and GND.
    • TX/RX to the corresponding pins on the ESP32 (Serial or a software serial).
  2. BME680
    • Power: 3.3 V and GND.
    • SDA and SCL to the corresponding I2C pins on the ESP32 (usually GPIO 21 and GPIO 22 unless changed in the code).
  3. OLED SSD1306
    • Power: 3.3 V and GND.
    • SDA and SCL to the same I2C lines as the BME680.
  4. RGB LED
    • If it’s a common-cathode LED, connect the cathode to ground through a resistor, or if it’s a common-anode LED, connect the anode to 3.3 V and each color pin to the ESP32 through separate resistors. Make sure to verify the LED’s polarity before powering it up!

It’s important not to mix up the pins and to check the polarity before switching on the power!

Software Part

Dependencies

The project uses the following libraries:

  • WiFi.h and HTTPClient.h for network communication.
  • Wire.h for the I2C bus.
  • Adafruit GFX Library, Adafruit SSD1306 for the OLED.
  • Adafruit BME680 Library for the BME680 sensor.
  • MHZ19 for the MH-Z19B CO₂ sensor.
  • UniversalTelegramBot for connecting to the Telegram bot.
  • ArduinoJson for parsing and generating JSON data.
  • Preferences for storing settings in the ESP32’s non-volatile memory.

You can install these libraries via the “Library Manager” in the Arduino IDE or using PlatformIO.

The config.h File

A separate file named config.h specifies the main parameters:

#ifndef CONFIG_H
#define CONFIG_H

// Wi-Fi
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"

// ThingSpeak
#define THINGSPEAK_API_KEY "your_api_key"

// Telegram
#define TELEGRAM_TOKEN "your_bot_token"
#define TELEGRAM_CHAT_ID "your_chat_id"

// CO₂ Threshold Values
#define CO2_LOW_THRESHOLD 800
#define CO2_MEDIUM_THRESHOLD 1200

#endif

Make sure to replace these fields with your own. In Telegram, you can create your own bot via @BotFather and get your TOKEN; you’ll find your CHAT_ID using special Telegram bots or by logging incoming messages.

Main Code (main_ru.ino or main_en.ino)

In the main code:

  1. Include the required libraries and config.h.
  2. Initialize the MH-Z19B and BME680 sensors.
  3. Configure the Wi-Fi connection.
  4. Set up periodic data uploads to ThingSpeak.
  5. Handle Telegram bot commands.
  6. Update the display and the LED color based on sensor readings.

Key functions:

  • void setup() — Initializes peripherals.
  • void loop() — The main loop that every few seconds reads the data, uploads it to ThingSpeak (if it’s time), and checks for incoming Telegram messages.

Working with the Telegram Bot

Once the bot is configured in config.h, the station can receive the following commands from you (for example):

  • /start — Displays a welcome message.
  • /status — Returns the current status: CO₂, temperature, humidity, VOC.
  • /setapikey <api_key> — Changes the ThingSpeak API key.
  • /setinterval — Sets the data upload interval (usually at least 300 seconds to avoid exceeding ThingSpeak limits).
  • /setthreshold — Sets new CO₂ threshold values.
  • /getsettings — Shows the current settings (SSID, thresholds, interval).
  • /toggledebug — Enables or disables debug output on the OLED display.

This way, you don’t need to reflash the microcontroller every time you change a setting — just send a command through Telegram.

Viewing Data on ThingSpeak

The ThingSpeak platform is convenient for building graphs: you can view the history of CO₂ changes over several days or weeks, track the rise/fall of VOC, and compare it with temperature and humidity. If you have MATLAB code, you can run it directly on ThingSpeak for additional analysis.

Important: On the free plan, ThingSpeak recommends sending data no more frequently than once every 15 seconds. Sending data too often may result in blocking.

Advanced Capabilities

  • Automation
    A signal about exceeding a CO₂ threshold can turn on ventilation, air conditioning, or a supply-exhaust ventilation system (via a relay).
  • Notifications
    When measurements go beyond safe limits, send a message to Telegram or email.
  • Mobile App
    Using standard Telegram widgets, you can create a convenient button for an instant status check.
  • Configurable Calibration
    The MH-Z19B sensor supports calibration functions. If necessary, you can implement both automatic and manual calibration.

Conclusion

The ESP32 Air Quality Station HOME project is a great example of how modern electronics and the Internet of Things can simplify life and make it more comfortable. A DIY station constantly monitors air quality, provides handy graphs on ThingSpeak, and allows management through a Telegram bot.

If you have any questions or want to share improvements, leave comments! I’ll be happy to hear your feedback and ideas.


I hope this article was helpful! If you decide to build a similar station, feel free to ask questions in the comments and share your results. Now, onward to a world of cleaner and safer air!

ESP32 Air Quality Station