Merge pull request #5817 from gemu2015/sgp30_compensation

Sgp30 compensation
This commit is contained in:
Theo Arends 2019-05-18 15:50:11 +02:00 committed by GitHub
commit c54bcf4b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 148 additions and 37 deletions

View File

View File

@ -60,7 +60,8 @@ boolean Adafruit_SGP30::begin(TwoWire *theWire) {
_i2c = theWire; _i2c = theWire;
} }
_i2c->begin(); // assume i2c initialized already to avoid resetting clock stretching
// _i2c->begin();
uint8_t command[2]; uint8_t command[2];
@ -156,6 +157,29 @@ boolean Adafruit_SGP30::setIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base) {
return readWordFromCommand(command, 8, 10); return readWordFromCommand(command, 8, 10);
} }
/**************************************************************************/
/*!
@brief Set the absolute humidity value [mg/m^3] for compensation to increase precision of TVOC and eCO2.
@param absolute_humidity A uint32_t [mg/m^3] which we will be used for compensation. If the absolute humidity is set to zero, humidity compensation will be disabled.
@returns True if command completed successfully, false if something went wrong!
*/
/**************************************************************************/
boolean Adafruit_SGP30::setHumidity(uint32_t absolute_humidity) {
if (absolute_humidity > 256000) {
return false;
}
uint16_t ah_scaled = (uint16_t)(((uint64_t)absolute_humidity * 256 * 16777) >> 24);
uint8_t command[5];
command[0] = 0x20;
command[1] = 0x61;
command[2] = ah_scaled >> 8;
command[3] = ah_scaled & 0xFF;
command[4] = generateCRC(command+2, 2);
return readWordFromCommand(command, 5, 10);
}
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief I2C low level interfacing @brief I2C low level interfacing

View File

