AirGradient Arduino Library

repository·master·Indexed 18 days ago

https://github.com/airgradienthq/arduino

Firmware for AirGradient open-source indoor and outdoor air quality monitors. Supports ESP8266 (Wemos D1 MINI) and ESP32-C3 microcontrollers, with integration for sensors including Plantower PMS5003/PMS5003T, SenseAir S8, Sensirion SGP41, and Sensirion SHT40. Includes integrated libraries for OLED displays (Adafruit GFX, SH110X, SSD1306), LED control (Adafruit NeoPixel), and cloud/local server API access.

Tokens
18.1K
Snippets
42
Records
88
Agent score
60%

What's inside airgradienthq-arduino

  1. Overview of U8g2_Arduino Monochrome Graphics Library

    master
    U8g2_Arduino is a monochrome graphics library for Arduino used to drive various display controllers. It is a version of the Uglib V2 library designed specifically for the Arduino ecosystem. It supports a wide range of monochrome displays (OLED, LCD, etc.) and provides a comprehensive set of drawing functions.
  2. Overview of AirGradient Arduino Library

    master

    The AirGradient Arduino Library provides the firmware for AirGradient open-source indoor and outdoor air quality monitors. It is designed for devices using ESP8266 (Wemos D1 MINI) or ESP32 (ESP32-C3 Mini) microcontrollers.

    Supported sensor modules include:

    • Plantower PMS5003
    • Plantower PMS5003T
    • SenseAir S8
    • Sensirion SGP41
    • Sensirion SHT40
  3. Supported sensor modules in the AirGradient library

    master

    The AirGradient Arduino library is designed for ESP8266 (Wemos D1 MINI) and ESP32 (ESP32-C3 Mini) microcontrollers and supports the following sensor modules:

    • Particulate Matter: Plantower PMS5003, Plantower PMS5003T
    • CO2: SenseAir S8
    • VOC/Gas: Sensirion SGP41
    • Temperature/Humidity: Sensirion SHT40
  4. Understand PubSubClient limitations

    master

    When using PubSubClient, be aware of the following protocol and performance constraints:

    • QoS Support: The client can only publish QoS 0 messages. It can subscribe to messages at either QoS 0 or QoS 1.
    • Packet Size: The default maximum packet size is 256 bytes. If your messages exceed this, you must increase the buffer size using setBufferSize() or the MQTT_MAX_PACKET_SIZE macro.
    • MQTT Version: It uses MQTT 3.1.1 by default.
  5. Configure PM2.5 and Temperature/Humidity corrections

    master

    The corrections object allows you to apply local correction algorithms to PM2.5 (pm02), Temperature (atmp), and Humidity (rhum) values. This affects the local server response, the physical display, and open metrics.

    PM 2.5 (pm02)

    Available correctionAlgorithm values:

    • "none": No correction (default).
    • "epa_2021": Uses EPA 2021 correction factors.
    • "slr_PMS5003_20240104", "slr_PMS5003_20231218", "slr_PMS5003_20231030": Specific SLR corrections for sensor batches. Requires slr object with intercept, scalingFactor, and useEpa2021.

    Temperature (atmp) & Humidity (rhum)

    Available correctionAlgorithm values:

    • "none": No correction (default).
    • "ag_pms5003t_2024": Standard AirGradient correction (for outdoor monitors).
    • "custom": Uses manually set intercept and scalingFactor in the slr object.

    Note: If configurationControl is set to local, corrections must be set manually.

    # Example: Apply SLR correction for PMS5003 batch 20231030
    curl --location -X PUT 'http://airgradient_84fce612eff4.local/config' \
    --header 'Content-Type: application/json' \
    --data '{"corrections":{"pm02":{"correctionAlgorithm":"slr_PMS5003_20231030","slr":{"intercept":0,"scalingFactor":0.02838,"useEpa2021":true}}}}'
    
    # Example: Apply custom temperature correction
    curl --location -X PUT 'http://airgradient_84fce612eff4.local/config' \
    --header 'Content-Type: application/json' \
    --data '{"corrections":{"atmp":{"correctionAlgorithm":"custom","slr":{"intercept":0.2,"scalingFactor":1.1}}}}'
  6. How WiFiManager works

    master

    WiFiManager provides a fallback web configuration portal for ESP8266 and ESP32 devices.

    The Lifecycle:

    1. Station Mode: On startup, the ESP attempts to connect to previously saved WiFi credentials.
    2. Access Point Mode: If connection fails (or no credentials exist), the ESP enters Access Point (AP) mode and starts a DNS and WebServer (default gateway: 192.168.4.1).
    3. Configuration: A user connects to the ESP's AP using a browser. Because it uses a Captive Portal, most devices will automatically trigger a 'Join to network' popup or redirect any URL to the configuration portal.
    4. Setup: The user selects an SSID, enters the password, and saves.
    5. Reconnection: The ESP attempts to connect to the new network. If successful, it returns control to your application. If it fails, the user must reconnect to the AP to reconfigure.
  7. How firmware updates are deployed

    master

    Releases published on GitHub are not immediately deployed to all devices. They undergo internal testing and limited deployments first. Once verified, firmware is made available via:

    1. FOTA (Firmware Over-The-Air) updates through the AirGradient dashboard.
    2. Manual flashing via the Airgradient firmware website.

    Check the GitHub release notes for the planned rollout date for wider availability.

  8. How the Sensirion Gas Index Algorithm works

    master

    The Sensirion Gas Index Algorithm converts raw sensor signals (SRAW_VOC and SRAW_NOX) from SGP40/41 sensors into robust VOC and NOx Index outputs.

    Key Concepts

    • Normalization: The algorithm uses statistical gain-offset normalization that adapts constantly using an exponentially decaying function to handle changing environments and minimize sensor-to-sensor variation.
    • Dual Instances: To get both indices, you must instantiate the software twice: once as a VOC Algorithm (using SRAW_VOC) and once as a NOx Algorithm (using SRAW_NOX). Note that the SGP40 sensor only provides SRAW_VOC.
    • Sampling Interval: Raw signals must be fed to the algorithm at a constant sampling interval that matches the sensor's readout interval. The default interval is 1 second. If you change the sensor sampling interval, you must also update the sampling interval definition in the algorithm's .h file.
    • Recursive Calculation: The algorithm calculates indices recursively using a single raw tick value per time step, maintaining internal states like estimated mean, variance, and uptime.
  9. Handle multi-product codebases with VHUB

    master

    If a single firmware codebase supports multiple products (e.g., sharing ~80% of tests), do not attempt to use one large tests.json. Instead, use a build script to emit one distinct tests.json per product into a distribution folder.

    Recommended Layout:

    verification/
    ├── shared/            # Shared scenarios (not consumed by VHUB)
    ├── build_templates.py # Script that filters and emits per-product JSON
    └── dist/
        ├── product-a.tests.json
        └── product-b.tests.json

    VHUB treats each imported file as an independent product database. The logic for filtering tests based on product variants is the responsibility of your build system.

  10. How OTA updates work on AirGradient monitors

    master

    AirGradient ONE and Open Air monitors (firmware version 3.1.1+) support Over-the-Air (OTA) updates. The device automatically checks for updates on startup and at regular intervals.

    Update Mechanism

    1. Version Identification: During compilation of an official release, the git tag (GIT_VERSION) is embedded into the binary.
    2. Update Request: The device sends a GET request to the following URL, passing its current version as a query parameter: http://hw.airgradient.com/sensors/{deviceId}/generic/os/firmware.bin?current_firmware={GIT_VERSION}
    3. Server Response Scenarios:
      • 200 OK: An update is available. The server returns the binary data, and the device performs the update.
      • 304 Not Modified: The device is already running the latest firmware.
      • 400 Bad Request: The firmware version is unknown to the server. This typically happens when running local development builds where GIT_VERSION defaults to snapshot. In this case, no update is performed.
    http://hw.airgradient.com/sensors/{deviceId}/generic/os/firmware.bin?current_firmware={GIT_VERSION}
  11. Discover AirGradient monitors via mDNS

    master

    AirGradient ONE and Open Air monitors (firmware 3.0.10+) run mDNS discovery. You can access a monitor on your local network using the following URL format:

    http://airgradient_{{serialnumber}}.local

    Replace {{serialnumber}} with the actual serial number of your device.