ESP32 and CCS811 gas sensor data to Thingspeak example

In this example we connect a CCS811 gas sensor to an ESP32 and then we will upload the data to Thingspeak

The CCS811 is a low-power digital gas sensor solution, which integrates a gas sensor solution for detecting low levels of VOCs typically found indoors, with a microcontroller unit (MCU) and an Analog-to-Digital converter to monitor the local environment and provide an indication of the indoor air quality via an equivalent CO2 or TVOC output over a standard I2C digital interface.

 

 

Thingspeak setup

You will now need to create a new account at thingspeak – https://thingspeak.com. Once done create a new channel and add one new field called temperature. You can see this in a screen capture of my simple channel, notice the ChannelID you will need that in your code later.

You can also fill in other fields such as Name, description and there are a few others as well. The key one(s) are Field1, Field 2  and Field 3 – this effectively is the data you send to thingspeak

ESP32 and CCS811 Thingspeak channel setup
ESP32 and CCS811 Thingspeak channel setup

Parts List

 

Part Link
CCS811 CJMCU-811 CCS811 Air Quality Gas Sensor
ESP32 New ESP-32 esp32 Lite V1.0.0 For Rev1 wifi Module + bluetooth board 4MB FLASH
Connecting Cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

 

Schematics/Layout

Remember and connect WAKE to gnd

Layout

esp32 and CCS811 layout
esp32 and CCS811 layout

Schematic

esp32 and CCS811 schematic
esp32 and CCS811 schematic

 

Code

Again we use a library this is the adafruit CCS811 one – you can use the library manager and add this you will also the Thingspeak libraries for this example

https://github.com/mathworks/thingspeak-arduino

I got the sea level pressure value from this link


#include "ThingSpeak.h"
#include <WiFi.h>
#include "Adafruit_CCS811.h"

char ssid[] = "networkssid"; // your network SSID (name)
char pass[] = "networkname"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;

unsigned long myChannelNumber = 00000;
const char * myWriteAPIKey = "apikey";

Adafruit_CCS811 ccs;

void setup()
{
Serial.begin(115200); //Initialize serial
if(!ccs.begin())
{
Serial.println("Failed to start sensor! Please check your wiring.");
while(1);
}

//calibrate temperature sensor
while(!ccs.available());
float temp = ccs.calculateTemperature();
ccs.setTempOffset(temp - 25.0);
delay(10);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak

}

void loop()
{
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
//Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}

if(ccs.available())
{
if(!ccs.readData())
{
float ccsCO2 = ccs.geteCO2();
float ccsTVOC = ccs.getTVOC();
float ccsTemp = ccs.calculateTemperature();
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel.
ThingSpeak.setField(1, ccsCO2);
ThingSpeak.setField(2, ccsTVOC);
ThingSpeak.setField(3, ccsTemp);

// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
else
{
Serial.println("ERROR!");
while(1);
}
}

delay(20000); // Wait 20 seconds to update the channel again
}

Output

Open the serial monitor and verify you are connecting and the data has been successfully

Attempting to connect to SSID: .
Connected.
Channel update successful.
Channel update successful.
Channel update successful.
Channel update successful.
Channel update successful.

Lets look at our Thingspeak channel, all going well you should see data like the following

CCS811 and Thingspeak output
CCS811 and Thingspeak output

Links