@ -42,6 +42,7 @@ class Adafruit_SGP30 {
boolean getIAQBaseline(uint16_t *eco2_base, uint16_t *tvoc_base); boolean getIAQBaseline(uint16_t *eco2_base, uint16_t *tvoc_base);
boolean setIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base); boolean setIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base);
boolean setHumidity(uint32_t absolute_humidity);
/** /**
* The last measurement of the IAQ-calculated Total Volatile Organic Compounds in ppb. This value is set when you call {@link IAQmeasure()} * The last measurement of the IAQ-calculated Total Volatile Organic Compounds in ppb. This value is set when you call {@link IAQmeasure()}

View File

@ -3,6 +3,17 @@
Adafruit_SGP30 sgp; Adafruit_SGP30 sgp;
/* return absolute humidity [mg/m^3] with approximation formula
* @param temperature [°C]
* @param humidity [%RH]
*/
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
// approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
return absoluteHumidityScaled;
}
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
Serial.println("SGP30 test"); Serial.println("SGP30 test");
@ -22,6 +33,11 @@ void setup() {
int counter = 0; int counter = 0;
void loop() { void loop() {
// If you have a temperature / humidity sensor, you can set the absolute humidity to enable the humditiy compensation for the air quality signals
//float temperature = 22.1; // [°C]
//float humidity = 45.2; // [%RH]
//sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));
if (! sgp.IAQmeasure()) { if (! sgp.IAQmeasure()) {
Serial.println("Measurement failed"); Serial.println("Measurement failed");
return; return;

View File

@ -1,5 +1,5 @@
name=Adafruit SGP30 Sensor name=Adafruit SGP30 Sensor
version=1.0.2 version=1.0.3
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=This is an Arduino library for the Adafruit SGP30 Gas / Air Quality Sensor sentence=This is an Arduino library for the Adafruit SGP30 Gas / Air Quality Sensor

View File

@ -34,55 +34,122 @@ Adafruit_SGP30 sgp;
uint8_t sgp30_type = 0; uint8_t sgp30_type = 0;
uint8_t sgp30_ready = 0; uint8_t sgp30_ready = 0;
uint8_t sgp30_counter = 0; float sgp30_abshum;
/********************************************************************************************/ /********************************************************************************************/
void sgp30_Init(void) {
if (sgp.begin()) {
sgp30_type = 1;
// snprintf_P(log_data, sizeof(log_data), PSTR("SGP: Serialnumber 0x%04X-0x%04X-0x%04X"), sgp.serialnumber[0], sgp.serialnumber[1], sgp.serialnumber[2]);
// AddLog(LOG_LEVEL_DEBUG);
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "SGP30", 0x58);
AddLog(LOG_LEVEL_DEBUG);
}
}
//#define POW_FUNC pow
#define POW_FUNC FastPrecisePow
float sgp30_AbsoluteHumidity(float temperature, float humidity,char tempUnit) {
//taken from https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/
//precision is about 0.1°C in range -30 to 35°C
//August-Roche-Magnus 6.1094 exp(17.625 x T)/(T + 243.04)
//Buck (1981) 6.1121 exp(17.502 x T)/(T + 240.97)
//reference https://www.eas.ualberta.ca/jdwilson/EAS372_13/Vomel_CIRES_satvpformulae.html
float temp = NAN;
const float mw = 18.01534; // molar mass of water g/mol
const float r = 8.31447215; // Universal gas constant J/mol/K
if (isnan(temperature) || isnan(humidity) ) {
return NAN;
}
if (tempUnit != 'C') {
temperature = (temperature - 32.0) * (5.0 / 9.0); /*conversion to [°C]*/
}
temp = POW_FUNC(2.718281828, (17.67 * temperature) / (temperature + 243.5));
//return (6.112 * temp * humidity * 2.1674) / (273.15 + temperature); //simplified version
return (6.112 * temp * humidity * mw) / ((273.15 + temperature) * r); //long version
}
#define SAVE_PERIOD 30
void Sgp30Update(void) // Perform every second to ensure proper operation of the baseline compensation algorithm void Sgp30Update(void) // Perform every second to ensure proper operation of the baseline compensation algorithm
{ {
sgp30_ready = 0; sgp30_ready = 0;
if (!sgp30_type) { if (!sgp.IAQmeasure() || !sgp30_type) {
if (sgp.begin()) { // retry to init every 100 seconds
sgp30_type = 1; if (21 == (uptime %100)) {
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SGP: Serialnumber 0x%04X-0x%04X-0x%04X"), sgp.serialnumber[0], sgp.serialnumber[1], sgp.serialnumber[2]); sgp30_Init();
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "SGP30", 0x58);
} }
} else { return; // Measurement failed
if (!sgp.IAQmeasure()) return; // Measurement failed }
sgp30_counter++; if (global_update) {
if (30 == sgp30_counter) { // abs hum in mg/m3
sgp30_counter = 0; sgp30_abshum=sgp30_AbsoluteHumidity(global_temperature,global_humidity,TempUnit());
sgp.setHumidity(sgp30_abshum*1000);
}
sgp30_ready = 1;
// these should normally be stored permanently and used for fast restart
if (!(uptime%SAVE_PERIOD)) {
// store settings every N seconds
uint16_t TVOC_base; uint16_t TVOC_base;
uint16_t eCO2_base; uint16_t eCO2_base;
if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) return; // Failed to get baseline readings if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) return; // Failed to get baseline readings
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base); // snprintf_P(log_data, sizeof(log_data), PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base);
} // AddLog(LOG_LEVEL_DEBUG);
sgp30_ready = 1;
} }
} }
#ifdef USE_WEBSERVER
const char HTTP_SNS_SGP30[] PROGMEM = const char HTTP_SNS_SGP30[] PROGMEM =
"{s}SGP30 " D_ECO2 "{m}%d " D_UNIT_PARTS_PER_MILLION "{e}" // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr> "{s}SGP30 " D_ECO2 "{m}%d " D_UNIT_PARTS_PER_MILLION "{e}" // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
"{s}SGP30 " D_TVOC "{m}%d " D_UNIT_PARTS_PER_BILLION "{e}"; "{s}SGP30 " D_TVOC "{m}%d " D_UNIT_PARTS_PER_BILLION "{e}";
const char HTTP_SNS_AHUM[] PROGMEM = "{s}SGP30 " "Abs Humidity" "{m}%s g/m3{e}";
#endif
#define D_JSON_AHUM "aHumidity"
void Sgp30Show(bool json) void Sgp30Show(bool json)
{ {
if (sgp30_ready) { if (sgp30_ready) {
char abs_hum[33];
if (global_update) {
// has humidity + temperature
dtostrfd(sgp30_abshum,4,abs_hum);
}
if (json) { if (json) {
ResponseAppend_P(PSTR(",\"SGP30\":{\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TVOC "\":%d}"), sgp.eCO2, sgp.TVOC); ResponseAppend_P(PSTR(",\"SGP30\":{\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TVOC "\":%d"), sgp.eCO2, sgp.TVOC);
if (global_update) {
ResponseAppend_P(PSTR(",\"" D_JSON_AHUM "\":%s"),abs_hum);
}
ResponseAppend_P(PSTR("}"));
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if (0 == tele_period) DomoticzSensor(DZ_AIRQUALITY, sgp.eCO2); if (0 == tele_period) DomoticzSensor(DZ_AIRQUALITY, sgp.eCO2);
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } else {
WSContentSend_PD(HTTP_SNS_SGP30, sgp.eCO2, sgp.TVOC); WSContentSend_PD(HTTP_SNS_SGP30, sgp.eCO2, sgp.TVOC);
if (global_update) {
WSContentSend_PD(HTTP_SNS_AHUM, abs_hum);
}
#endif #endif
} }
} }
} }
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
@ -93,6 +160,9 @@ bool Xsns21(uint8_t function)
if (i2c_flg) { if (i2c_flg) {
switch (function) { switch (function) {
case FUNC_INIT:
sgp30_Init();
break;
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
Sgp30Update(); Sgp30Update();
break; break;