I want to connect two CCS811 sensors with ESP32 dev board. Since the sensors have fixed address, I am unable to use them simultaneously. I have read that on ESP32 multiple I2C buses can be created. While trying to follow that route I tried to modify the library but I am stuck. I can't simply pass different pins for SDA and SCL when calling ccs.begin() function. It only takes one argument and that is the sensor address (code chunk from library below):
bool Adafruit_CCS811::begin(uint8_t addr) {
_i2caddr = addr;
_i2c_init();
SWReset();
delay(100);
// check that the HW id is correct
if (this->read8(CCS811_HW_ID) != CCS811_HW_ID_CODE)
return false;
// try to start the app
this->write(CCS811_BOOTLOADER_APP_START, NULL, 0);
delay(100);
and the actual wire.begin() function is called in the _i2c_init() function which is defined in the lib as:
void Adafruit_CCS811::_i2c_init() {
Wire.begin();
#ifdef ESP8266
Wire.setClockStretchLimit(500);
#endif
}
With my very basic knowledge of classes and object, I am unable to modify the library to make it work with 2 I2C buses. Could you please help me with this?
Link to Adafruit CCS811 library:(https://github.com/adafruit/Adafruit_CCS811/blob/master/Adafruit_CCS811.cpp)
My Code so far:
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_CCS811.h"
Adafruit_CCS811 ccs1;
Adafruit_CCS811 ccs2;
TwoWire sen1 = TwoWire(0);
TwoWire sen2 = TwoWire(1);
void setup()
{
Serial.begin(115200);
Serial.println(F("CCS811 test"));
if (!ccs1.begin(25, 26, 100000))
{
Serial.println("CCS 1 Failed.");
//while (1);
} else {
Serial.println("CCS 1 OK!");
}
// Wait for the sensor to be ready
//while (!ccs.available());
}
void loop()
{
if (ccs1.available())
{
if (!ccs1.readData())
{
Serial.println(" This is CSS");
Serial.print("CO2: ");
Serial.print(ccs1.geteCO2());
Serial.print("ppm, TVOC: ");
Serial.println(ccs1.getTVOC());
}
else
{
Serial.println("ERROR!");
//while (1);
}
}
delay(500);
}