2017-12-22 13:55:24 +00:00
|
|
|
/*
|
2017-12-25 16:41:12 +00:00
|
|
|
xsns_17_senseair.ino - SenseAir CO2 sensor support for Sonoff-Tasmota
|
2017-12-22 13:55:24 +00:00
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends
|
2017-12-22 13:55:24 +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_SENSEAIR
|
|
|
|
/*********************************************************************************************\
|
2017-12-25 16:41:12 +00:00
|
|
|
* SenseAir K30, K70 and S8 - CO2 sensor
|
2017-12-22 13:55:24 +00:00
|
|
|
*
|
|
|
|
* Adapted from EspEasy plugin P052 by Mikael Trieb (mikael__AT__triebconsulting.se)
|
2018-05-10 16:21:26 +01:00
|
|
|
*
|
|
|
|
* Hardware Serial will be selected if GPIO1 = [SAir Rx] and GPIO3 = [SAir Tx]
|
2017-12-22 13:55:24 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_17 17
|
|
|
|
|
2018-10-15 16:18:30 +01:00
|
|
|
#define SENSEAIR_MODBUS_SPEED 9600
|
|
|
|
#define SENSEAIR_DEVICE_ADDRESS 0xFE // Any address
|
|
|
|
#define SENSEAIR_READ_REGISTER 0x04 // Command Read
|
2017-12-25 16:41:12 +00:00
|
|
|
|
2017-12-27 13:10:55 +00:00
|
|
|
#ifndef CO2_LOW
|
|
|
|
#define CO2_LOW 800 // Below this CO2 value show green light
|
|
|
|
#endif
|
|
|
|
#ifndef CO2_HIGH
|
|
|
|
#define CO2_HIGH 1200 // Above this CO2 value show red light
|
|
|
|
#endif
|
|
|
|
|
2018-10-15 16:18:30 +01:00
|
|
|
#include <TasmotaModbus.h>
|
|
|
|
TasmotaModbus *SenseairModbus;
|
2017-12-22 13:55:24 +00:00
|
|
|
|
|
|
|
const char kSenseairTypes[] PROGMEM = "Kx0|S8";
|
|
|
|
|
|
|
|
uint8_t senseair_type = 1;
|
|
|
|
char senseair_types[7];
|
|
|
|
|
|
|
|
uint16_t senseair_co2 = 0;
|
|
|
|
float senseair_temperature = 0;
|
|
|
|
float senseair_humidity = 0;
|
|
|
|
|
2018-08-26 16:10:18 +01:00
|
|
|
//uint8_t senseair_state = 0;
|
2017-12-22 13:55:24 +00:00
|
|
|
|
|
|
|
const uint8_t start_addresses[] { 0x1A, 0x00, 0x03, 0x04, 0x05, 0x1C, 0x0A };
|
|
|
|
|
|
|
|
uint8_t senseair_read_state = 0;
|
|
|
|
uint8_t senseair_send_retry = 0;
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Senseair250ms(void) // Every 250 mSec
|
2017-12-22 13:55:24 +00:00
|
|
|
{
|
2018-08-26 16:10:18 +01:00
|
|
|
// senseair_state++;
|
|
|
|
// if (6 == senseair_state) { // Every 300 mSec
|
|
|
|
// senseair_state = 0;
|
2017-12-25 16:41:12 +00:00
|
|
|
|
|
|
|
uint16_t value = 0;
|
2018-10-15 16:18:30 +01:00
|
|
|
bool data_ready = SenseairModbus->ReceiveReady();
|
2017-12-25 16:41:12 +00:00
|
|
|
|
|
|
|
if (data_ready) {
|
2018-10-15 16:18:30 +01:00
|
|
|
uint8_t error = SenseairModbus->Receive16BitRegister(&value);
|
2017-12-25 16:41:12 +00:00
|
|
|
if (error) {
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "SenseAir response error %d"), error);
|
2017-12-25 16:41:12 +00:00
|
|
|
} else {
|
|
|
|
switch(senseair_read_state) {
|
|
|
|
case 0: // 0x1A (26) READ_TYPE_LOW - S8: fe 04 02 01 77 ec 92
|
|
|
|
senseair_type = 2;
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "SenseAir type id low %04X"), value);
|
2017-12-25 16:41:12 +00:00
|
|
|
break;
|
|
|
|
case 1: // 0x00 (0) READ_ERRORLOG - fe 04 02 00 00 ad 24
|
|
|
|
if (value) {
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "SenseAir error %04X"), value);
|
2017-12-25 16:41:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // 0x03 (3) READ_CO2 - fe 04 02 06 2c af 59
|
|
|
|
senseair_co2 = value;
|
2017-12-27 13:10:55 +00:00
|
|
|
LightSetSignal(CO2_LOW, CO2_HIGH, senseair_co2);
|
2017-12-25 16:41:12 +00:00
|
|
|
break;
|
|
|
|
case 3: // 0x04 (4) READ_TEMPERATURE - S8: fe 84 02 f2 f1 - Illegal Data Address
|
|
|
|
senseair_temperature = ConvertTemp((float)value / 100);
|
|
|
|
break;
|
|
|
|
case 4: // 0x05 (5) READ_HUMIDITY - S8: fe 84 02 f2 f1 - Illegal Data Address
|
2019-04-15 17:12:42 +01:00
|
|
|
senseair_humidity = ConvertHumidity((float)value / 100);
|
2017-12-25 16:41:12 +00:00
|
|
|
break;
|
|
|
|
case 5: // 0x1C (28) READ_RELAY_STATE - S8: fe 04 02 01 54 ad 4b - firmware version
|
|
|
|
{
|
|
|
|
bool relay_state = value >> 8 & 1;
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "SenseAir relay state %d"), relay_state);
|
2017-12-25 16:41:12 +00:00
|
|
|
break;
|
2017-12-22 13:55:24 +00:00
|
|
|
}
|
2017-12-25 16:41:12 +00:00
|
|
|
case 6: // 0x0A (10) READ_TEMP_ADJUSTMENT - S8: fe 84 02 f2 f1 - Illegal Data Address
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "SenseAir temp adjustment %d"), value);
|
2017-12-25 16:41:12 +00:00
|
|
|
break;
|
2017-12-22 13:55:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-25 16:41:12 +00:00
|
|
|
senseair_read_state++;
|
|
|
|
if (2 == senseair_type) { // S8
|
|
|
|
if (3 == senseair_read_state) {
|
|
|
|
senseair_read_state = 1;
|
|
|
|
}
|
|
|
|
} else { // K30, K70
|
|
|
|
if (sizeof(start_addresses) == senseair_read_state) {
|
|
|
|
senseair_read_state = 1;
|
|
|
|
}
|
2017-12-22 13:55:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-25 16:41:12 +00:00
|
|
|
if (0 == senseair_send_retry || data_ready) {
|
|
|
|
senseair_send_retry = 5;
|
2018-10-15 16:18:30 +01:00
|
|
|
SenseairModbus->Send(SENSEAIR_DEVICE_ADDRESS, SENSEAIR_READ_REGISTER, (uint16_t)start_addresses[senseair_read_state], 1);
|
2017-12-25 16:41:12 +00:00
|
|
|
} else {
|
|
|
|
senseair_send_retry--;
|
|
|
|
}
|
|
|
|
|
2018-08-26 16:10:18 +01:00
|
|
|
// }
|
2017-12-22 13:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void SenseairInit(void)
|
2017-12-22 13:55:24 +00:00
|
|
|
{
|
|
|
|
senseair_type = 0;
|
|
|
|
if ((pin[GPIO_SAIR_RX] < 99) && (pin[GPIO_SAIR_TX] < 99)) {
|
2018-10-15 16:18:30 +01:00
|
|
|
SenseairModbus = new TasmotaModbus(pin[GPIO_SAIR_RX], pin[GPIO_SAIR_TX]);
|
|
|
|
uint8_t result = SenseairModbus->Begin(SENSEAIR_MODBUS_SPEED);
|
|
|
|
if (result) {
|
|
|
|
if (2 == result) { ClaimSerial(); }
|
2017-12-22 13:55:24 +00:00
|
|
|
senseair_type = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void SenseairShow(bool json)
|
2017-12-22 13:55:24 +00:00
|
|
|
{
|
2018-12-21 15:17:06 +00:00
|
|
|
char temperature[33];
|
2017-12-22 13:55:24 +00:00
|
|
|
dtostrfd(senseair_temperature, Settings.flag2.temperature_resolution, temperature);
|
2018-12-21 15:17:06 +00:00
|
|
|
char humidity[33];
|
2017-12-22 13:55:24 +00:00
|
|
|
dtostrfd(senseair_humidity, Settings.flag2.temperature_resolution, humidity);
|
|
|
|
GetTextIndexed(senseair_types, sizeof(senseair_types), senseair_type -1, kSenseairTypes);
|
|
|
|
|
|
|
|
if (json) {
|
2019-05-19 11:07:20 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_CO2 "\":%d"), senseair_types, senseair_co2);
|
2017-12-22 13:55:24 +00:00
|
|
|
if (senseair_type != 2) {
|
2019-05-19 11:07:20 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_HUMIDITY "\":%s"), temperature, humidity);
|
2017-12-22 13:55:24 +00:00
|
|
|
}
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
ResponseJsonEnd();
|
2017-12-22 13:55:24 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-04-11 09:11:20 +01:00
|
|
|
if (0 == tele_period) DomoticzSensor(DZ_AIRQUALITY, senseair_co2);
|
2017-12-22 13:55:24 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_CO2, senseair_types, senseair_co2);
|
2017-12-22 13:55:24 +00:00
|
|
|
if (senseair_type != 2) {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_TEMP, senseair_types, temperature, TempUnit());
|
|
|
|
WSContentSend_PD(HTTP_SNS_HUM, senseair_types, humidity);
|
2017-12-22 13:55:24 +00:00
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns17(uint8_t function)
|
2017-12-22 13:55:24 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-12-22 13:55:24 +00:00
|
|
|
|
|
|
|
if (senseair_type) {
|
|
|
|
switch (function) {
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_INIT:
|
2017-12-22 13:55:24 +00:00
|
|
|
SenseairInit();
|
|
|
|
break;
|
2018-08-26 16:10:18 +01:00
|
|
|
case FUNC_EVERY_250_MSECOND:
|
|
|
|
Senseair250ms();
|
2017-12-25 16:41:12 +00:00
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
2017-12-22 13:55:24 +00:00
|
|
|
SenseairShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2017-12-22 13:55:24 +00:00
|
|
|
SenseairShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_SENSEAIR
|