2018-07-23 15:27:15 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_31_ccs811.ino - CCS811 gas and air quality sensor support for Tasmota
|
2018-07-23 15:27:15 +01:00
|
|
|
|
2019-12-31 13:23:34 +00:00
|
|
|
Copyright (C) 2020 Gerhard Mutz and Theo Arends
|
2018-07-23 15:27:15 +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_CCS811
|
|
|
|
/*********************************************************************************************\
|
2018-10-01 17:06:25 +01:00
|
|
|
* CCS811 - Gas (TVOC - Total Volatile Organic Compounds) and Air Quality (CO2)
|
2018-07-23 15:27:15 +01:00
|
|
|
*
|
2018-07-23 16:19:09 +01:00
|
|
|
* Source: Adafruit
|
2018-07-23 15:27:15 +01:00
|
|
|
*
|
|
|
|
* I2C Address: 0x5A assumes ADDR connected to Gnd, Wake also must be grounded
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_31 31
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_24 24 // See I2CDEVICES.md
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2019-11-11 17:36:13 +00:00
|
|
|
#define EVERYNSECONDS 5
|
|
|
|
|
2018-07-23 15:27:15 +01:00
|
|
|
#include "Adafruit_CCS811.h"
|
|
|
|
|
|
|
|
Adafruit_CCS811 ccs;
|
2019-11-10 16:02:02 +00:00
|
|
|
uint8_t CCS811_ready = 0;
|
|
|
|
uint8_t CCS811_type = 0;;
|
2018-07-23 15:27:15 +01:00
|
|
|
uint16_t eCO2;
|
|
|
|
uint16_t TVOC;
|
2018-07-23 16:42:12 +01:00
|
|
|
uint8_t tcnt = 0;
|
|
|
|
uint8_t ecnt = 0;
|
2018-07-23 15:27:15 +01:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2019-11-11 17:36:13 +00:00
|
|
|
void CCS811Detect(void)
|
2018-07-23 15:27:15 +01:00
|
|
|
{
|
2019-11-10 16:02:02 +00:00
|
|
|
if (I2cActive(CCS811_ADDRESS)) { return; }
|
|
|
|
|
2019-11-11 17:36:13 +00:00
|
|
|
if (!ccs.begin(CCS811_ADDRESS)) {
|
|
|
|
CCS811_type = 1;
|
|
|
|
I2cSetActiveFound(CCS811_ADDRESS, "CCS811");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCS811Update(void) // Perform every n second
|
|
|
|
{
|
2018-07-23 16:42:12 +01:00
|
|
|
tcnt++;
|
|
|
|
if (tcnt >= EVERYNSECONDS) {
|
|
|
|
tcnt = 0;
|
2018-07-23 15:27:15 +01:00
|
|
|
CCS811_ready = 0;
|
2019-11-11 17:36:13 +00:00
|
|
|
if (ccs.available()) {
|
|
|
|
if (!ccs.readData()){
|
|
|
|
TVOC = ccs.getTVOC();
|
|
|
|
eCO2 = ccs.geteCO2();
|
|
|
|
CCS811_ready = 1;
|
2020-10-28 18:03:39 +00:00
|
|
|
if (TasmotaGlobal.global_update && (TasmotaGlobal.humidity > 0) && !isnan(TasmotaGlobal.temperature_celsius)) {
|
|
|
|
ccs.setEnvironmentalData((uint8_t)TasmotaGlobal.humidity, TasmotaGlobal.temperature_celsius);
|
2020-06-12 15:51:21 +01:00
|
|
|
}
|
2019-11-11 17:36:13 +00:00
|
|
|
ecnt = 0;
|
2018-07-23 15:27:15 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-11 17:36:13 +00:00
|
|
|
// failed, count up
|
|
|
|
ecnt++;
|
|
|
|
if (ecnt > 6) {
|
|
|
|
// after 30 seconds, restart
|
|
|
|
ccs.begin(CCS811_ADDRESS);
|
2018-07-23 15:27:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 16:31:43 +00:00
|
|
|
const char HTTP_SNS_CCS811[] PROGMEM =
|
2018-07-23 15:27:15 +01:00
|
|
|
"{s}CCS811 " D_ECO2 "{m}%d " D_UNIT_PARTS_PER_MILLION "{e}" // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
|
|
|
"{s}CCS811 " D_TVOC "{m}%d " D_UNIT_PARTS_PER_BILLION "{e}";
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void CCS811Show(bool json)
|
2018-07-23 15:27:15 +01:00
|
|
|
{
|
|
|
|
if (CCS811_ready) {
|
|
|
|
if (json) {
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"CCS811\":{\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TVOC "\":%d}"), eCO2,TVOC);
|
2018-07-23 15:27:15 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
2020-10-29 12:37:09 +00:00
|
|
|
if (0 == TasmotaGlobal.tele_period) DomoticzSensor(DZ_AIRQUALITY, eCO2);
|
2018-07-23 15:27:15 +01:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
2018-07-23 16:17:40 +01:00
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_CCS811, eCO2, TVOC);
|
2018-07-23 15:27:15 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns31(uint8_t function)
|
2018-07-23 15:27:15 +01:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_24)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-07-23 15:27:15 +01:00
|
|
|
|
2019-11-11 17:36:13 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
CCS811Detect();
|
|
|
|
}
|
|
|
|
else if (CCS811_type) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
CCS811Update();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
CCS811Show(1);
|
|
|
|
break;
|
2018-07-23 15:27:15 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-11 17:36:13 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
CCS811Show(0);
|
|
|
|
break;
|
2018-07-23 15:27:15 +01:00
|
|
|
#endif // USE_WEBSERVER
|
2019-11-11 17:36:13 +00:00
|
|
|
}
|
2018-07-23 15:27:15 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_CCS811
|
|
|
|
#endif // USE_I2C
|