CO2 error handling in library

Small improvements in examples
This commit is contained in:
Achim
2022-10-29 07:39:11 +07:00
parent d5c8af68a0
commit 90eee5d17f
10 changed files with 108 additions and 50 deletions

View File

@ -105,7 +105,7 @@ int AirGradient::getPM2_Raw(){
pm02 = data.PM_AE_UG_2_5;
return pm02;
} else {
return 0;
return -1;
}
}
@ -589,45 +589,109 @@ void AirGradient::CO2_Init(int rx_pin,int tx_pin,int baudRate){
delay(10000);
}
}
const char* AirGradient::getCO2(int retryLimit) {
int ctr = 0;
int result_CO2 = getCO2_Raw();
while(result_CO2 == -1){
result_CO2 = getCO2_Raw();
if((ctr == retryLimit) || (result_CO2 == -1)){
Char_CO2[0] = 'N';
Char_CO2[1] = 'U';
Char_CO2[2] = 'L';
Char_CO2[3] = 'L';
return Char_CO2;
//const char* AirGradient::getCO2(int retryLimit) {
// int ctr = 0;
// int result_CO2 = getCO2_Raw();
// while(result_CO2 == -1){
// result_CO2 = getCO2_Raw();
// if((ctr == retryLimit) || (result_CO2 == -1)){
// Char_CO2[0] = 'N';
// Char_CO2[1] = 'U';
// Char_CO2[2] = 'L';
// Char_CO2[3] = 'L';
// return Char_CO2;
// }
// ctr++;
// }
// sprintf(Char_CO2,"%d", result_CO2);
// return Char_CO2;
//}
//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
// }
//}
int AirGradient::getCO2(int numberOfSamplesToTake) {
int successfulSamplesCounter = 0;
int co2AsPpmSum = 0;
for (int sample = 0; sample < numberOfSamplesToTake; sample++) {
int co2AsPpm = getCO2_Raw();
if (co2AsPpm > 300 && co2AsPpm < 10000) {
Serial.println("CO2 read success " + String(co2AsPpm));
successfulSamplesCounter++;
co2AsPpmSum += co2AsPpm;
} else {
Serial.println("CO2 read failed with " + String(co2AsPpm));
}
ctr++;
// without delay we get a few 10ms spacing, add some more
delay(250);
}
sprintf(Char_CO2,"%d", result_CO2);
return Char_CO2;
if (successfulSamplesCounter <= 0) {
// total failure
return -5;
}
Serial.println("# of CO2 reads that worked: " + String(successfulSamplesCounter));
Serial.println("CO2 reads sum " + String(co2AsPpmSum));
return co2AsPpmSum / successfulSamplesCounter;
}
int AirGradient::getCO2_Raw(){
// <<>>
int AirGradient::getCO2_Raw() {
while(_SoftSerial_CO2->available()) // flush whatever we might have
_SoftSerial_CO2->read();
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
const int commandSize = 7;
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
int numberOfBytesWritten = _SoftSerial_CO2->write(CO2Command, commandSize);
if (numberOfBytesWritten != commandSize) {
// failed to write request
return -2;
}
// attempt to read response
int timeoutCounter = 0;
while (_SoftSerial_CO2->available() < commandSize) {
timeoutCounter++;
if (timeoutCounter > 10) {
// timeout when reading response
return -3;
}
}
unsigned long val = CO2Response[3]*256 + CO2Response[4];
return val;
delay(50);
}
else
{
return -2; //error code for debugging
// we have 7 bytes ready to be read
for (int i=0; i < commandSize; i++) {
CO2Response[i] = _SoftSerial_CO2->read();
}
return CO2Response[3]*256 + CO2Response[4];
}
//END CO2 FUNCTIONS //