Tasmota/tasmota/tasmota_xsns_sensor/xsns_42_scd30.ino

361 lines
11 KiB
Arduino
Raw Normal View History

2019-03-10 01:44:42 +00:00
/*
xsns_42_scd30.ino - SCD30 CO2 sensor support for Tasmota
2019-03-10 01:44:42 +00:00
2021-01-01 12:44:04 +00:00
Copyright (C) 2021 Frogmore42
2019-03-10 01:44:42 +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_SCD30
2022-09-04 15:14:32 +01:00
/*********************************************************************************************\
* SCD30 NDIR CO2 Temperature and Humidity sensor
\*********************************************************************************************/
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
#define XSNS_42 42
#define XI2C_29 29 // See I2CDEVICES.md
2019-11-11 17:36:13 +00:00
//#define SCD30_DEBUG
2022-09-04 15:14:32 +01:00
#define SCD30_ADDRESS 0x61
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
#define SCD30_MAX_MISSED_READS 3
#define SCD30_STATE_NO_ERROR 0
#define SCD30_STATE_ERROR_DATA_CRC 1
#define SCD30_STATE_ERROR_READ_MEAS 2
#define SCD30_STATE_ERROR_SOFT_RESET 3
#define SCD30_STATE_ERROR_I2C_RESET 4
#define SCD30_STATE_ERROR_UNKNOWN 5
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
const char kScd30Commands[] PROGMEM = "Scd30|" // Prefix
"Alt|Auto|Cal|FW|Int|Pres|TOff";
void (*const kScd30Command[])(void) PROGMEM = {
&CmndScd30Altitude, &CmndScd30AutoMode, &CmndScd30Calibrate, &CmndScd30Firmware, &CmndScd30Interval, &CmndScd30Pressure, &CmndScd30TempOffset };
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
/********************************************************************************************/
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
#include <FrogmoreScd30.h>
2019-03-10 01:44:42 +00:00
FrogmoreScd30 scd30;
2022-09-04 15:14:32 +01:00
struct {
float humidity = 0.0f;
float temperature = 0.0f;
int error_state = SCD30_STATE_NO_ERROR;
int loop_count = 0;
int data_not_available_count = 0;
int good_measure_count = 0;
int reset_count = 0;
int error_count = 0;
int co2_zero_count = 0;
int i2c_reset_count = 0;
uint16_t interval;
uint16_t co2 = 0;
uint16_t co2e_avg = 0;
bool init_once;
bool found = false;
bool data_valid = false;
} Scd30;
void Scd30Detect(void) {
if (!I2cSetDevice(SCD30_ADDRESS)) { return; }
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
scd30.begin();
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
uint8_t major = 0;
uint8_t minor = 0;
if (scd30.getFirmwareVersion(&major, &minor)) { return; }
2022-09-04 15:14:32 +01:00
if (scd30.getMeasurementInterval(&Scd30.interval)) { return; }
2019-11-11 17:36:13 +00:00
if (scd30.beginMeasuring()) { return; }
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
I2cSetActiveFound(SCD30_ADDRESS, "SCD30");
2022-09-04 15:14:32 +01:00
Scd30.found = true;
2019-11-11 17:36:13 +00:00
2022-09-04 15:14:32 +01:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("SCD: FW v%d.%d"), major, minor);
2019-03-10 01:44:42 +00:00
}
// gets data from the sensor every 3 seconds or so to give the sensor time to gather new data
2022-09-04 15:14:32 +01:00
void Scd30Update(void) {
Scd30.loop_count++;
if (Scd30.loop_count > (Scd30.interval - 1)) {
2020-07-14 17:30:23 +01:00
uint32_t error = 0;
2022-09-04 15:14:32 +01:00
switch (Scd30.error_state) {
2019-11-11 17:36:13 +00:00
case SCD30_STATE_NO_ERROR: {
2022-09-04 15:14:32 +01:00
error = scd30.readMeasurement(&Scd30.co2, &Scd30.co2e_avg, &Scd30.temperature, &Scd30.humidity);
2019-11-11 17:36:13 +00:00
switch (error) {
case ERROR_SCD30_NO_ERROR:
2022-09-04 15:14:32 +01:00
Scd30.loop_count = 0;
Scd30.data_valid = true;
Scd30.good_measure_count++;
#ifdef USE_LIGHT
LightSetSignal(CO2_LOW, CO2_HIGH, Scd30.co2);
#endif // USE_LIGHT
2019-11-11 17:36:13 +00:00
break;
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
case ERROR_SCD30_NO_DATA:
2022-09-04 15:14:32 +01:00
Scd30.data_not_available_count++;
2019-11-11 17:36:13 +00:00
break;
case ERROR_SCD30_CRC_ERROR:
2022-09-04 15:14:32 +01:00
Scd30.error_state = SCD30_STATE_ERROR_DATA_CRC;
Scd30.error_count++;
2019-03-10 01:44:42 +00:00
#ifdef SCD30_DEBUG
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: CRC error, CRC error: %ld, CO2 zero: %ld, good: %ld, no data: %ld, sc30_reset: %ld, i2c_reset: %ld"),
2022-09-04 15:14:32 +01:00
Scd30.error_count, Scd30.co2_zero_count, Scd30.good_measure_count, Scd30.data_not_available_count, Scd30.reset_count, Scd30.i2c_reset_count);
2019-03-10 01:44:42 +00:00
#endif
2019-11-11 17:36:13 +00:00
break;
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
case ERROR_SCD30_CO2_ZERO:
2022-09-04 15:14:32 +01:00
Scd30.co2_zero_count++;
2019-03-10 01:44:42 +00:00
#ifdef SCD30_DEBUG
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: CO2 zero, CRC error: %ld, CO2 zero: %ld, good: %ld, no data: %ld, sc30_reset: %ld, i2c_reset: %ld"),
2022-09-04 15:14:32 +01:00
Scd30.error_count, Scd30.co2_zero_count, Scd30.good_measure_count, Scd30.data_not_available_count, Scd30.reset_count, Scd30.i2c_reset_count);
2019-03-10 01:44:42 +00:00
#endif
2019-11-11 17:36:13 +00:00
break;
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
default: {
2022-09-04 15:14:32 +01:00
Scd30.error_state = SCD30_STATE_ERROR_READ_MEAS;
2019-03-10 01:44:42 +00:00
#ifdef SCD30_DEBUG
2022-09-04 15:14:32 +01:00
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: Update: ReadMeasurement error: 0x%lX, counter: %ld"), error, Scd30.loop_count);
2019-03-10 01:44:42 +00:00
#endif
2019-11-11 17:36:13 +00:00
return;
2019-03-10 01:44:42 +00:00
}
2019-11-11 17:36:13 +00:00
break;
2019-03-10 01:44:42 +00:00
}
2019-11-11 17:36:13 +00:00
}
break;
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
case SCD30_STATE_ERROR_DATA_CRC: {
2022-09-04 15:14:32 +01:00
//Scd30.data_valid = false;
2019-03-10 01:44:42 +00:00
#ifdef SCD30_DEBUG
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld"),
2022-09-04 15:14:32 +01:00
Scd30.error_state, Scd30.good_measure_count, Scd30.data_not_available_count, Scd30.reset_count, Scd30.i2c_reset_count);
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: got CRC error, try again, counter: %ld"), Scd30.loop_count);
2019-03-10 01:44:42 +00:00
#endif
2022-09-04 15:14:32 +01:00
Scd30.error_state = ERROR_SCD30_NO_ERROR;
2019-11-11 17:36:13 +00:00
}
break;
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
case SCD30_STATE_ERROR_READ_MEAS: {
2022-09-04 15:14:32 +01:00
//Scd30.data_valid = false;
2019-03-10 01:44:42 +00:00
#ifdef SCD30_DEBUG
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld"),
2022-09-04 15:14:32 +01:00
Scd30.error_state, Scd30.good_measure_count, Scd30.data_not_available_count, Scd30.reset_count, Scd30.i2c_reset_count);
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: not answering, sending soft reset, counter: %ld"), Scd30.loop_count);
2019-03-10 01:44:42 +00:00
#endif
2022-09-04 15:14:32 +01:00
Scd30.reset_count++;
2019-11-11 17:36:13 +00:00
error = scd30.softReset();
if (error) {
2019-03-10 01:44:42 +00:00
#ifdef SCD30_DEBUG
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: resetting got error: 0x%lX"), error);
2019-03-10 01:44:42 +00:00
#endif
2019-11-11 17:36:13 +00:00
error >>= 8;
if (error == 4) {
2022-09-04 15:14:32 +01:00
Scd30.error_state = SCD30_STATE_ERROR_SOFT_RESET;
2019-11-11 17:36:13 +00:00
} else {
2022-09-04 15:14:32 +01:00
Scd30.error_state = SCD30_STATE_ERROR_UNKNOWN;
2019-03-10 01:44:42 +00:00
}
2019-11-11 17:36:13 +00:00
} else {
2022-09-04 15:14:32 +01:00
Scd30.error_state = ERROR_SCD30_NO_ERROR;
2019-03-10 01:44:42 +00:00
}
2019-11-11 17:36:13 +00:00
}
break;
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
case SCD30_STATE_ERROR_SOFT_RESET: {
2022-09-04 15:14:32 +01:00
//Scd30.data_valid = false;
2019-03-10 01:44:42 +00:00
#ifdef SCD30_DEBUG
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld"),
2022-09-04 15:14:32 +01:00
Scd30.error_state, Scd30.good_measure_count, Scd30.data_not_available_count, Scd30.reset_count, Scd30.i2c_reset_count);
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: clearing i2c bus"));
2019-03-10 01:44:42 +00:00
#endif
2022-09-04 15:14:32 +01:00
Scd30.i2c_reset_count++;
2019-11-11 17:36:13 +00:00
error = scd30.clearI2CBus();
if (error) {
2022-09-04 15:14:32 +01:00
Scd30.error_state = SCD30_STATE_ERROR_I2C_RESET;
2019-03-10 01:44:42 +00:00
#ifdef SCD30_DEBUG
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: error clearing i2c bus: 0x%lX"), error);
2019-03-10 01:44:42 +00:00
#endif
2019-11-11 17:36:13 +00:00
} else {
2022-09-04 15:14:32 +01:00
Scd30.error_state = ERROR_SCD30_NO_ERROR;
2019-03-10 01:44:42 +00:00
}
2019-11-11 17:36:13 +00:00
}
break;
2019-03-10 01:44:42 +00:00
2019-11-11 17:36:13 +00:00
default: {
2022-09-04 15:14:32 +01:00
//Scd30.data_valid = false;
2019-03-10 01:44:42 +00:00
#ifdef SCD30_DEBUG
2022-09-04 15:14:32 +01:00
AddLog(LOG_LEVEL_ERROR, PSTR("SCD30: unknown error state: 0x%lX"), Scd30.error_state);
2019-03-10 01:44:42 +00:00
#endif
2022-09-04 15:14:32 +01:00
Scd30.error_state = SCD30_STATE_ERROR_SOFT_RESET; // try again
2019-03-10 01:44:42 +00:00
}
2019-11-11 17:36:13 +00:00
}
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
if (Scd30.loop_count > (SCD30_MAX_MISSED_READS * Scd30.interval)) {
Scd30.data_valid = false;
2019-03-10 01:44:42 +00:00
}
}
}
2022-09-04 15:14:32 +01:00
/*********************************************************************************************\
* Command Scd30
\*********************************************************************************************/
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
void CmndScd30Altitude(void) {
uint16_t value = 0;
if (XdrvMailbox.data_len > 0) {
value = XdrvMailbox.payload;
scd30.setAltitudeCompensation(value);
} else {
scd30.getAltitudeCompensation(&value);
2019-03-10 01:44:42 +00:00
}
2022-09-04 15:14:32 +01:00
ResponseCmndNumber(value);
};
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
void CmndScd30AutoMode(void) {
uint16_t value = 0;
if (XdrvMailbox.data_len > 0) {
value = XdrvMailbox.payload;
scd30.setCalibrationType(value);
} else {
scd30.getCalibrationType(&value);
}
ResponseCmndNumber(value);
};
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
void CmndScd30Calibrate(void) {
uint16_t value = 0;
if (XdrvMailbox.data_len > 0) {
value = XdrvMailbox.payload;
scd30.setForcedRecalibrationFactor(value);
} else {
scd30.getForcedRecalibrationFactor(&value);
}
ResponseCmndNumber(value);
};
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
void CmndScd30Firmware(void) {
uint8_t major = 0;
uint8_t minor = 0;
int error = scd30.getFirmwareVersion(&major, &minor);
if (!error) {
float firmware = major + ((float)minor / 100);
ResponseCmndFloat(firmware, 2);
}
};
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
void CmndScd30Interval(void) {
uint16_t value = 0;
if (XdrvMailbox.data_len > 0) {
value = XdrvMailbox.payload;
int error = scd30.setMeasurementInterval(value);
if (!error) {
Scd30.interval = value;
2019-03-10 01:44:42 +00:00
}
}
2022-09-04 15:14:32 +01:00
scd30.getMeasurementInterval(&value);
ResponseCmndNumber(value);
};
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
void CmndScd30Pressure(void) {
uint16_t value = 0;
if (XdrvMailbox.data_len > 0) {
value = XdrvMailbox.payload;
scd30.setAmbientPressure(value);
} else {
scd30.getAmbientPressure(&value);
}
ResponseCmndNumber(value);
};
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
void CmndScd30TempOffset(void) {
uint16_t value = 0;
if (XdrvMailbox.data_len > 0) {
value = XdrvMailbox.payload;
scd30.setTemperatureOffset(value);
} else {
2022-09-04 15:14:32 +01:00
scd30.getTemperatureOffset(&value);
2019-03-10 01:44:42 +00:00
}
2022-09-04 15:14:32 +01:00
ResponseCmndNumber(value);
};
/********************************************************************************************/
2019-03-10 01:44:42 +00:00
2022-09-04 15:14:32 +01:00
void Scd30Show(bool json) {
if (Scd30.data_valid) {
float t = ConvertTemp(Scd30.temperature);
float h = ConvertHumidity(Scd30.humidity);
2019-11-11 17:36:13 +00:00
2019-03-10 01:44:42 +00:00
if (json) {
2022-09-04 15:14:32 +01:00
ResponseAppend_P(PSTR(",\"SCD30\":{\"" D_JSON_CO2 "\":%d,\"" D_JSON_ECO2 "\":%d,"), Scd30.co2, Scd30.co2e_avg);
ResponseAppendTHD(t, h);
ResponseJsonEnd();
2019-03-10 01:44:42 +00:00
#ifdef USE_DOMOTICZ
2020-10-29 12:37:09 +00:00
if (0 == TasmotaGlobal.tele_period) {
2022-09-04 15:14:32 +01:00
DomoticzSensor(DZ_AIRQUALITY, Scd30.co2);
DomoticzTempHumPressureSensor(t, h);
}
2019-03-10 01:44:42 +00:00
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
2022-09-04 15:14:32 +01:00
WSContentSend_PD(HTTP_SNS_CO2EAVG, "SCD30", Scd30.co2e_avg);
WSContentSend_PD(HTTP_SNS_CO2, "SCD30", Scd30.co2);
WSContentSend_THD("SCD30", t, h);
2019-03-10 01:44:42 +00:00
#endif // USE_WEBSERVER
}
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
2022-09-04 15:14:32 +01:00
bool Xsns42(byte function) {
2019-11-04 09:38:05 +00:00
if (!I2cEnabled(XI2C_29)) { return false; }
2019-03-10 01:44:42 +00:00
bool result = false;
// https://github.com/arendst/Tasmota/issues/15438 and datasheet (The boot-up time is < 2 s.)
/*
2019-11-11 17:36:13 +00:00
if (FUNC_INIT == function) {
Scd30Detect();
}
*/
if (!Scd30.init_once && (FUNC_EVERY_SECOND == function) && (TasmotaGlobal.uptime > 3)) {
2022-09-04 15:14:32 +01:00
Scd30.init_once = true;
Scd30Detect();
}
2022-09-04 15:14:32 +01:00
else if (Scd30.found) {
2019-11-11 17:36:13 +00:00
switch (function) {
case FUNC_EVERY_SECOND:
Scd30Update();
break;
case FUNC_COMMAND:
2022-09-04 15:14:32 +01:00
result = DecodeCommand(kScd30Commands, kScd30Command);
2019-11-11 17:36:13 +00:00
break;
case FUNC_JSON_APPEND:
Scd30Show(1);
break;
2022-09-04 15:14:32 +01:00
#ifdef USE_WEBSERVER
2019-11-11 17:36:13 +00:00
case FUNC_WEB_SENSOR:
Scd30Show(0);
break;
2022-09-04 15:14:32 +01:00
#endif // USE_WEBSERVER
2019-11-11 17:36:13 +00:00
}
2019-03-10 01:44:42 +00:00
}
return result;
}
#endif // USE_SCD30
#endif // USE_I2C