2018-03-31 15:26:00 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_21_sgp30.ino - SGP30 gas and air quality sensor support for Tasmota
|
2018-03-31 15:26:00 +01:00
|
|
|
|
2022-12-10 15:55:04 +00:00
|
|
|
Copyright (C) 2021 Gerhard Mutz
|
2018-03-31 15:26:00 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_SGP30
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* SGP30 - Gas (TVOC - Total Volatile Organic Compounds) and Air Quality (CO2)
|
|
|
|
*
|
|
|
|
* Source: Gerhard Mutz and Adafruit Industries
|
|
|
|
*
|
|
|
|
* I2C Address: 0x58
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_21 21
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_18 18 // See I2CDEVICES.md
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2019-11-09 17:34:22 +00:00
|
|
|
#define SGP30_ADDRESS 0x58
|
|
|
|
|
2018-03-31 15:26:00 +01:00
|
|
|
#include "Adafruit_SGP30.h"
|
|
|
|
Adafruit_SGP30 sgp;
|
|
|
|
|
2019-11-09 17:34:22 +00:00
|
|
|
bool sgp30_type = false;
|
|
|
|
bool sgp30_ready = false;
|
2019-05-18 12:23:55 +01:00
|
|
|
float sgp30_abshum;
|
2018-03-31 15:26:00 +01:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2019-11-09 17:34:22 +00:00
|
|
|
void sgp30_Init(void)
|
|
|
|
{
|
2021-10-31 14:13:28 +00:00
|
|
|
if (!I2cSetDevice(SGP30_ADDRESS)) { return; }
|
2019-11-09 17:34:22 +00:00
|
|
|
|
2019-05-18 12:23:55 +01:00
|
|
|
if (sgp.begin()) {
|
2019-11-09 17:34:22 +00:00
|
|
|
sgp30_type = true;
|
2021-01-23 16:10:06 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("SGP: Serialnumber 0x%04X-0x%04X-0x%04X"), sgp.serialnumber[0], sgp.serialnumber[1], sgp.serialnumber[2]);
|
2019-11-09 17:34:22 +00:00
|
|
|
I2cSetActiveFound(SGP30_ADDRESS, "SGP30");
|
2019-05-18 12:23:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 13:21:43 +01:00
|
|
|
//#define POW_FUNC pow
|
|
|
|
#define POW_FUNC FastPrecisePow
|
|
|
|
|
2020-06-28 11:23:18 +01:00
|
|
|
float sgp30_AbsoluteHumidity(float temperature, float humidity) {
|
2019-05-18 12:23:55 +01:00
|
|
|
//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;
|
2022-04-19 14:44:53 +01:00
|
|
|
const float mw = 18.01534f; // molar mass of water g/mol
|
|
|
|
const float r = 8.31447215f; // Universal gas constant J/mol/K
|
2019-05-18 12:23:55 +01:00
|
|
|
|
|
|
|
if (isnan(temperature) || isnan(humidity) ) {
|
|
|
|
return NAN;
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:44:53 +01:00
|
|
|
temp = POW_FUNC(2.718281828f, (17.67f * temperature) / (temperature + 243.5f));
|
2019-05-18 13:21:43 +01:00
|
|
|
|
2019-05-18 12:23:55 +01:00
|
|
|
//return (6.112 * temp * humidity * 2.1674) / (273.15 + temperature); //simplified version
|
2022-04-19 14:44:53 +01:00
|
|
|
return (6.112f * temp * humidity * mw) / ((273.15f + temperature) * r); //long version
|
2019-05-18 12:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SAVE_PERIOD 30
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Sgp30Update(void) // Perform every second to ensure proper operation of the baseline compensation algorithm
|
2018-03-31 15:26:00 +01:00
|
|
|
{
|
2019-11-11 16:32:44 +00:00
|
|
|
sgp30_ready = false;
|
|
|
|
if (!sgp.IAQmeasure()) {
|
|
|
|
return; // Measurement failed
|
|
|
|
}
|
2020-10-28 18:03:39 +00:00
|
|
|
if (TasmotaGlobal.global_update && (TasmotaGlobal.humidity > 0) && !isnan(TasmotaGlobal.temperature_celsius)) {
|
2019-11-11 16:32:44 +00:00
|
|
|
// abs hum in mg/m3
|
2020-10-28 18:03:39 +00:00
|
|
|
sgp30_abshum = sgp30_AbsoluteHumidity(TasmotaGlobal.temperature_celsius, TasmotaGlobal.humidity);
|
2019-11-11 16:32:44 +00:00
|
|
|
sgp.setHumidity(sgp30_abshum*1000);
|
|
|
|
}
|
|
|
|
sgp30_ready = true;
|
2019-05-18 12:23:55 +01:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
// these should normally be stored permanently and used for fast restart
|
2020-10-28 16:32:07 +00:00
|
|
|
if (!(TasmotaGlobal.uptime%SAVE_PERIOD)) {
|
2019-11-11 16:32:44 +00:00
|
|
|
// store settings every N seconds
|
|
|
|
uint16_t TVOC_base;
|
|
|
|
uint16_t eCO2_base;
|
2018-03-31 15:26:00 +01:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) return; // Failed to get baseline readings
|
2021-01-23 16:10:06 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base);
|
2018-03-31 15:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 12:23:55 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
const char HTTP_SNS_SGP30[] PROGMEM =
|
2018-04-01 20:54:15 +01:00
|
|
|
"{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}";
|
2019-11-09 17:34:22 +00:00
|
|
|
const char HTTP_SNS_AHUM[] PROGMEM = "{s}SGP30 Abs Humidity{m}%s g/m3{e}";
|
2019-05-18 12:23:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define D_JSON_AHUM "aHumidity"
|
2018-03-31 15:26:00 +01:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Sgp30Show(bool json)
|
2018-03-31 15:26:00 +01:00
|
|
|
{
|
|
|
|
if (sgp30_ready) {
|
2019-05-18 12:23:55 +01:00
|
|
|
char abs_hum[33];
|
2021-11-09 20:07:43 +00:00
|
|
|
bool ahum_available = TasmotaGlobal.global_update && (TasmotaGlobal.humidity > 0) && !isnan(TasmotaGlobal.temperature_celsius);
|
|
|
|
if (ahum_available) {
|
2020-05-23 22:41:50 +01:00
|
|
|
// has humidity + temperature
|
|
|
|
dtostrfd(sgp30_abshum,4,abs_hum);
|
|
|
|
}
|
2018-03-31 15:26:00 +01:00
|
|
|
if (json) {
|
2019-05-18 12:23:55 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"SGP30\":{\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TVOC "\":%d"), sgp.eCO2, sgp.TVOC);
|
2021-11-09 20:07:43 +00:00
|
|
|
if (ahum_available) {
|
2019-05-18 12:23:55 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_AHUM "\":%s"),abs_hum);
|
|
|
|
}
|
2019-09-12 13:19:44 +01:00
|
|
|
ResponseJsonEnd();
|
2018-03-31 15:26:00 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
2020-10-29 12:37:09 +00:00
|
|
|
if (0 == TasmotaGlobal.tele_period) DomoticzSensor(DZ_AIRQUALITY, sgp.eCO2);
|
2018-03-31 15:26:00 +01:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
2018-05-28 10:35:23 +01:00
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_SGP30, sgp.eCO2, sgp.TVOC);
|
2021-11-09 20:07:43 +00:00
|
|
|
if (ahum_available) {
|
2019-05-18 12:23:55 +01:00
|
|
|
WSContentSend_PD(HTTP_SNS_AHUM, abs_hum);
|
|
|
|
}
|
2018-03-31 15:26:00 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xsns21(uint32_t function)
|
2018-03-31 15:26:00 +01:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_18)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-03-31 15:26:00 +01:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
sgp30_Init();
|
|
|
|
}
|
|
|
|
else if (sgp30_type) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Sgp30Update();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Sgp30Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Sgp30Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2018-03-31 15:26:00 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_SGP30
|
2019-05-18 12:23:55 +01:00
|
|
|
#endif // USE_I2C
|