2018-02-18 17:39:28 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_19_mgs.ino - Xadow and Grove Mutichannel Gas sensor support for Tasmota
|
2018-02-18 17:39:28 +00:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Palich2000 and Theo Arends
|
2018-02-18 17:39:28 +00: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_MGS
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Grove - Multichannel Gas Sensor
|
|
|
|
* http://wiki.seeed.cc/Grove-Multichannel_Gas_Sensor/
|
|
|
|
*
|
|
|
|
* https://github.com/Seeed-Studio/Mutichannel_Gas_Sensor.git
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_19 19
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_17 17 // See I2CDEVICES.md
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2018-02-18 17:39:28 +00:00
|
|
|
#ifndef MGS_SENSOR_ADDR
|
|
|
|
#define MGS_SENSOR_ADDR 0x04 // Default Mutichannel Gas sensor i2c address
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "MutichannelGasSensor.h"
|
|
|
|
|
2019-11-07 16:08:58 +00:00
|
|
|
bool mgs_detected = false;
|
|
|
|
|
|
|
|
void MGSPrepare(void)
|
2018-02-18 17:39:28 +00:00
|
|
|
{
|
2021-10-31 14:13:28 +00:00
|
|
|
if (!I2cSetDevice(MGS_SENSOR_ADDR)) { return; }
|
2019-11-07 16:08:58 +00:00
|
|
|
|
2018-02-18 17:39:28 +00:00
|
|
|
gas.begin(MGS_SENSOR_ADDR);
|
|
|
|
if (!gas.isError()) {
|
2019-11-09 17:34:22 +00:00
|
|
|
I2cSetActiveFound(MGS_SENSOR_ADDR, "MultiGas");
|
2019-11-07 16:08:58 +00:00
|
|
|
mgs_detected = true;
|
2018-02-18 17:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char* measure_gas(int gas_type, char* buffer)
|
|
|
|
{
|
|
|
|
float f = gas.calcGas(gas_type);
|
|
|
|
dtostrfd(f, 2, buffer);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
const char HTTP_MGS_GAS[] PROGMEM = "{s}MGS %s{m}%s " D_UNIT_PARTS_PER_MILLION "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
2018-02-18 17:39:28 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void MGSShow(bool json)
|
2018-02-18 17:39:28 +00:00
|
|
|
{
|
2018-12-21 15:17:06 +00:00
|
|
|
char buffer[33];
|
2018-02-18 17:39:28 +00:00
|
|
|
if (json) {
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"MGS\":{\"NH3\":%s"), measure_gas(NH3, buffer));
|
|
|
|
ResponseAppend_P(PSTR(",\"CO\":%s"), measure_gas(CO, buffer));
|
|
|
|
ResponseAppend_P(PSTR(",\"NO2\":%s"), measure_gas(NO2, buffer));
|
|
|
|
ResponseAppend_P(PSTR(",\"C3H8\":%s"), measure_gas(C3H8, buffer));
|
|
|
|
ResponseAppend_P(PSTR(",\"C4H10\":%s"), measure_gas(C4H10, buffer));
|
|
|
|
ResponseAppend_P(PSTR(",\"CH4\":%s"), measure_gas(GAS_CH4, buffer));
|
|
|
|
ResponseAppend_P(PSTR(",\"H2\":%s"), measure_gas(H2, buffer));
|
|
|
|
ResponseAppend_P(PSTR(",\"C2H5OH\":%s}"), measure_gas(C2H5OH, buffer));
|
2018-02-18 17:39:28 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_MGS_GAS, "NH3", measure_gas(NH3, buffer));
|
|
|
|
WSContentSend_PD(HTTP_MGS_GAS, "CO", measure_gas(CO, buffer));
|
|
|
|
WSContentSend_PD(HTTP_MGS_GAS, "NO2", measure_gas(NO2, buffer));
|
|
|
|
WSContentSend_PD(HTTP_MGS_GAS, "C3H8", measure_gas(C3H8, buffer));
|
|
|
|
WSContentSend_PD(HTTP_MGS_GAS, "C4H10", measure_gas(C4H10, buffer));
|
|
|
|
WSContentSend_PD(HTTP_MGS_GAS, "CH4", measure_gas(GAS_CH4, buffer));
|
|
|
|
WSContentSend_PD(HTTP_MGS_GAS, "H2", measure_gas(H2, buffer));
|
|
|
|
WSContentSend_PD(HTTP_MGS_GAS, "C2H5OH", measure_gas(C2H5OH, buffer));
|
2018-02-18 17:39:28 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xsns19(uint32_t function)
|
2018-02-18 17:39:28 +00:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_17)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-02-18 17:39:28 +00:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
MGSPrepare();
|
|
|
|
}
|
|
|
|
else if (mgs_detected) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
MGSShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
MGSShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2018-02-18 17:39:28 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_MGS
|
|
|
|
#endif // USE_I2C
|