# titaniummonkey/esp32-air-quality-sensor architecture

> Architecture diagram of the GitHub repository titaniummonkey/esp32-air-quality-sensor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/titaniummonkey/esp32-air-quality-sensor
- Repository: https://github.com/titaniummonkey/esp32-air-quality-sensor

## Overview

##### Project Type & Purpose
- Type: Embedded IoT device firmware with local display + cloud integration.
- Purpose: Read air‐quality sensors, display results on an OLED, and publish data over MQTT for Home Assistant.

##### File Structure
Top-level:
- sketch.ino – application entry point & orchestrator
- / – public headers, divided into lib/ (MQTT, display, scheduler, Wi-Fi) and sensors/
- src/ – implementations mirroring include/
Patterns:
– Modular separation: “lib” components vs “sensor” drivers
– Hardware abstraction layers (I2C, UART) in sensor modules & display
– Central scheduler triggering periodic tasks

##### Key Insights from README
- Sensor set: SGP30, SCD41, PMS7003
- SSD1306 over I2C
- Connectivity: Wi-Fi manager with smart retry logic → MQTT client → Home Assistant
- Behavior:
– 1-minute measurement cycle
– Auto display shutdown & button toggle
– Offline first (sensors run even if Wi-Fi down)
– NTP time sync

##### Main Components & Their Interactions
A. Hardware Layer (Physical)
1. Sensors (I2C: SGP30, SCD41; UART: PMS7003)
2. OLED Display (I2C SSD1306)
3. Boot Button (GPIO input)
4. ESP32 MCU

B. Firmware Modules (On MCU)
1. Scheduler
– Triggers sensor readings & display updates every minute
2. Sensor Drivers
– pms7003_sensor
– scd41_sensor
– sgp30_sensor
3. Data Aggregation & AQI Calculation
– enhanced_aqi
4. Controller (oled_display)
– Renders readings on SSD1306, handles timeout & button toggles
5. Wi-Fi Manager
– Connect/disconnect logic, retry intervals, NTP sync
6. MQTT Client
– Publishes JSON payload to broker; supports Home Assistant auto-discovery
7. Application Orchestrator (sketch.ino)
– Initializes modules, wires callbacks, starts scheduler

C. External Services
1. NTP Server (pool.ntp.org) for time sync
2. MQTT Broker (local or cloud)
3. Home Assistant (subscribes to MQTT topics)

##### Relationships & Data Flows
- Scheduler → invokes Sensor Drivers → returns raw measurements
- Measurements → passed to AQI Calculator → produces index
- Aggregated Data → 1) Display Controller → OLED  2) MQTT Client → Broker
- Wi-Fi Manager ensures network availability; if offline, skip MQTT but continue sensors & display
- Boot Button → Display Controller (toggle on/off)
- On successful Wi-Fi connect → NTP sync → timestamps readings

##### Highlighted Architectural Patterns
- Layered design: Hardware abstraction → drivers → services (Wi-Fi/MQTT) → orchestration
- Modular partitioning: “lib” vs “sensors” promotes reusability
- Event-driven scheduling: decouples timing logic from sensor code
- Fail-safe offline operation: sensors always run, network is best‐effort

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Hardware Layer
    subgraph "Hardware Layer"
        direction TB
        SGP30["SGP30 Sensor"]:::hardware
        SCD41["SCD41 Sensor"]:::hardware
        PMS7003["PMS7003 Sensor"]:::hardware
        OLED["OLED Display (SSD1306)"]:::hardware
        Button["Boot Button"]:::hardware
        MCU["ESP32 MCU"]:::hardware
    end

    %% Firmware Modules
    subgraph "Firmware Modules"
        direction TB
        orchestrator["sketch.ino"]:::firmware
        Scheduler["scheduler"]:::firmware
        subgraph "Sensor Drivers"
            direction TB
            PmsDrv["pms7003_sensor"]:::firmware
            ScdDrv["scd41_sensor"]:::firmware
            SgpDrv["sgp30_sensor"]:::firmware
        end
        AQI["enhanced_aqi"]:::firmware
        DisplayCtrl["oled_display"]:::firmware
        WiFiMgr["wifi_manager"]:::firmware
        MQTT["mqtt_client"]:::firmware
    end

    %% External Services
    subgraph "External Services"
        direction TB
        NTP["NTP Server (pool.ntp.org)"]:::external
        Broker["MQTT Broker"]:::datastore
        HA["Home Assistant"]:::external
    end

    %% Connections
    orchestrator -->|initializes| Scheduler
    orchestrator -->|initializes| WiFiMgr
    Scheduler -->|triggers Every 1 min| PmsDrv
    Scheduler -->|triggers Every 1 min| ScdDrv
    Scheduler -->|triggers Every 1 min| SgpDrv
    PmsDrv -->|data| AQI
    ScdDrv -->|data| AQI
    SgpDrv -->|data| AQI
    AQI -->|aggregated data| DisplayCtrl
    AQI -->|aggregated data| MQTT
    DisplayCtrl -->|I2C| OLED
    Button -->|GPIO| DisplayCtrl
    WiFiMgr -->|connects| MQTT
    WiFiMgr -->|on success| NTP
    MQTT -->|publishes JSON over MQTT| Broker
    HA -->|subscribes| Broker

    %% Hardware to drivers
    PmsDrv -->|UART| PMS7003
    ScdDrv -->|I2C| SCD41
    SgpDrv -->|I2C| SGP30

    %% Click Events
    click orchestrator "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/sketch.ino"
    click Scheduler "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/include/lib/scheduler.h"
    click Scheduler "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/src/lib/scheduler.cpp"
    click PmsDrv "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/include/sensors/pms7003_sensor.h"
    click PmsDrv "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/src/sensors/pms7003_sensor.cpp"
    click ScdDrv "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/include/sensors/scd41_sensor.h"
    click ScdDrv "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/src/sensors/scd41_sensor.cpp"
    click SgpDrv "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/include/sensors/sgp30_sensor.h"
    click SgpDrv "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/src/sensors/sgp30_sensor.cpp"
    click DisplayCtrl "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/include/lib/oled_display.h"
    click DisplayCtrl "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/src/lib/oled_display.cpp"
    click WiFiMgr "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/include/lib/wifi_manager.h"
    click WiFiMgr "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/src/lib/wifi_manager.cpp"
    click MQTT "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/include/lib/mqtt_client.h"
    click MQTT "https://github.com/titaniummonkey/esp32-air-quality-sensor/blob/main/src/lib/mqtt_client.cpp"

    %% Styles
    classDef hardware fill:#AED6F1,stroke:#21618C,color:#1B2631;
    classDef firmware fill:#ABEBC6,stroke:#1D8348,color:#145A32;
    classDef external fill:#F9E79F,stroke:#B9770E,color:#7E5109;
    classDef datastore fill:#D7BDE2,stroke:#6C3483,color:#4A235A;
```
