diff --git a/tasmota/xsns_31_ccs811.ino b/tasmota/xsns_31_ccs811.ino index ef553cb13..e9729d174 100644 --- a/tasmota/xsns_31_ccs811.ino +++ b/tasmota/xsns_31_ccs811.ino @@ -30,6 +30,8 @@ #define XSNS_31 31 #define XI2C_24 24 // See I2CDEVICES.md +#define EVERYNSECONDS 5 + #include "Adafruit_CCS811.h" Adafruit_CCS811 ccs; @@ -41,40 +43,37 @@ uint8_t tcnt = 0; uint8_t ecnt = 0; /********************************************************************************************/ -#define EVERYNSECONDS 5 -void CCS811Update(void) // Perform every n second +void CCS811Detect(void) { if (I2cActive(CCS811_ADDRESS)) { return; } + if (!ccs.begin(CCS811_ADDRESS)) { + CCS811_type = 1; + I2cSetActiveFound(CCS811_ADDRESS, "CCS811"); + } +} + +void CCS811Update(void) // Perform every n second +{ tcnt++; if (tcnt >= EVERYNSECONDS) { tcnt = 0; CCS811_ready = 0; - if (!CCS811_type) { - sint8_t res = ccs.begin(CCS811_ADDRESS); - if (!res) { - CCS811_type = 1; - I2cSetActiveFound(CCS811_ADDRESS, "CCS811"); - } else { - //AddLog_P2(LOG_LEVEL_DEBUG, "CCS811 init failed: %d",res); + if (ccs.available()) { + if (!ccs.readData()){ + TVOC = ccs.getTVOC(); + eCO2 = ccs.geteCO2(); + CCS811_ready = 1; + if (global_update && global_humidity>0 && global_temperature!=9999) { ccs.setEnvironmentalData((uint8_t)global_humidity, global_temperature); } + ecnt = 0; } } else { - if (ccs.available()) { - if (!ccs.readData()){ - TVOC = ccs.getTVOC(); - eCO2 = ccs.geteCO2(); - CCS811_ready = 1; - if (global_update && global_humidity>0 && global_temperature!=9999) { ccs.setEnvironmentalData((uint8_t)global_humidity, global_temperature); } - ecnt = 0; - } - } else { - // failed, count up - ecnt++; - if (ecnt > 6) { - // after 30 seconds, restart - ccs.begin(CCS811_ADDRESS); - } + // failed, count up + ecnt++; + if (ecnt > 6) { + // after 30 seconds, restart + ccs.begin(CCS811_ADDRESS); } } } @@ -110,18 +109,23 @@ bool Xsns31(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - CCS811Update(); - break; - case FUNC_JSON_APPEND: - CCS811Show(1); - break; + if (FUNC_INIT == function) { + CCS811Detect(); + } + else if (CCS811_type) { + switch (function) { + case FUNC_EVERY_SECOND: + CCS811Update(); + break; + case FUNC_JSON_APPEND: + CCS811Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - CCS811Show(0); - break; + case FUNC_WEB_SENSOR: + CCS811Show(0); + break; #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_32_mpu6050.ino b/tasmota/xsns_32_mpu6050.ino index 61395d5d6..48cd201bc 100644 --- a/tasmota/xsns_32_mpu6050.ino +++ b/tasmota/xsns_32_mpu6050.ino @@ -67,8 +67,6 @@ MPU6050 mpu6050; void MPU_6050PerformReading(void) { - if (!MPU_6050_found) { return; } - #ifdef USE_MPU6050_DMP mpu6050.resetFIFO(); // with a default dampling rate of 200Hz, we create a delay of approx. 5ms with a complete read cycle MPU6050_dmp.fifoCount = mpu6050.getFIFOCount(); @@ -118,8 +116,6 @@ void MPU_6050SetAccelOffsets(int x, int y, int z) void MPU_6050Detect(void) { - if (MPU_6050_found) { return; } - for (uint32_t i = 0; i < sizeof(MPU_6050_addresses); i++) { MPU_6050_address = MPU_6050_addresses[i]; @@ -144,8 +140,7 @@ void MPU_6050Detect(void) Settings.flag2.axis_resolution = 2; // Need to be services by command Sensor32 } - if (MPU_6050_found) - { + if (MPU_6050_found) { I2cSetActiveFound(MPU_6050_address, D_SENSOR_MPU6050); } } @@ -169,8 +164,6 @@ const char HTTP_SNS_AXIS[] PROGMEM = void MPU_6050Show(bool json) { - if (!MPU_6050_found) { return; } - MPU_6050PerformReading(); double tempConv = (MPU_6050_temperature / 340.0 + 35.53); @@ -225,24 +218,26 @@ bool Xsns32(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - if (tele_period == Settings.tele_period -3) { - MPU_6050PerformReading(); - } - break; - case FUNC_JSON_APPEND: - MPU_6050Show(1); - break; + if (FUNC_INIT == function) { + MPU_6050Detect(); + } + else if (MPU_6050_found) { + switch (function) { + case FUNC_EVERY_SECOND: + if (tele_period == Settings.tele_period -3) { + MPU_6050PerformReading(); + } + break; + case FUNC_JSON_APPEND: + MPU_6050Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - MPU_6050Show(0); - MPU_6050PerformReading(); - break; + case FUNC_WEB_SENSOR: + MPU_6050Show(0); + MPU_6050PerformReading(); + break; #endif // USE_WEBSERVER - case FUNC_INIT: - MPU_6050Detect(); - break; + } } return result; } diff --git a/tasmota/xsns_33_ds3231.ino b/tasmota/xsns_33_ds3231.ino index 9e5062acf..264c53e9e 100644 --- a/tasmota/xsns_33_ds3231.ino +++ b/tasmota/xsns_33_ds3231.ino @@ -72,7 +72,7 @@ bool DS3231chipDetected = false; ----------------------------------------------------------------------*/ void DS3231Detect(void) { - if (DS3231chipDetected || I2cActive(USE_RTC_ADDR)) { return; } + if (I2cActive(USE_RTC_ADDR)) { return; } if (I2cValidRead(USE_RTC_ADDR, RTC_STATUS, 1)) { I2cSetActiveFound(USE_RTC_ADDR, "DS3231"); @@ -129,8 +129,6 @@ void SetDS3231Time (uint32_t epoch_time) { void DS3231EverySecond(void) { - if (!DS3231chipDetected) { return; } - TIME_T tmpTime; if (!ds3231ReadStatus && Rtc.utc_time < START_VALID_TIME ) { // We still did not sync with NTP (time not valid) , so, read time from DS3231 ntp_force_sync = true; //force to sync with ntp @@ -170,13 +168,15 @@ bool Xsns33(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - DS3231EverySecond(); - break; - case FUNC_INIT: - DS3231Detect(); - break; + if (FUNC_INIT == function) { + DS3231Detect(); + } + else if (DS3231chipDetected) { + switch (function) { + case FUNC_EVERY_SECOND: + DS3231EverySecond(); + break; + } } return result; } diff --git a/tasmota/xsns_41_max44009.ino b/tasmota/xsns_41_max44009.ino index 7de6cf4d3..2cdf6b78d 100644 --- a/tasmota/xsns_41_max44009.ino +++ b/tasmota/xsns_41_max44009.ino @@ -66,8 +66,6 @@ bool Max4409Read_lum(void) void Max4409Detect(void) { - if (max44009_found) { return; } - uint8_t buffer1; uint8_t buffer2; for (uint32_t i = 0; 0 != max44009_addresses[i]; i++) { @@ -101,9 +99,7 @@ void Max4409Detect(void) void Max4409EverySecond(void) { - if (max44009_found) { - Max4409Read_lum(); - } + Max4409Read_lum(); } void Max4409Show(bool json) @@ -150,21 +146,23 @@ bool Xsns41(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - Max4409EverySecond(); - break; - case FUNC_JSON_APPEND: - Max4409Show(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Max4409Show(0); - break; -#endif // USE_WEBSERVER - case FUNC_INIT: - Max4409Detect(); - break; + if (FUNC_INIT == function) { + Max4409Detect(); + } + else if (max44009_found) { + switch (function) { + case FUNC_EVERY_SECOND: + Max4409EverySecond(); + break; + case FUNC_JSON_APPEND: + Max4409Show(1); + break; + #ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + Max4409Show(0); + break; + #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_42_scd30.ino b/tasmota/xsns_42_scd30.ino index 5c3e741f1..d3bb5cbef 100644 --- a/tasmota/xsns_42_scd30.ino +++ b/tasmota/xsns_42_scd30.ino @@ -20,8 +20,10 @@ #ifdef USE_I2C #ifdef USE_SCD30 -#define XSNS_42 42 -#define XI2C_29 29 // See I2CDEVICES.md +#define XSNS_42 42 +#define XI2C_29 29 // See I2CDEVICES.md + +#define SCD30_ADDRESS 0x61 #define SCD30_MAX_MISSED_READS 3 #define SCD30_STATE_NO_ERROR 0 @@ -55,8 +57,6 @@ enum SCD30_Commands { // commands useable in console or rules CMND_SCD30_TEMPOFFSET }; - - FrogmoreScd30 scd30; bool scd30Found = false; @@ -75,223 +75,149 @@ uint16_t scd30_CO2EAvg = 0; float scd30_Humid = 0.0; float scd30_Temp = 0.0; -bool Scd30Init() +void Scd30Detect(void) { - int error; - bool i2c_flg = ((pin[GPIO_I2C_SCL] < 99) && (pin[GPIO_I2C_SDA] < 99)); - if (i2c_flg) - { - uint8_t major = 0; - uint8_t minor = 0; - uint16_t interval_sec; - scd30.begin(); - error = scd30.getFirmwareVersion(&major, &minor); - if (error) - { -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: did not find an SCD30: 0x%lX", error); - AddLog(LOG_LEVEL_DEBUG); -#endif - return false; - } - else - { -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: found an SCD30: FW v%d.%d", major, minor); - AddLog(LOG_LEVEL_INFO); -#endif - } + if (I2cActive(SCD30_ADDRESS)) { return; } - error = scd30.getMeasurementInterval(&scd30Interval_sec); - if (error) - { -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: error getMeasurementInterval: 0x%lX", error); - AddLog(LOG_LEVEL_ERROR); -#endif - return false; - } + scd30.begin(); - error = scd30.beginMeasuring(); - if (error) - { -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "Error: Scd30BeginMeasuring: 0x%lX", error); - AddLog(LOG_LEVEL_ERROR); -#endif - return false; - } + uint8_t major = 0; + uint8_t minor = 0; + if (scd30.getFirmwareVersion(&major, &minor)) { return; } + uint16_t interval_sec; + if (scd30.getMeasurementInterval(&scd30Interval_sec)) { return; } + if (scd30.beginMeasuring()) { return; } - return true; - } + I2cSetActiveFound(SCD30_ADDRESS, "SCD30"); + scd30Found = true; + + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SCD: FW v%d.%d"), major, minor); } // gets data from the sensor every 3 seconds or so to give the sensor time to gather new data -int Scd30Update() +void Scd30Update(void) { - int error = 0; - int16_t delta = 0; scd30Loop_count++; - - if (!scd30Found) - { - scd30Found = Scd30Init(); -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "Scd30Update: found: %d ", scd30Found); - AddLog(LOG_LEVEL_INFO); -#endif - if (!scd30Found) - { -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "Scd30Update: found: %d ", scd30Found); - AddLog(LOG_LEVEL_INFO); -#endif - return (ERROR_SCD30_NOT_FOUND_ERROR); - } - } - else - { - if (scd30Loop_count > (scd30Interval_sec - 1)) - { - switch (scd30ErrorState) - { - case SCD30_STATE_NO_ERROR: - { - error = scd30.readMeasurement(&scd30_CO2, &scd30_CO2EAvg, &scd30_Temp, &scd30_Humid); - switch (error) - { - case ERROR_SCD30_NO_ERROR: - scd30Loop_count = 0; - scd30IsDataValid = true; - scd30GoodMeas_count++; - break; - - case ERROR_SCD30_NO_DATA: - scd30DataNotAvailable_count++; - break; - - case ERROR_SCD30_CRC_ERROR: - scd30ErrorState = SCD30_STATE_ERROR_DATA_CRC; - scd30CrcError_count++; -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: CRC error, CRC error: %ld, CO2 zero: %ld, good: %ld, no data: %ld, sc30_reset: %ld, i2c_reset: %ld", scd30CrcError_count, scd30Co2Zero_count, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); - AddLog(LOG_LEVEL_ERROR); -#endif - break; - - case ERROR_SCD30_CO2_ZERO: - scd30Co2Zero_count++; -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: CO2 zero, CRC error: %ld, CO2 zero: %ld, good: %ld, no data: %ld, sc30_reset: %ld, i2c_reset: %ld", scd30CrcError_count, scd30Co2Zero_count, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); - AddLog(LOG_LEVEL_ERROR); -#endif - break; - - default: - { - scd30ErrorState = SCD30_STATE_ERROR_READ_MEAS; -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: Update: ReadMeasurement error: 0x%lX, counter: %ld", error, scd30Loop_count); - AddLog(LOG_LEVEL_ERROR); -#endif - return (error); - } + if (scd30Loop_count > (scd30Interval_sec - 1)) { + int error = 0; + switch (scd30ErrorState) { + case SCD30_STATE_NO_ERROR: { + error = scd30.readMeasurement(&scd30_CO2, &scd30_CO2EAvg, &scd30_Temp, &scd30_Humid); + switch (error) { + case ERROR_SCD30_NO_ERROR: + scd30Loop_count = 0; + scd30IsDataValid = true; + scd30GoodMeas_count++; break; - } - } - break; - case SCD30_STATE_ERROR_DATA_CRC: - { - //scd30IsDataValid = false; + case ERROR_SCD30_NO_DATA: + scd30DataNotAvailable_count++; + break; + + case ERROR_SCD30_CRC_ERROR: + scd30ErrorState = SCD30_STATE_ERROR_DATA_CRC; + scd30CrcError_count++; #ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld", scd30ErrorState, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); - AddLog(LOG_LEVEL_ERROR); - snprintf_P(log_data, sizeof(log_data), "SCD30: got CRC error, try again, counter: %ld", scd30Loop_count); + snprintf_P(log_data, sizeof(log_data), "SCD30: CRC error, CRC error: %ld, CO2 zero: %ld, good: %ld, no data: %ld, sc30_reset: %ld, i2c_reset: %ld", scd30CrcError_count, scd30Co2Zero_count, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); + AddLog(LOG_LEVEL_ERROR); +#endif + break; + + case ERROR_SCD30_CO2_ZERO: + scd30Co2Zero_count++; +#ifdef SCD30_DEBUG + snprintf_P(log_data, sizeof(log_data), "SCD30: CO2 zero, CRC error: %ld, CO2 zero: %ld, good: %ld, no data: %ld, sc30_reset: %ld, i2c_reset: %ld", scd30CrcError_count, scd30Co2Zero_count, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); + AddLog(LOG_LEVEL_ERROR); +#endif + break; + + default: { + scd30ErrorState = SCD30_STATE_ERROR_READ_MEAS; +#ifdef SCD30_DEBUG + snprintf_P(log_data, sizeof(log_data), "SCD30: Update: ReadMeasurement error: 0x%lX, counter: %ld", error, scd30Loop_count); + AddLog(LOG_LEVEL_ERROR); +#endif + return; + } + break; + } + } + break; + + case SCD30_STATE_ERROR_DATA_CRC: { + //scd30IsDataValid = false; +#ifdef SCD30_DEBUG + snprintf_P(log_data, sizeof(log_data), "SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld", scd30ErrorState, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); + AddLog(LOG_LEVEL_ERROR); + snprintf_P(log_data, sizeof(log_data), "SCD30: got CRC error, try again, counter: %ld", scd30Loop_count); + AddLog(LOG_LEVEL_ERROR); +#endif + scd30ErrorState = ERROR_SCD30_NO_ERROR; + } + break; + + case SCD30_STATE_ERROR_READ_MEAS: { + //scd30IsDataValid = false; +#ifdef SCD30_DEBUG + snprintf_P(log_data, sizeof(log_data), "SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld", scd30ErrorState, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); + AddLog(LOG_LEVEL_ERROR); + snprintf_P(log_data, sizeof(log_data), "SCD30: not answering, sending soft reset, counter: %ld", scd30Loop_count); + AddLog(LOG_LEVEL_ERROR); +#endif + scd30Reset_count++; + error = scd30.softReset(); + if (error) { +#ifdef SCD30_DEBUG + snprintf_P(log_data, sizeof(log_data), "SCD30: resetting got error: 0x%lX", error); AddLog(LOG_LEVEL_ERROR); #endif + error >>= 8; + if (error == 4) { + scd30ErrorState = SCD30_STATE_ERROR_SOFT_RESET; + } else { + scd30ErrorState = SCD30_STATE_ERROR_UNKNOWN; + } + } else { scd30ErrorState = ERROR_SCD30_NO_ERROR; } - break; + } + break; - case SCD30_STATE_ERROR_READ_MEAS: - { - //scd30IsDataValid = false; + case SCD30_STATE_ERROR_SOFT_RESET: { + //scd30IsDataValid = false; #ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld", scd30ErrorState, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); - AddLog(LOG_LEVEL_ERROR); - snprintf_P(log_data, sizeof(log_data), "SCD30: not answering, sending soft reset, counter: %ld", scd30Loop_count); + snprintf_P(log_data, sizeof(log_data), "SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld", scd30ErrorState, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); + AddLog(LOG_LEVEL_ERROR); + snprintf_P(log_data, sizeof(log_data), "SCD30: clearing i2c bus"); + AddLog(LOG_LEVEL_ERROR); +#endif + i2cReset_count++; + error = scd30.clearI2CBus(); + if (error) { + scd30ErrorState = SCD30_STATE_ERROR_I2C_RESET; +#ifdef SCD30_DEBUG + snprintf_P(log_data, sizeof(log_data), "SCD30: error clearing i2c bus: 0x%lX", error); AddLog(LOG_LEVEL_ERROR); #endif - scd30Reset_count++; - error = scd30.softReset(); - if (error) - { -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: resetting got error: 0x%lX", error); - AddLog(LOG_LEVEL_ERROR); -#endif - error >>= 8; - if (error == 4) - { - scd30ErrorState = SCD30_STATE_ERROR_SOFT_RESET; - } - else - { - scd30ErrorState = SCD30_STATE_ERROR_UNKNOWN; - } - } - else - { - scd30ErrorState = ERROR_SCD30_NO_ERROR; - } - } - break; - - case SCD30_STATE_ERROR_SOFT_RESET: - { - //scd30IsDataValid = false; -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld", scd30ErrorState, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count); - AddLog(LOG_LEVEL_ERROR); - snprintf_P(log_data, sizeof(log_data), "SCD30: clearing i2c bus"); - AddLog(LOG_LEVEL_ERROR); -#endif - i2cReset_count++; - error = scd30.clearI2CBus(); - if (error) - { - scd30ErrorState = SCD30_STATE_ERROR_I2C_RESET; -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: error clearing i2c bus: 0x%lX", error); - AddLog(LOG_LEVEL_ERROR); -#endif - } - else - { - scd30ErrorState = ERROR_SCD30_NO_ERROR; - } - } - break; - - default: - { - //scd30IsDataValid = false; -#ifdef SCD30_DEBUG - snprintf_P(log_data, sizeof(log_data), "SCD30: unknown error state: 0x%lX", scd30ErrorState); - AddLog(LOG_LEVEL_ERROR); -#endif - scd30ErrorState = SCD30_STATE_ERROR_SOFT_RESET; // try again + } else { + scd30ErrorState = ERROR_SCD30_NO_ERROR; } } + break; - if (scd30Loop_count > (SCD30_MAX_MISSED_READS * scd30Interval_sec)) - { - scd30IsDataValid = false; + default: { + //scd30IsDataValid = false; +#ifdef SCD30_DEBUG + AddLog_P2(LOG_LEVEL_ERROR, PSTR("SCD30: unknown error state: 0x%lX"), scd30ErrorState); +#endif + scd30ErrorState = SCD30_STATE_ERROR_SOFT_RESET; // try again } } + + if (scd30Loop_count > (SCD30_MAX_MISSED_READS * scd30Interval_sec)) { + scd30IsDataValid = false; + } } - return (ERROR_SCD30_NO_ERROR); } @@ -370,8 +296,9 @@ int Scd30SetCommand(int command_code, uint16_t value) break; } } + /*********************************************************************************************\ - * Command Sensor92 + * Command Sensor42 \*********************************************************************************************/ bool Scd30CommandSensor() @@ -438,13 +365,13 @@ bool Scd30CommandSensor() void Scd30Show(bool json) { - char humidity[10]; - char temperature[10]; - - if (scd30Found && scd30IsDataValid) + if (scd30IsDataValid) { + char humidity[10]; dtostrfd(ConvertHumidity(scd30_Humid), Settings.flag2.humidity_resolution, humidity); + char temperature[10]; dtostrfd(ConvertTemp(scd30_Temp), Settings.flag2.temperature_resolution, temperature); + if (json) { //ResponseAppend_P(PSTR(",\"SCD30\":{\"" D_JSON_CO2 "\":%d,\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_HUMIDITY "\":%s}"), scd30_CO2, temperature, humidity); ResponseAppend_P(PSTR(",\"SCD30\":{\"" D_JSON_CO2 "\":%d,\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_HUMIDITY "\":%s}"), @@ -477,21 +404,26 @@ bool Xsns42(byte function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - Scd30Update(); - break; - case FUNC_COMMAND: - result = Scd30CommandSensor(); - break; - case FUNC_JSON_APPEND: - Scd30Show(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Scd30Show(0); - break; -#endif // USE_WEBSERVER + if (FUNC_INIT == function) { + Scd30Detect(); + } + else if (scd30Found) { + switch (function) { + case FUNC_EVERY_SECOND: + Scd30Update(); + break; + case FUNC_COMMAND: + result = Scd30CommandSensor(); + break; + case FUNC_JSON_APPEND: + Scd30Show(1); + break; + #ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + Scd30Show(0); + break; + #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_44_sps30.ino b/tasmota/xsns_44_sps30.ino index bb4fcebc9..daa4dce98 100644 --- a/tasmota/xsns_44_sps30.ino +++ b/tasmota/xsns_44_sps30.ino @@ -130,6 +130,7 @@ unsigned char cmdb[6]; void SPS30_Detect(void) { if (!I2cSetDevice(SPS30_ADDR)) { return; } + I2cSetActiveFound(SPS30_ADDR, "SPS30"); uint8_t dcode[32]; sps30_get_data(SPS_CMD_GET_SERIAL,dcode,sizeof(dcode)); @@ -155,11 +156,8 @@ const char HTTP_SNS_SPS30_c[] PROGMEM ="{s}SPS30 " "TYPSIZ" "{m}%s " "um" "{e}"; //uint8_t sps30_inuse_hours; void SPS30_Every_Second() { - - if (!sps30_ready) return; if (!sps30_running) return; - if (uptime%10==0) { uint8_t vars[sizeof(float)*10]; sps30_get_data(SPS_CMD_READ_MEASUREMENT,vars,sizeof(vars)); @@ -192,16 +190,11 @@ void SPS30_Every_Second() { } -void SPS30_Show(bool json) { +void SPS30_Show(bool json) +{ + if (!sps30_running) { return; } + char str[64]; - if (!sps30_ready) { - return; - } - - if (!sps30_running) { - return; - } - if (json) { dtostrfd(sps30_result.PM1_0,PMDP,str); ResponseAppend_P(PSTR(",\"SPS30\":{\"" "PM1_0" "\":%s"), str); @@ -248,16 +241,17 @@ void SPS30_Show(bool json) { WSContentSend_PD(HTTP_SNS_SPS30_c,str); #endif } - } -void CmdClean(void) { +void CmdClean(void) +{ sps30_cmd(SPS_CMD_CLEAN); ResponseTime_P(PSTR(",\"SPS30\":{\"CFAN\":\"true\"}}")); MqttPublishTeleSensor(); } -bool SPS30_cmd(void) { +bool SPS30_cmd(void) +{ bool serviced = true; if (XdrvMailbox.data_len > 0) { char *cp=XdrvMailbox.data; @@ -280,33 +274,34 @@ bool SPS30_cmd(void) { * Interface \*********************************************************************************************/ - bool Xsns44(byte function) { if (!I2cEnabled(XI2C_30)) { return false; } bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - SPS30_Every_Second(); - break; - case FUNC_JSON_APPEND: - SPS30_Show(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - SPS30_Show(0); - break; -#endif // USE_WEBSERVER - case FUNC_COMMAND_SENSOR: - if (XSNS_44 == XdrvMailbox.index) { - result = SPS30_cmd(); - } - break; - case FUNC_INIT: - SPS30_Detect(); - break; + if (FUNC_INIT == function) { + SPS30_Detect(); + } + else if (sps30_ready) { + switch (function) { + case FUNC_EVERY_SECOND: + SPS30_Every_Second(); + break; + case FUNC_JSON_APPEND: + SPS30_Show(1); + break; + #ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + SPS30_Show(0); + break; + #endif // USE_WEBSERVER + case FUNC_COMMAND_SENSOR: + if (XSNS_44 == XdrvMailbox.index) { + result = SPS30_cmd(); + } + break; + } } return result; } diff --git a/tasmota/xsns_45_vl53l0x.ino b/tasmota/xsns_45_vl53l0x.ino index b2e00f3c8..ff4e8f260 100644 --- a/tasmota/xsns_45_vl53l0x.ino +++ b/tasmota/xsns_45_vl53l0x.ino @@ -36,8 +36,6 @@ uint8_t Vl53l0_index; void Vl53l0Detect(void) { - if (vl53l0x_ready) { return; } - if (!I2cSetDevice(0x29)) { return; } if (!sensor.init()) { return; } @@ -65,8 +63,6 @@ const char HTTP_SNS_VL53L0X[] PROGMEM = void Vl53l0Every_250MSecond(void) { - if (!vl53l0x_ready) { return; } - uint16_t tbuff[5],tmp; uint8_t flag; @@ -104,8 +100,6 @@ void Vl53l0Every_250MSecond(void) void Vl53l0Show(boolean json) { - if (!vl53l0x_ready) { return; } - if (json) { ResponseAppend_P(PSTR(",\"VL53L0X\":{\"" D_JSON_DISTANCE "\":%d}"), vl53l0x_distance); #ifdef USE_WEBSERVER @@ -125,21 +119,23 @@ bool Xsns45(byte function) bool result = false; - switch (function) { - case FUNC_EVERY_250_MSECOND: - Vl53l0Every_250MSecond(); - break; - case FUNC_JSON_APPEND: - Vl53l0Show(1); - break; + if (FUNC_INIT == function) { + Vl53l0Detect(); + } + else if (vl53l0x_ready) { + switch (function) { + case FUNC_EVERY_250_MSECOND: + Vl53l0Every_250MSecond(); + break; + case FUNC_JSON_APPEND: + Vl53l0Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Vl53l0Show(0); - break; + case FUNC_WEB_SENSOR: + Vl53l0Show(0); + break; #endif // USE_WEBSERVER - case FUNC_INIT: - Vl53l0Detect(); - break; + } } return result; }