Home monitoring with ESP32. Part III CO2 sensor

Jiashun Zheng
2 min readNov 8, 2021

--

I am curious about the indoor air quality, especially in the bedroom. When the ventilation flow is small, the CO2 level will increase quite fast.

Here is what I got from Aliexpress:

1. CJMCU-811 CCS811 CO2 Sensor.

2. ESP32 develop board

3. A temperature sensor: HTU21D

Wiring

It turns out the wiring is quite simple, as the sensor board is using I2C interface, so you just need to connect the SCL to D22, SDA to D21, WAK to GND, VCC to 3.3V, and GND to GND.

The HTU21D wiring is also similar, SCL to D22, SDA to D21 following the ESP32 I2C document.

Code

Reference: https://how2electronics.com/monitor-ccs811-co2-tvoc-on-esp8266-esp32-webserver/

// Read
uint16_t eco2, etvoc, errstat, raw;
ccs811.read(&eco2, &etvoc, &errstat, &raw);

// Print measurement results based on status
if ( errstat == CCS811_ERRSTAT_OK )
{
val1 = eco2;
val2 = etvoc;

Serial.print(“CCS811: “);
Serial.print(“eco2=”);
Serial.print(val1);
Serial.print(“ ppm “);

Serial.print(“etvoc=”);
Serial.print(val2);
Serial.print(“ ppb “);
Serial.println();
}

Transferring data to a MQTT server (to save it to a database)

//before setup

WiFiClient espClient;
const char* mqtt_server = “192.168.1.205”;
PubSubClient client(espClient);
long lastMsg = 0;
char message[200];

//In the loop

sprintf(message, “CO2:%.0f;TVOC:%.0f;T:%.2f;H:%.2f”, val1, val2, temp, rel_hum);

Serial.println();
Serial.print(“Message: “);
Serial.print(message);
if (!client.connected()) {
reconnect();
}
client.loop();

client.publish(“esp32/HOME”, message);

The full source code is available here:

Output from the MQTT receiver:

CO2 measurement in PPM

When fresh air is present (open the window), and put the sensor near the window, the CO2 level will drop to 400ppm quickly, then you can test it out by blowing towards the sensor and you should see a jump of both CO2 and TVOC level.

Save data to influxdb and display with Grafana:

Limitation of CCS811 sensor:

CCS811 is not actually measuring the real CO2 level, instead, it is using the level of some Volatile Organic Compounds (VOC) as a proxy for CO2 assuming there are human being present and some VOCs are released by breath.

--

--

No responses yet