How to Create an Indoor Temperature, Humidity, and Air Quality Monitoring System with ESP32-C6

Poor indoor air quality can lead to health problems. An indoor environment monitoring system based on the FireBeetle 2 ESP32-C6 IoT Development Board and other open-source hardware platforms is not only easy to configure and assemble but also more cost-effective than many commercial air monitoring systems. This tutorial will guide you through building an indoor environment monitoring system using the ESP32 development board, SHT31 temperature and humidity sensor, ENS160 air quality sensor, and a lithium battery. You'll be able to monitor and record indoor temperature, humidity, air quality index (AQI), volatile organic compounds (VOCs), and eCO2 in real-time.


An indoor environment monitoring system
1 FireBeetle ESP32-C6
1 Fermion: SHT31 Digital Temperature & Humidity Sensor
1 Fermion: ENS160 Air Quality Sensor
1 Lithium Battery

Hardware Connections


 Wiring Diagrams of FireBeetle ESP32 C6, Temperature & Humidity Sensor, and air quality sensor
Wiring Diagrams of FireBeetle ESP32 C6, Temperature & Humidity Sensor, and air quality sensor

Data Collection and Processing

The ESP32 microcontroller serves as the core processing unit, responsible for collecting sensor data, processing and analyzing it, and transmitting the data wirelessly to a server. The software logic includes:

 1. Battery Voltage Collection:

    - Use the ESP32 IO0 pin to collect battery voltage.

    - Convert the collected analog voltage signal to a digital signal via ADC for further processing.

 2. Temperature, Humidity, and Air Quality Data Collection:

    - Use the ESP32's I2C to read data from the sensors.

 3. Data Reporting:

    - Connect to a wireless network using the ESP32 WiFi module.

    - Report the collected data to a server via HTTP for remote monitoring and management.


Sample code

#include <WiFi.h>
#include <HTTPClient.h>
#include <DFRobot_SHT3x.h>
#include <DFRobot_ENS160.h>

DFRobot_SHT3x   sht3x;
DFRobot_ENS160_I2C ENS160(&Wire, /*iicAddr*/ 0x53);

// #define DATA_SEND_INTERVAL_MS 15 * 60 * 1000
#define DATA_SEND_INTERVAL_MS 60 * 1000

int lastDataSentTimestamp = 0;
unsigned long previousMillis = 0;
unsigned long interval = 30000;

// WiFi network information
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// Server URL
const char* serverName = "";
const char* serverToken = "your_influx_token";

// Define ADC pin
const int batteryPin = 0; // IO0

void sendData() {
  // Collect battery voltage
  int batteryVoltage = analogReadMilliVolts(batteryPin)*2;
  Serial.print("BAT millivolts value = ");
  // Collect temperature and humidity
  float temperature = sht3x.getHumidityRH();
  float humidity = sht3x.getTemperatureC();

  // Collect air quality
  ENS160.setTempAndHum(temperature, humidity);
  uint8_t AQI = ENS160.getAQI();
  Serial.print("AIQ : ");
  uint16_t TVOC = ENS160.getTVOC();
  Serial.print("TVOC(ppb): ");
  uint16_t ECO2 = ENS160.getECO2();
  Serial.print("ECO2(ppm): ");
  // Create HTTP client
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.addHeader("Content-Type", "text/plain");
    http.addHeader("authorization", serverToken);
    // Create POST data
    String httpRequestData = "batteryVoltage=" + String(batteryVoltage) + "&Temperature=" + String(temperature) + "&Humidity=" + String(humidity) + "&AIQ=" + String(AQI) + "&TVOC=" + String(TVOC) + "&ECO2=" + String(ECO2);
    // Send HTTP POST request
    int httpResponseCode = http.POST(httpRequestData);
    // Print response result
    if (httpResponseCode > 0) {
      String response = http.getString();
    } else {
      Serial.print("Error on sending POST: ");
    // End HTTP request

void setup() {
  // Initialize SHT31
  while (sht3x.begin() != 0) {
    Serial.println("SHT31 error");
  // Initialize ENS160
  while( NO_ERR != ENS160.begin() ){
    Serial.println("ENS160 error");
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi...");
  Serial.println("Connected to WiFi");
  Serial.print("IP address: ");

void loop() {
  unsigned long currentMillis = millis();
  // if WiFi is down, try reconnecting
  if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >= interval)) {
    Serial.println("Reconnecting to WiFi...");
    previousMillis = currentMillis;
  if (millis() > lastDataSentTimestamp + DATA_SEND_INTERVAL_MS) {

    Serial.print("Millis since last measurement: ");
    Serial.println(millis() - lastDataSentTimestamp);
    lastDataSentTimestamp = millis();


Server Setup

 1. Install Python 3.

 2. Install Flask:

    - Press Win+R, type cmd, and open the command prompt.

    - Enter pip install flask to install Flask.

 3. Run the Python code in the command line.

from flask import Flask, request, jsonify

app = Flask(__name__)

# Endpoint to handle data from Arduino
@app.route('/update', methods=['POST'])
def update():
    auth_header = request.headers.get('authorization')
    if (auth_header == 'your_influx_token'):
        data = request.data.decode('utf-8')
        print(f"Received data: {data}")
        # Process the data sent from Arduino here, such as storing it in a database
        return jsonify({"status": "success", "message": "Data received"}), 200
        return jsonify({"status": "error", "message": "Unauthorized"}), 401

def home():
    return "Hello from Flask server!", 200

if __name__ == '__main__':
    app.run(host='', port=5000)


Now you can check the data:


Learn More About the Data:

For more information on interpreting air quality data, refer to: ENS160 Air Quality Sensor Wiki

Project Expansion

The onboard GDI interface allows for easy connection to screens, ensuring efficient data visualization. Detailed screen lists and configuration tutorials are provided to facilitate seamless integration of ESP32-C6 functionalities into any setup.

DFRobot's all Gravity and Fermion series sensors are compatible with the FireBeetle 2 ESP32-C6, offering a comprehensive selection for various projects.


This guide details the design and implementation of an indoor environment monitoring system based on the ESP32 development board, SHT31 temperature and humidity sensor, ENS160 air quality sensor, and a lithium battery. By integrating these advanced hardware devices and IoT technology, we successfully achieved real-time monitoring and data analysis of key indoor air quality parameters.

All Rights