2018-02-18 17:39:28 +00:00
|
|
|
/*
|
|
|
|
xsns_19_mgs.ino - Xadow and Grove Mutichannel Gas sensor support for Sonoff-Tasmota
|
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 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
|
|
|
|
|
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"
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void MGSInit(void) {
|
2018-02-18 17:39:28 +00:00
|
|
|
gas.begin(MGS_SENSOR_ADDR);
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool MGSPrepare(void)
|
2018-02-18 17:39:28 +00:00
|
|
|
{
|
|
|
|
gas.begin(MGS_SENSOR_ADDR);
|
|
|
|
if (!gas.isError()) {
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "MultiGasSensor", MGS_SENSOR_ADDR);
|
2018-02-18 17:39:28 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns19(uint8_t function)
|
2018-02-18 17:39:28 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-02-18 17:39:28 +00:00
|
|
|
static int detected = false;
|
|
|
|
|
|
|
|
if (i2c_flg) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
// MGSInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PREP_BEFORE_TELEPERIOD:
|
|
|
|
detected = MGSPrepare();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
if (detected) MGSShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2018-02-18 17:39:28 +00:00
|
|
|
if (detected) MGSShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_MGS
|
|
|
|
#endif // USE_I2C
|