2018-07-23 15:27:15 +01:00
|
|
|
/*
|
2018-07-23 16:19:09 +01:00
|
|
|
xsns_31_ccs811.ino - CCS811 gas and air quality sensor support for Sonoff-Tasmota
|
2018-07-23 15:27:15 +01:00
|
|
|
|
2018-07-23 16:19:09 +01:00
|
|
|
Copyright (C) 2018 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
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#include "Adafruit_CCS811.h"
|
|
|
|
|
|
|
|
Adafruit_CCS811 ccs;
|
|
|
|
uint8_t CCS811_ready;
|
|
|
|
uint8_t CCS811_type;
|
|
|
|
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
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
#define EVERYNSECONDS 5
|
|
|
|
|
|
|
|
void CCS811Update() // 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;
|
|
|
|
if (!CCS811_type) {
|
2018-09-01 23:47:35 +01:00
|
|
|
sint8_t res = ccs.begin(CCS811_ADDRESS);
|
|
|
|
if (!res) {
|
2018-07-23 15:27:15 +01:00
|
|
|
CCS811_type = 1;
|
|
|
|
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "CCS811", 0x5A);
|
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
} else {
|
2018-09-01 23:47:35 +01:00
|
|
|
//snprintf_P(log_data, sizeof(log_data), "CCS811 init failed: %d",res);
|
2018-07-23 15:27:15 +01:00
|
|
|
//AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ccs.available()) {
|
|
|
|
if (!ccs.readData()){
|
2018-07-23 16:42:12 +01:00
|
|
|
TVOC = ccs.getTVOC();
|
|
|
|
eCO2 = ccs.geteCO2();
|
2018-07-23 15:27:15 +01:00
|
|
|
CCS811_ready = 1;
|
2018-07-24 17:41:50 +01:00
|
|
|
if (global_update) { ccs.setEnvironmentalData((uint8_t)global_humidity, global_temperature); }
|
2018-07-23 16:42:12 +01:00
|
|
|
ecnt = 0;
|
2018-07-23 15:27:15 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// failed, count up
|
2018-07-23 16:42:12 +01:00
|
|
|
ecnt++;
|
|
|
|
if (ecnt > 6) {
|
2018-07-23 15:27:15 +01:00
|
|
|
// after 30 seconds, restart
|
|
|
|
ccs.begin(CCS811_ADDRESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char HTTP_SNS_CCS811[] PROGMEM = "%s"
|
|
|
|
"{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}";
|
|
|
|
|
|
|
|
void CCS811Show(boolean json)
|
|
|
|
{
|
|
|
|
if (CCS811_ready) {
|
|
|
|
if (json) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"CCS811\":{\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TVOC "\":%d}"), mqtt_data,eCO2,TVOC);
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
if (0 == tele_period) DomoticzSensor(DZ_AIRQUALITY, eCO2);
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
2018-07-23 16:17:40 +01:00
|
|
|
} else {
|
2018-07-23 15:27:15 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_CCS811, mqtt_data, eCO2, TVOC);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-07-23 16:17:40 +01:00
|
|
|
#define XSNS_31
|
2018-07-23 15:27:15 +01:00
|
|
|
|
2018-07-23 16:17:40 +01:00
|
|
|
boolean Xsns31(byte function)
|
2018-07-23 15:27:15 +01:00
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
if (i2c_flg) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
CCS811Update();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
CCS811Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_APPEND:
|
|
|
|
CCS811Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_CCS811
|
|
|
|
#endif // USE_I2C
|