AirGradient Arduino Library
repository·master·Indexed 18 days ago
https://github.com/airgradienthq/arduinoFirmware 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.
What's inside airgradienthq-arduino
- 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.
Overview of Adafruit Bus IO Library
masterThe Adafruit Bus IO Library is a helper library designed to abstract away I2C and SPI transactions and registers. It provides a unified interface for managing communication with hardware devices over these common protocols, simplifying the development of drivers for various sensors and peripherals.Overview of AirGradient Arduino Library
masterThe 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
Supported sensor modules in the AirGradient library
masterThe 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
Understand PubSubClient limitations
masterWhen 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 theMQTT_MAX_PACKET_SIZEmacro. - MQTT Version: It uses MQTT 3.1.1 by default.
Configure PM2.5 and Temperature/Humidity corrections
masterThe
correctionsobject 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
correctionAlgorithmvalues:"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. Requiresslrobject withintercept,scalingFactor, anduseEpa2021.
Temperature (
atmp) & Humidity (rhum)Available
correctionAlgorithmvalues:"none": No correction (default)."ag_pms5003t_2024": Standard AirGradient correction (for outdoor monitors)."custom": Uses manually setinterceptandscalingFactorin theslrobject.
Note: If
configurationControlis set tolocal, 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}}}}'How WiFiManager works
masterWiFiManager provides a fallback web configuration portal for ESP8266 and ESP32 devices.
The Lifecycle:
- Station Mode: On startup, the ESP attempts to connect to previously saved WiFi credentials.
- 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). - 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.
- Setup: The user selects an SSID, enters the password, and saves.
- 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.
How firmware updates are deployed
masterReleases 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:
- FOTA (Firmware Over-The-Air) updates through the AirGradient dashboard.
- Manual flashing via the Airgradient firmware website.
Check the GitHub release notes for the planned rollout date for wider availability.
How the Sensirion Gas Index Algorithm works
masterThe Sensirion Gas Index Algorithm converts raw sensor signals (
SRAW_VOCandSRAW_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 (usingSRAW_NOX). Note that the SGP40 sensor only providesSRAW_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
.hfile. - 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.
Handle multi-product codebases with VHUB
masterIf 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 distincttests.jsonper 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.jsonVHUB 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.
How OTA updates work on AirGradient monitors
masterAirGradient 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
- Version Identification: During compilation of an official release, the git tag (
GIT_VERSION) is embedded into the binary. - 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} - 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_VERSIONdefaults tosnapshot. In this case, no update is performed.
http://hw.airgradient.com/sensors/{deviceId}/generic/os/firmware.bin?current_firmware={GIT_VERSION}- Version Identification: During compilation of an official release, the git tag (
Discover AirGradient monitors via mDNS
masterAirGradient 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}}.localReplace
{{serialnumber}}with the actual serial number of your device.