This project is an ESP32-based Air Quality Monitoring System that measures and reports environmental data, including:
- Temperature & Humidity (GY-SGP30)
- COβ Levels (SCD41)
- Particulate Matter (PM1.0, PM2.5, PM10) (PMS7003)
- Air Quality Index (AQI) (Calculated from PM data)
- TVOC, Hydrogen, Ethenol (GY-SGP30)
The collected data is displayed on an SSD1306 OLED screen and published to an MQTT broker every 1 minute for integration with platforms like Home Assistant.
- Temperature and Humidity monitoring (GY-SGP30)
- CO2 level monitoring (SCD41)
- Particulate Matter monitoring (PMS7003)
- PM1.0, PM2.5, and PM10 measurements
- Total volatile organic compounds(TVOC) including Hydrogen and Ethenol (GY-SGP30)
- Standard AQI calculation
- OLED Display showing real-time sensor readings
- Auto display shutdown after 5 minutes to prevent burn-in
- Boot button (GPIO 0) functions as OLED toggle switch
- Display automatically reactivates when new button press detected
- Graceful offline operation when WiFi is unavailable
- Automatic WiFi reconnection attempts
- MQTT integration with Home Assistant when network available
- Smart connection management:
- Initial connection attempts with 15-second timeout
- After 3 failed attempts, retries every 15 minutes
- Automatic recovery when network becomes available
- No impact on sensor operation during network outages
- Local display via OLED
- Serial output for debugging
- MQTT publishing to Home Assistant when connected
- Automatic sensor discovery in Home Assistant
π **ESP32_Air_Quality_Sensor**
βββ π `sketch.ino` # Main entry point
βββ π `README.md` # Project documentation
βββ π `secrets.h` # Wi-Fi & MQTT credentials (template included but must be updated)
βββ π `LICENSE` # License file
βββ π `include` # Header files (.h)
β βββ π `lib` # Library component headers
β β βββ π `mqtt_client.h` # MQTT connection management
β β βββ π `oled_display.h` # OLED display control
β β βββ π `scheduler.h` # Task scheduling
β β βββ π `enhanced_aqi.h` # Enhanced AQI calculation
β β βββ π `wifi_manager.h` # Manages Wi-Fi connection
β βββ π `sensors` # Sensor headers
β βββ π `sgp30_sensor.h` # SGP30 sensor interface
β βββ π `scd41_sensor.h` # SCD41 sensor interface
β βββ π `pms7003_sensor.h` # PMS7003 sensor interface
βββ π `src` # Implementation files (.cpp)
βββ π `lib` # Library component implementations
β βββ π `mqtt_client.cpp` # MQTT connection implementation
β βββ π `oled_display.cpp` # OLED display implementation
β βββ π `scheduler.cpp` # Task scheduling implementation
β βββ π `enhanced_aqi.cpp` # Enhanced AQI implementation
β βββ π `wifi_manager.cpp` # Wi-Fi management implementation
βββ π `sensors` # Sensor implementations
βββ π `sgp30_sensor.cpp` # SGP30 sensor implementation
βββ π `scd41_sensor.cpp` # SCD41 sensor implementation
βββ π `pms7003_sensor.cpp` # PMS7003 sensor implementation
| Component | Description |
|---|---|
| ESP32 Dev Board | Wi-Fi-enabled microcontroller |
| SGP30 Sensor | Measures temperature, humidity, TVOC, H2 & Ethenol |
| SCD41 Sensor | Measures COβ concentration |
| PMS7003 Sensor | Measures particulate matter (PM1.0, PM2.5, PM10) |
| SSD1306 OLED | 0.96-inch I2C OLED screen |
| MQTT Broker | Receives and processes sensor data |
Install the following libraries in Arduino IDE:
- WiFi (Built-in for ESP32)
- PubSubClient (by Nick O'Leary)
- Adafruit SGP30 Sensor (by Adafruit)
- Adafruit GFX Library (by Adafruit)
- Adafruit SSD1306 (by Adafruit)
- SparkFun SCD4x Arduino Library (for SCD41 sensor)
- PMS Library (for PMS7003 sensor)
- Open Arduino IDE
- Go to Sketch β Include Library β Manage Libraries
- Search for and install the required libraries
-
Install ESP32 Board Support
- Open Arduino IDE
- Go to File β Preferences
- In Additional Board Manager URLs, add:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json - Go to Tools β Board β Boards Manager
- Search for ESP32 and install the latest version
-
Select the Board
- Go to Tools β Board
- Select ESP32 Dev Module
- Set Upload Speed: 230400
- Flash Mode: QIO
- Partition Scheme: Default
| ESP32 Pin | Sensor | Description |
|---|---|---|
| 3.3V | All Sensors | Power Supply |
| GND | All Sensors | Ground |
| GPIO21 | SDA (OLED, SGP30, SCD41) | I2C Data |
| GPIO22 | SCL (OLED, SGP30, SCD41) | I2C Clock |
| GPIO14 | RX (PMS7003) | UART Receive |
| GPIO27 | TX (PMS7003) | UART Transmit |
This project uses NTP (Network Time Protocol) to sync the correct time. Adjust the timezone in wifi_manager.h:
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -8 * 3600; // Pacific Standard Time (PST)
const int daylightOffset_sec = 0; // No DST (Set to 3600 for 1 hour DST adjustment)Before uploading the code, create a secrets.h file next to your .ino file with your Wi-Fi and MQTT credentials:
#ifndef SECRETS_H
#define SECRETS_H
#define WIFI_SSID "Your_WiFi_SSID"
#define WIFI_PASSWORD "Your_WiFi_Password"
#define MQTT_SERVER "Your_MQTT_Broker_IP"
#define MQTT_USER "Your_MQTT_Username"
#define MQTT_PASS "Your_MQTT_Password"
#endif // SECRETS_H- Connect the ESP32 to your PC via USB
- Open the Arduino IDE
- Select ESP32 Dev Module under Tools β Board
- Open Tools β Port and select the correct COM port
- Click the Upload button
- Open the Serial Monitor (baud rate:
115200) to check the logs
Temp: 70.97 F
Humidity: 34.05 %
CO2: 674 ppm
PM: 81 / 121 / 126 ug
AQI: 184
TVOC: 30 ppb
H2: 10000 res
Ethanol: 13000 res
- Ensure SDA (GPIO21) & SCL (GPIO22) are correctly connected.
- Check I2C address (default is
0x3C). - Try scanning I2C devices using an I2C scanner sketch.
- Double-check SSID and Password in
secrets.h. - Ensure your Wi-Fi network is 2.4 GHz (ESP32 doesn't support 5 GHz).
- Verify the MQTT broker IP and credentials in
secrets.h. - Ensure the broker is running and accessible.
- Try disabling MQTT authentication temporarily.
- Additional Sensors β Support for VOC, NOβ, and other air quality sensors.
- LoRa & Meshtastic Integration β Use LoRaWAN or Meshtastic to transmit sensor data over long distances.
Let me know if you have any issues. Feel free to contribute and improve this project! π

