After quite some testing different improvement options, I now have stable values and no drop-outs anymore. I think the problem occured due to timing issues reading serial data. I have simplified the code and this seems to work fine.
This is the code I am currently using in the function “getCO2_Raw()” in the library airgradient.cpp
int AirGradient::getCO2_Raw(){
const byte CO2Command[] = {0xFE, 0X44, 0X00, 0X08, 0X02, 0X9F, 0X25};
byte CO2Response[] = {0,0,0,0,0,0,0};
_SoftSerial_CO2->write(CO2Command, 7);
delay(100); //give the sensor a bit of time to respond
if (_SoftSerial_CO2->available()){
for (int i=0; i < 7; i++) {
int byte = _SoftSerial_CO2->read();
CO2Response[i] = byte;
if (CO2Response[0] != 254) {
return -1; //error code for debugging
}
}
unsigned long val = CO2Response[3]*256 + CO2Response[4];
return val;
}
else
{
return -2; //error code for debugging
}
}
In my main sketch I loop this function in case I receive an exit code -1 or -2 but in the last 24hrs this did not happen once.
I hope that someone else can have an advantage of this change.