# ESP32 SEN66 Air Quality Monitor

[![ESP-IDF v6.0.1](https://img.shields.io/badge/ESP--IDF-v6.0.1-E7352C?logo=espressif&logoColor=white)](https://docs.espressif.com/projects/esp-idf/en/v6.0.1/esp32c6/)
[![Target ESP32-C6](https://img.shields.io/badge/target-ESP32--C6-000000)](https://docs.espressif.com/projects/esp-dev-kits/en/latest/esp32c6/esp32-c6-devkitc-1/user_guide.html)
[![Sensor SEN66](https://img.shields.io/badge/sensor-Sensirion%20SEN66-009A93)](https://sensirion.com/products/catalog/SEN66)
[![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

Native ESP-IDF firmware for an ESP32-C6-DevKitC-1 and Sensirion SEN66 indoor
air quality sensor. It provides a lightweight web interface, JSON readings,
Prometheus metrics, persistent sensor configuration, and HTTP OTA updates.

<p align="center">
  <img src="assets/web-interface.png" width="760" alt="Air quality readings web interface">
</p>

## Features

- PM1.0, PM2.5, PM4.0, PM10, temperature, humidity, VOC, NOx, and CO2 readings
- Live browser UI, JSON API, and Prometheus metrics
- SEN66 health flags and ESP32/WiFi diagnostics
- Persistent altitude, temperature offset, and CO2 ASC configuration
- Forced CO2 recalibration and SEN66 fan cleaning controls
- Authenticated configuration and OTA firmware updates
- SNTP time synchronization and onboard RGB status indication
- Dual 3 MB OTA application partitions with rollback support

## Hardware

| Part | Reference |
| --- | --- |
| Development board | [ESP32-C6-DevKitC-1](https://docs.espressif.com/projects/esp-dev-kits/en/latest/esp32c6/esp32-c6-devkitc-1/user_guide.html), 8 MB flash |
| Air quality sensor | [Sensirion SEN66](https://sensirion.com/products/catalog/SEN66) |
| Connection | Data-capable USB-C cable and four sensor wires |

Default wiring:

| ESP32-C6 | SEN66 |
| --- | --- |
| GND | GND |
| 3V3 | VCC |
| GPIO 6 | SDA |
| GPIO 7 | SCL |

The SEN66 supply range is 3.15-3.6 V. Check the official product documentation
before changing the power source or wiring.

## Quick Start

The Makefile installs the pinned ESP-IDF checkout and tools under the ignored
`.esp-idf/` directory. On Linux or macOS, with Git, GNU Make, and the ESP-IDF
host prerequisites installed:

```sh
git clone --recurse-submodules https://github.com/cdump/esp32-sen66-airq.git
cd esp32-sen66-airq
make setup
make menuconfig
make build
make flash
make monitor
```

In `make menuconfig`, open the `airq` section and configure:

- WiFi SSID and password
- HTTP auth username and a non-empty password
- Optional device name, SNTP server, and sensor defaults

Use an explicit serial port if auto-detection selects the wrong device:

```sh
make flash PORT=/dev/ttyACM0
make monitor PORT=/dev/ttyACM0
```

On macOS the port is usually similar to `/dev/cu.usbmodem*`.

## Web Interface

The HTTP server starts after WiFi connects, even if the router was unavailable
at boot. After a disconnect, the firmware keeps reconnecting automatically:
the configured number of fast retries are spaced one second apart, followed
by one attempt every 30 seconds until WiFi returns.

| Path | Access | Description |
| --- | --- | --- |
| `/` | Public | Current readings and device health |
| `/readings.json` | Public | Readings, validity flags, and sensor status as JSON |
| `/metrics` | Public | Prometheus metrics |
| `/config` | Authenticated | Persistent sensor configuration and fan cleaning |
| `/calibrate` | Authenticated | Forced SEN66 CO2 recalibration |
| `/ota` | Authenticated | Firmware upload page |

Configuration is stored in ESP32 NVS and reapplied to the SEN66 at boot.
Runtime changes restart continuous measurement when required by the sensor
command sequence. Fan cleaning pauses measurement for the documented 10-second
cleaning cycle.

## OTA Updates

Once OTA-capable firmware is running, upload a new build with:

```sh
make ota OTA_HOST=192.0.2.10
```

Set `OTA_HOST` to the device IP address or a hostname supplied by your network's
DNS. The firmware does not currently advertise an mDNS `.local` hostname.

The target builds `build/airq.bin` and posts it to
`http://<OTA_HOST>/ota`. Credentials default to
`CONFIG_AIRQ_AUTH_USERNAME` and `CONFIG_AIRQ_AUTH_PASSWORD` from the local
`sdkconfig`. They can be overridden with `OTA_USER` and `OTA_PASSWORD`; the URL
and image can be adjusted with `OTA_SCHEME` and `OTA_FIRMWARE`.

## Security

Read-only endpoints are public. `/config`, `/calibrate`, and `/ota` require
HTTP Basic Auth when `CONFIG_AIRQ_AUTH_ENABLED` is enabled.

> Basic Auth does not encrypt traffic. Use these controls only on a trusted
> local network or behind transport protection.

WiFi and HTTP credentials are stored in the generated local `sdkconfig`, which
is ignored by Git. Do not force-add that file or place credentials in
`sdkconfig.defaults`, source files, or documentation.

## Dependencies

- [ESP-IDF v6.0.1](https://github.com/espressif/esp-idf/tree/v6.0.1), installed locally by `make setup`
- [Sensirion embedded-i2c-sen66](https://github.com/Sensirion/embedded-i2c-sen66), pinned as a Git submodule
- [Espressif led_strip](https://components.espressif.com/components/espressif/led_strip), managed by ESP-IDF

The SEN66 driver remains pinned to version 1.2.0. Version 1.3.0 changes the
integer measurement command from `0x0300` to `0x0414`; the currently deployed
SEN66 firmware does not return measurements for that command.

## License

This project's original code is licensed under the [MIT License](LICENSE).
Third-party components remain subject to their respective licenses, including
the BSD-3-Clause SEN66 driver and Apache-2.0 ESP-IDF components.
