mirror of
https://github.com/esphome/esphome.git
synced 2026-05-25 10:26:10 +08:00
Sgp40 (#1513)
* Start of SGP40 dev * Clean up * Initial Commit * VOC is working * Fixed up sensor config * Lint Fixes Added in save/restore baseline Noted original repo in header * Lint Fixes Added to test * Lint Fixes * Added additional check on restoring * Removed double check * Changed defines to static const double * Changed defines to const Do not send voc index until sensor stabilizes * Fixed sensor stabilization message * Fixup according to PR * samples_read increment fix * Fixed missing device class * Choose a SENSOR device class * Moved some sensors for tests Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
This commit is contained in:
@@ -78,6 +78,7 @@ esphome/components/rf_bridge/* @jesserockz
|
||||
esphome/components/rtttl/* @glmnet
|
||||
esphome/components/script/* @esphome/core
|
||||
esphome/components/sensor/* @esphome/core
|
||||
esphome/components/sgp40/* @SenexCrenshaw
|
||||
esphome/components/shutdown/* @esphome/core
|
||||
esphome/components/sim800l/* @glmnet
|
||||
esphome/components/spi/* @esphome/core
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,147 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
namespace esphome {
|
||||
namespace sgp40 {
|
||||
|
||||
/* The VOC code were originally created by
|
||||
* https://github.com/Sensirion/embedded-sgp
|
||||
* The fixed point arithmetic parts of this code were originally created by
|
||||
* https://github.com/PetteriAimonen/libfixmath
|
||||
*/
|
||||
|
||||
using fix16_t = int32_t;
|
||||
|
||||
#define F16(x) ((fix16_t)(((x) >= 0) ? ((x) *65536.0 + 0.5) : ((x) *65536.0 - 0.5)))
|
||||
|
||||
static const float VOC_ALGORITHM_SAMPLING_INTERVAL(1.);
|
||||
static const float VOC_ALGORITHM_INITIAL_BLACKOUT(45.);
|
||||
static const float VOC_ALGORITHM_VOC_INDEX_GAIN(230.);
|
||||
static const float VOC_ALGORITHM_SRAW_STD_INITIAL(50.);
|
||||
static const float VOC_ALGORITHM_SRAW_STD_BONUS(220.);
|
||||
static const float VOC_ALGORITHM_TAU_MEAN_VARIANCE_HOURS(12.);
|
||||
static const float VOC_ALGORITHM_TAU_INITIAL_MEAN(20.);
|
||||
static const float VOC_ALGORITHM_INIT_DURATION_MEAN((3600. * 0.75));
|
||||
static const float VOC_ALGORITHM_INIT_TRANSITION_MEAN(0.01);
|
||||
static const float VOC_ALGORITHM_TAU_INITIAL_VARIANCE(2500.);
|
||||
static const float VOC_ALGORITHM_INIT_DURATION_VARIANCE((3600. * 1.45));
|
||||
static const float VOC_ALGORITHM_INIT_TRANSITION_VARIANCE(0.01);
|
||||
static const float VOC_ALGORITHM_GATING_THRESHOLD(340.);
|
||||
static const float VOC_ALGORITHM_GATING_THRESHOLD_INITIAL(510.);
|
||||
static const float VOC_ALGORITHM_GATING_THRESHOLD_TRANSITION(0.09);
|
||||
static const float VOC_ALGORITHM_GATING_MAX_DURATION_MINUTES((60. * 3.));
|
||||
static const float VOC_ALGORITHM_GATING_MAX_RATIO(0.3);
|
||||
static const float VOC_ALGORITHM_SIGMOID_L(500.);
|
||||
static const float VOC_ALGORITHM_SIGMOID_K(-0.0065);
|
||||
static const float VOC_ALGORITHM_SIGMOID_X0(213.);
|
||||
static const float VOC_ALGORITHM_VOC_INDEX_OFFSET_DEFAULT(100.);
|
||||
static const float VOC_ALGORITHM_LP_TAU_FAST(20.0);
|
||||
static const float VOC_ALGORITHM_LP_TAU_SLOW(500.0);
|
||||
static const float VOC_ALGORITHM_LP_ALPHA(-0.2);
|
||||
static const float VOC_ALGORITHM_PERSISTENCE_UPTIME_GAMMA((3. * 3600.));
|
||||
static const float VOC_ALGORITHM_MEAN_VARIANCE_ESTIMATOR_GAMMA_SCALING(64.);
|
||||
static const float VOC_ALGORITHM_MEAN_VARIANCE_ESTIMATOR_FI_X16_MAX(32767.);
|
||||
|
||||
/**
|
||||
* Struct to hold all the states of the VOC algorithm.
|
||||
*/
|
||||
struct VocAlgorithmParams {
|
||||
fix16_t mVoc_Index_Offset;
|
||||
fix16_t mTau_Mean_Variance_Hours;
|
||||
fix16_t mGating_Max_Duration_Minutes;
|
||||
fix16_t mSraw_Std_Initial;
|
||||
fix16_t mUptime;
|
||||
fix16_t mSraw;
|
||||
fix16_t mVoc_Index;
|
||||
fix16_t m_Mean_Variance_Estimator__Gating_Max_Duration_Minutes;
|
||||
bool m_Mean_Variance_Estimator___Initialized;
|
||||
fix16_t m_Mean_Variance_Estimator___Mean;
|
||||
fix16_t m_Mean_Variance_Estimator___Sraw_Offset;
|
||||
fix16_t m_Mean_Variance_Estimator___Std;
|
||||
fix16_t m_Mean_Variance_Estimator___Gamma;
|
||||
fix16_t m_Mean_Variance_Estimator___Gamma_Initial_Mean;
|
||||
fix16_t m_Mean_Variance_Estimator___Gamma_Initial_Variance;
|
||||
fix16_t m_Mean_Variance_Estimator__Gamma_Mean;
|
||||
fix16_t m_Mean_Variance_Estimator__Gamma_Variance;
|
||||
fix16_t m_Mean_Variance_Estimator___Uptime_Gamma;
|
||||
fix16_t m_Mean_Variance_Estimator___Uptime_Gating;
|
||||
fix16_t m_Mean_Variance_Estimator___Gating_Duration_Minutes;
|
||||
fix16_t m_Mean_Variance_Estimator___Sigmoid__L;
|
||||
fix16_t m_Mean_Variance_Estimator___Sigmoid__K;
|
||||
fix16_t m_Mean_Variance_Estimator___Sigmoid__X0;
|
||||
fix16_t m_Mox_Model__Sraw_Std;
|
||||
fix16_t m_Mox_Model__Sraw_Mean;
|
||||
fix16_t m_Sigmoid_Scaled__Offset;
|
||||
fix16_t m_Adaptive_Lowpass__A1;
|
||||
fix16_t m_Adaptive_Lowpass__A2;
|
||||
bool m_Adaptive_Lowpass___Initialized;
|
||||
fix16_t m_Adaptive_Lowpass___X1;
|
||||
fix16_t m_Adaptive_Lowpass___X2;
|
||||
fix16_t m_Adaptive_Lowpass___X3;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the VOC algorithm parameters. Call this once at the beginning or
|
||||
* whenever the sensor stopped measurements.
|
||||
* @param params Pointer to the VocAlgorithmParams struct
|
||||
*/
|
||||
void voc_algorithm_init(VocAlgorithmParams *params);
|
||||
|
||||
/**
|
||||
* Get current algorithm states. Retrieved values can be used in
|
||||
* voc_algorithm_set_states() to resume operation after a short interruption,
|
||||
* skipping initial learning phase. This feature can only be used after at least
|
||||
* 3 hours of continuous operation.
|
||||
* @param params Pointer to the VocAlgorithmParams struct
|
||||
* @param state0 State0 to be stored
|
||||
* @param state1 State1 to be stored
|
||||
*/
|
||||
void voc_algorithm_get_states(VocAlgorithmParams *params, int32_t *state0, int32_t *state1);
|
||||
|
||||
/**
|
||||
* Set previously retrieved algorithm states to resume operation after a short
|
||||
* interruption, skipping initial learning phase. This feature should not be
|
||||
* used after inerruptions of more than 10 minutes. Call this once after
|
||||
* voc_algorithm_init() and the optional voc_algorithm_set_tuning_parameters(), if
|
||||
* desired. Otherwise, the algorithm will start with initial learning phase.
|
||||
* @param params Pointer to the VocAlgorithmParams struct
|
||||
* @param state0 State0 to be restored
|
||||
* @param state1 State1 to be restored
|
||||
*/
|
||||
void voc_algorithm_set_states(VocAlgorithmParams *params, int32_t state0, int32_t state1);
|
||||
|
||||
/**
|
||||
* Set parameters to customize the VOC algorithm. Call this once after
|
||||
* voc_algorithm_init(), if desired. Otherwise, the default values will be used.
|
||||
*
|
||||
* @param params Pointer to the VocAlgorithmParams struct
|
||||
* @param voc_index_offset VOC index representing typical (average)
|
||||
* conditions. Range 1..250, default 100
|
||||
* @param learning_time_hours Time constant of long-term estimator.
|
||||
* Past events will be forgotten after about
|
||||
* twice the learning time.
|
||||
* Range 1..72 [hours], default 12 [hours]
|
||||
* @param gating_max_duration_minutes Maximum duration of gating (freeze of
|
||||
* estimator during high VOC index signal).
|
||||
* 0 (no gating) or range 1..720 [minutes],
|
||||
* default 180 [minutes]
|
||||
* @param std_initial Initial estimate for standard deviation.
|
||||
* Lower value boosts events during initial
|
||||
* learning period, but may result in larger
|
||||
* device-to-device variations.
|
||||
* Range 10..500, default 50
|
||||
*/
|
||||
void voc_algorithm_set_tuning_parameters(VocAlgorithmParams *params, int32_t voc_index_offset,
|
||||
int32_t learning_time_hours, int32_t gating_max_duration_minutes,
|
||||
int32_t std_initial);
|
||||
|
||||
/**
|
||||
* Calculate the VOC index value from the raw sensor value.
|
||||
*
|
||||
* @param params Pointer to the VocAlgorithmParams struct
|
||||
* @param sraw Raw value from the SGP40 sensor
|
||||
* @param voc_index Calculated VOC index value from the raw sensor value. Zero
|
||||
* during initial blackout period and 1..500 afterwards
|
||||
*/
|
||||
void voc_algorithm_process(VocAlgorithmParams *params, int32_t sraw, int32_t *voc_index);
|
||||
} // namespace sgp40
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,57 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.const import CONF_ID, DEVICE_CLASS_EMPTY, ICON_RADIATOR, UNIT_EMPTY
|
||||
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
CODEOWNERS = ["@SenexCrenshaw"]
|
||||
|
||||
sgp40_ns = cg.esphome_ns.namespace("sgp40")
|
||||
SGP40Component = sgp40_ns.class_(
|
||||
"SGP40Component", sensor.Sensor, cg.PollingComponent, i2c.I2CDevice
|
||||
)
|
||||
|
||||
CONF_COMPENSATION = "compensation"
|
||||
CONF_HUMIDITY_SOURCE = "humidity_source"
|
||||
CONF_TEMPERATURE_SOURCE = "temperature_source"
|
||||
CONF_STORE_BASELINE = "store_baseline"
|
||||
CONF_VOC_BASELINE = "voc_baseline"
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
sensor.sensor_schema(UNIT_EMPTY, ICON_RADIATOR, 0, DEVICE_CLASS_EMPTY)
|
||||
.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(SGP40Component),
|
||||
cv.Optional(CONF_STORE_BASELINE, default=True): cv.boolean,
|
||||
cv.Optional(CONF_VOC_BASELINE): cv.hex_uint16_t,
|
||||
cv.Optional(CONF_COMPENSATION): cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_HUMIDITY_SOURCE): cv.use_id(sensor.Sensor),
|
||||
cv.Required(CONF_TEMPERATURE_SOURCE): cv.use_id(sensor.Sensor),
|
||||
},
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(i2c.i2c_device_schema(0x59))
|
||||
)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield i2c.register_i2c_device(var, config)
|
||||
yield sensor.register_sensor(var, config)
|
||||
|
||||
if CONF_COMPENSATION in config:
|
||||
compensation_config = config[CONF_COMPENSATION]
|
||||
sens = yield cg.get_variable(compensation_config[CONF_HUMIDITY_SOURCE])
|
||||
cg.add(var.set_humidity_sensor(sens))
|
||||
sens = yield cg.get_variable(compensation_config[CONF_TEMPERATURE_SOURCE])
|
||||
cg.add(var.set_temperature_sensor(sens))
|
||||
|
||||
cg.add(var.set_store_baseline(config[CONF_STORE_BASELINE]))
|
||||
|
||||
if CONF_VOC_BASELINE in config:
|
||||
cg.add(var.set_voc_baseline(CONF_VOC_BASELINE))
|
||||
@@ -0,0 +1,314 @@
|
||||
#include "esphome/core/log.h"
|
||||
#include "sgp40.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sgp40 {
|
||||
|
||||
static const char *TAG = "sgp40";
|
||||
|
||||
void SGP40Component::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up SGP40...");
|
||||
|
||||
// Serial Number identification
|
||||
if (!this->write_command_(SGP40_CMD_GET_SERIAL_ID)) {
|
||||
this->error_code_ = COMMUNICATION_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
uint16_t raw_serial_number[3];
|
||||
|
||||
if (!this->read_data_(raw_serial_number, 3)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
this->serial_number_ = (uint64_t(raw_serial_number[0]) << 24) | (uint64_t(raw_serial_number[1]) << 16) |
|
||||
(uint64_t(raw_serial_number[2]));
|
||||
ESP_LOGD(TAG, "Serial Number: %llu", this->serial_number_);
|
||||
|
||||
// Featureset identification for future use
|
||||
if (!this->write_command_(SGP40_CMD_GET_FEATURESET)) {
|
||||
ESP_LOGD(TAG, "raw_featureset write_command_ failed");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
uint16_t raw_featureset[1];
|
||||
if (!this->read_data_(raw_featureset, 1)) {
|
||||
ESP_LOGD(TAG, "raw_featureset read_data_ failed");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
this->featureset_ = raw_featureset[0];
|
||||
if ((this->featureset_ & 0x1FF) != SGP40_FEATURESET) {
|
||||
ESP_LOGD(TAG, "Product feature set failed 0x%0X , expecting 0x%0X", uint16_t(this->featureset_ & 0x1FF),
|
||||
SGP40_FEATURESET);
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Product version: 0x%0X", uint16_t(this->featureset_ & 0x1FF));
|
||||
|
||||
voc_algorithm_init(&this->voc_algorithm_params_);
|
||||
|
||||
if (this->store_baseline_) {
|
||||
// Hash with compilation time
|
||||
// This ensures the baseline storage is cleared after OTA
|
||||
uint32_t hash = fnv1_hash(App.get_compilation_time());
|
||||
this->pref_ = global_preferences.make_preference<SGP40Baselines>(hash, true);
|
||||
|
||||
if (this->pref_.load(&this->baselines_storage_)) {
|
||||
this->state0_ = this->baselines_storage_.state0;
|
||||
this->state1_ = this->baselines_storage_.state1;
|
||||
ESP_LOGI(TAG, "Loaded VOC baseline state0: 0x%04X, state1: 0x%04X", this->baselines_storage_.state0,
|
||||
baselines_storage_.state1);
|
||||
}
|
||||
|
||||
// Initialize storage timestamp
|
||||
this->seconds_since_last_store_ = 0;
|
||||
|
||||
if (this->baselines_storage_.state0 > 0 && this->baselines_storage_.state1 > 0) {
|
||||
ESP_LOGI(TAG, "Setting VOC baseline from save state0: 0x%04X, state1: 0x%04X", this->baselines_storage_.state0,
|
||||
baselines_storage_.state1);
|
||||
voc_algorithm_set_states(&this->voc_algorithm_params_, this->baselines_storage_.state0,
|
||||
this->baselines_storage_.state1);
|
||||
}
|
||||
}
|
||||
|
||||
this->self_test_();
|
||||
}
|
||||
|
||||
void SGP40Component::self_test_() {
|
||||
ESP_LOGD(TAG, "selfTest started");
|
||||
if (!this->write_command_(SGP40_CMD_SELF_TEST)) {
|
||||
this->error_code_ = COMMUNICATION_FAILED;
|
||||
ESP_LOGD(TAG, "selfTest communicatin failed");
|
||||
this->mark_failed();
|
||||
}
|
||||
|
||||
this->set_timeout(250, [this]() {
|
||||
uint16_t reply[1];
|
||||
if (!this->read_data_(reply, 1)) {
|
||||
ESP_LOGD(TAG, "selfTest read_data_ failed");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
if (reply[0] == 0xD400) {
|
||||
ESP_LOGD(TAG, "selfTest completed");
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "selfTest failed");
|
||||
this->mark_failed();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Combined the measured gasses, temperature, and humidity
|
||||
* to calculate the VOC Index
|
||||
*
|
||||
* @param temperature The measured temperature in degrees C
|
||||
* @param humidity The measured relative humidity in % rH
|
||||
* @return int32_t The VOC Index
|
||||
*/
|
||||
int32_t SGP40Component::measure_voc_index_() {
|
||||
int32_t voc_index;
|
||||
|
||||
uint16_t sraw = measure_raw_();
|
||||
|
||||
if (sraw == UINT16_MAX)
|
||||
return UINT16_MAX;
|
||||
|
||||
this->status_clear_warning();
|
||||
|
||||
voc_algorithm_process(&voc_algorithm_params_, sraw, &voc_index);
|
||||
|
||||
// Store baselines after defined interval or if the difference between current and stored baseline becomes too
|
||||
// much
|
||||
if (this->store_baseline_ && this->seconds_since_last_store_ > SHORTEST_BASELINE_STORE_INTERVAL) {
|
||||
voc_algorithm_get_states(&voc_algorithm_params_, &this->state0_, &this->state1_);
|
||||
if (abs(this->baselines_storage_.state0 - this->state0_) > MAXIMUM_STORAGE_DIFF ||
|
||||
abs(this->baselines_storage_.state1 - this->state1_) > MAXIMUM_STORAGE_DIFF) {
|
||||
this->seconds_since_last_store_ = 0;
|
||||
this->baselines_storage_.state0 = this->state0_;
|
||||
this->baselines_storage_.state1 = this->state1_;
|
||||
|
||||
if (this->pref_.save(&this->baselines_storage_)) {
|
||||
ESP_LOGI(TAG, "Stored VOC baseline state0: 0x%04X ,state1: 0x%04X", this->baselines_storage_.state0,
|
||||
baselines_storage_.state1);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Could not store VOC baselines");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return voc_index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the raw gas measurement
|
||||
*
|
||||
* @param temperature The measured temperature in degrees C
|
||||
* @param humidity The measured relative humidity in % rH
|
||||
* @return uint16_t The current raw gas measurement
|
||||
*/
|
||||
uint16_t SGP40Component::measure_raw_() {
|
||||
float humidity = NAN;
|
||||
if (this->humidity_sensor_ != nullptr) {
|
||||
humidity = this->humidity_sensor_->state;
|
||||
}
|
||||
if (isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
|
||||
humidity = 50;
|
||||
}
|
||||
|
||||
float temperature = NAN;
|
||||
if (this->temperature_sensor_ != nullptr) {
|
||||
temperature = float(this->temperature_sensor_->state);
|
||||
}
|
||||
if (isnan(temperature) || temperature < -40.0f || temperature > 85.0f) {
|
||||
temperature = 25;
|
||||
}
|
||||
|
||||
uint8_t command[8];
|
||||
|
||||
command[0] = 0x26;
|
||||
command[1] = 0x0F;
|
||||
|
||||
uint16_t rhticks = llround((uint16_t)((humidity * 65535) / 100));
|
||||
command[2] = rhticks >> 8;
|
||||
command[3] = rhticks & 0xFF;
|
||||
command[4] = generate_crc_(command + 2, 2);
|
||||
uint16_t tempticks = (uint16_t)(((temperature + 45) * 65535) / 175);
|
||||
command[5] = tempticks >> 8;
|
||||
command[6] = tempticks & 0xFF;
|
||||
command[7] = generate_crc_(command + 5, 2);
|
||||
|
||||
if (!this->write_bytes_raw(command, 8)) {
|
||||
this->status_set_warning();
|
||||
ESP_LOGD(TAG, "write_bytes_raw error");
|
||||
return UINT16_MAX;
|
||||
}
|
||||
delay(250); // NOLINT
|
||||
uint16_t raw_data[1];
|
||||
|
||||
if (!this->read_data_(raw_data, 1)) {
|
||||
this->status_set_warning();
|
||||
ESP_LOGD(TAG, "read_data_ error");
|
||||
return UINT16_MAX;
|
||||
}
|
||||
return raw_data[0];
|
||||
}
|
||||
|
||||
uint8_t SGP40Component::generate_crc_(const uint8_t *data, uint8_t datalen) {
|
||||
// calculates 8-Bit checksum with given polynomial
|
||||
uint8_t crc = SGP40_CRC8_INIT;
|
||||
|
||||
for (uint8_t i = 0; i < datalen; i++) {
|
||||
crc ^= data[i];
|
||||
for (uint8_t b = 0; b < 8; b++) {
|
||||
if (crc & 0x80)
|
||||
crc = (crc << 1) ^ SGP40_CRC8_POLYNOMIAL;
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
void SGP40Component::update() {
|
||||
this->seconds_since_last_store_ += this->update_interval_ / 1000;
|
||||
|
||||
uint32_t voc_index = this->measure_voc_index_();
|
||||
|
||||
if (this->samples_read_++ < this->samples_to_stabalize_) {
|
||||
ESP_LOGD(TAG, "Sensor has not collected enough samples yet. (%d/%d) VOC index is: %u", this->samples_read_,
|
||||
this->samples_to_stabalize_, voc_index);
|
||||
return;
|
||||
}
|
||||
|
||||
if (voc_index != UINT16_MAX) {
|
||||
this->status_clear_warning();
|
||||
this->publish_state(voc_index);
|
||||
} else {
|
||||
this->status_set_warning();
|
||||
}
|
||||
}
|
||||
|
||||
void SGP40Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "SGP40:");
|
||||
LOG_I2C_DEVICE(this);
|
||||
if (this->is_failed()) {
|
||||
switch (this->error_code_) {
|
||||
case COMMUNICATION_FAILED:
|
||||
ESP_LOGW(TAG, "Communication failed! Is the sensor connected?");
|
||||
break;
|
||||
default:
|
||||
ESP_LOGW(TAG, "Unknown setup error!");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGCONFIG(TAG, " Serial number: %llu", this->serial_number_);
|
||||
ESP_LOGCONFIG(TAG, " Minimum Samples: %f", VOC_ALGORITHM_INITIAL_BLACKOUT);
|
||||
}
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
|
||||
if (this->humidity_sensor_ != nullptr && this->temperature_sensor_ != nullptr) {
|
||||
ESP_LOGCONFIG(TAG, " Compensation:");
|
||||
LOG_SENSOR(" ", "Temperature Source:", this->temperature_sensor_);
|
||||
LOG_SENSOR(" ", "Humidity Source:", this->humidity_sensor_);
|
||||
} else {
|
||||
ESP_LOGCONFIG(TAG, " Compensation: No source configured");
|
||||
}
|
||||
}
|
||||
|
||||
bool SGP40Component::write_command_(uint16_t command) {
|
||||
// Warning ugly, trick the I2Ccomponent base by setting register to the first 8 bit.
|
||||
return this->write_byte(command >> 8, command & 0xFF);
|
||||
}
|
||||
|
||||
uint8_t SGP40Component::sht_crc_(uint8_t data1, uint8_t data2) {
|
||||
uint8_t bit;
|
||||
uint8_t crc = 0xFF;
|
||||
|
||||
crc ^= data1;
|
||||
for (bit = 8; bit > 0; --bit) {
|
||||
if (crc & 0x80)
|
||||
crc = (crc << 1) ^ 0x131;
|
||||
else
|
||||
crc = (crc << 1);
|
||||
}
|
||||
|
||||
crc ^= data2;
|
||||
for (bit = 8; bit > 0; --bit) {
|
||||
if (crc & 0x80)
|
||||
crc = (crc << 1) ^ 0x131;
|
||||
else
|
||||
crc = (crc << 1);
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
bool SGP40Component::read_data_(uint16_t *data, uint8_t len) {
|
||||
const uint8_t num_bytes = len * 3;
|
||||
std::vector<uint8_t> buf(num_bytes);
|
||||
|
||||
if (!this->parent_->raw_receive(this->address_, buf.data(), num_bytes)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
const uint8_t j = 3 * i;
|
||||
uint8_t crc = sht_crc_(buf[j], buf[j + 1]);
|
||||
if (crc != buf[j + 2]) {
|
||||
ESP_LOGE(TAG, "CRC8 Checksum invalid! 0x%02X != 0x%02X", buf[j + 2], crc);
|
||||
return false;
|
||||
}
|
||||
data[i] = (buf[j] << 8) | buf[j + 1];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace sgp40
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
#include "sensirion_voc_algorithm.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace esphome {
|
||||
namespace sgp40 {
|
||||
|
||||
struct SGP40Baselines {
|
||||
int32_t state0;
|
||||
int32_t state1;
|
||||
} PACKED; // NOLINT
|
||||
|
||||
// commands and constants
|
||||
static const uint8_t SGP40_FEATURESET = 0x0020; ///< The required set for this library
|
||||
static const uint8_t SGP40_CRC8_POLYNOMIAL = 0x31; ///< Seed for SGP40's CRC polynomial
|
||||
static const uint8_t SGP40_CRC8_INIT = 0xFF; ///< Init value for CRC
|
||||
static const uint8_t SGP40_WORD_LEN = 2; ///< 2 bytes per word
|
||||
|
||||
// Commands
|
||||
|
||||
static const uint16_t SGP40_CMD_GET_SERIAL_ID = 0x3682;
|
||||
static const uint16_t SGP40_CMD_GET_FEATURESET = 0x202f;
|
||||
static const uint16_t SGP40_CMD_SELF_TEST = 0x280e;
|
||||
|
||||
// Shortest time interval of 3H for storing baseline values.
|
||||
// Prevents wear of the flash because of too many write operations
|
||||
const long SHORTEST_BASELINE_STORE_INTERVAL = 10800;
|
||||
|
||||
// Store anyway if the baseline difference exceeds the max storage diff value
|
||||
const long MAXIMUM_STORAGE_DIFF = 50;
|
||||
|
||||
class SGP40Component;
|
||||
|
||||
/// This class implements support for the Sensirion sgp40 i2c GAS (VOC) sensors.
|
||||
class SGP40Component : public PollingComponent, public sensor::Sensor, public i2c::I2CDevice {
|
||||
public:
|
||||
void set_humidity_sensor(sensor::Sensor *humidity) { humidity_sensor_ = humidity; }
|
||||
void set_temperature_sensor(sensor::Sensor *temperature) { temperature_sensor_ = temperature; }
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
void set_store_baseline(bool store_baseline) { store_baseline_ = store_baseline; }
|
||||
|
||||
protected:
|
||||
/// Input sensor for humidity and temperature compensation.
|
||||
sensor::Sensor *humidity_sensor_{nullptr};
|
||||
sensor::Sensor *temperature_sensor_{nullptr};
|
||||
bool write_command_(uint16_t command);
|
||||
bool read_data_(uint16_t *data, uint8_t len);
|
||||
int16_t sensirion_init_sensors_();
|
||||
int16_t sgp40_probe_();
|
||||
uint8_t sht_crc_(uint8_t data1, uint8_t data2);
|
||||
uint64_t serial_number_;
|
||||
uint16_t featureset_;
|
||||
int32_t measure_voc_index_();
|
||||
uint8_t generate_crc_(const uint8_t *data, uint8_t datalen);
|
||||
uint16_t measure_raw_();
|
||||
ESPPreferenceObject pref_;
|
||||
long seconds_since_last_store_;
|
||||
SGP40Baselines baselines_storage_;
|
||||
VocAlgorithmParams voc_algorithm_params_;
|
||||
bool store_baseline_;
|
||||
int32_t state0_;
|
||||
int32_t state1_;
|
||||
uint8_t samples_read_ = 0;
|
||||
uint8_t samples_to_stabalize_ = static_cast<int8_t>(VOC_ALGORITHM_INITIAL_BLACKOUT) * 2;
|
||||
|
||||
/**
|
||||
* @brief Request the sensor to perform a self-test, returning the result
|
||||
*
|
||||
* @return true: success false:failure
|
||||
*/
|
||||
void self_test_();
|
||||
enum ErrorCode {
|
||||
COMMUNICATION_FAILED,
|
||||
MEASUREMENT_INIT_FAILED,
|
||||
INVALID_ID,
|
||||
UNSUPPORTED_ID,
|
||||
UNKNOWN
|
||||
} error_code_{UNKNOWN};
|
||||
};
|
||||
} // namespace sgp40
|
||||
} // namespace esphome
|
||||
+4
-14
@@ -235,10 +235,6 @@ wled:
|
||||
|
||||
adalight:
|
||||
|
||||
mcp3008:
|
||||
- id: 'mcp3008_hub'
|
||||
cs_pin: GPIO12
|
||||
|
||||
mcp23s08:
|
||||
- id: 'mcp23s08_hub'
|
||||
cs_pin: GPIO12
|
||||
@@ -877,12 +873,6 @@ sensor:
|
||||
id: ph_ezo
|
||||
address: 99
|
||||
unit_of_measurement: 'pH'
|
||||
- platform: mcp3008
|
||||
update_interval: 5s
|
||||
mcp3008_id: 'mcp3008_hub'
|
||||
id: freezer_temp_source
|
||||
reference_voltage: 3.19
|
||||
number: 0
|
||||
|
||||
esp32_touch:
|
||||
setup_mode: False
|
||||
@@ -1488,14 +1478,14 @@ climate:
|
||||
min_temperature: 18 °C
|
||||
max_temperature: 25 °C
|
||||
temperature_step: 0.1 °C
|
||||
name: "Electrolux EACS"
|
||||
name: 'Electrolux EACS'
|
||||
beeper: true
|
||||
outdoor_temperature:
|
||||
name: "Temp"
|
||||
name: 'Temp'
|
||||
power_usage:
|
||||
name: "Power"
|
||||
name: 'Power'
|
||||
humidity_setpoint:
|
||||
name: "Hum"
|
||||
name: 'Hum'
|
||||
|
||||
midea_dongle:
|
||||
uart_id: uart0
|
||||
|
||||
+17
-4
@@ -54,6 +54,10 @@ deep_sleep:
|
||||
as3935_i2c:
|
||||
irq_pin: GPIO12
|
||||
|
||||
mcp3008:
|
||||
- id: 'mcp3008_hub'
|
||||
cs_pin: GPIO12
|
||||
|
||||
sensor:
|
||||
- platform: homeassistant
|
||||
entity_id: sensor.hello_world
|
||||
@@ -212,12 +216,21 @@ sensor:
|
||||
- platform: inkbird_ibsth1_mini
|
||||
mac_address: 38:81:D7:0A:9C:11
|
||||
temperature:
|
||||
name: 'Inkbird IBS-TH1 Temperature'
|
||||
name: 'Inkbird IBS-TH1 Temperature'
|
||||
humidity:
|
||||
name: 'Inkbird IBS-TH1 Humidity'
|
||||
name: 'Inkbird IBS-TH1 Humidity'
|
||||
battery_level:
|
||||
name: 'Inkbird IBS-TH1 Battery Level'
|
||||
|
||||
name: 'Inkbird IBS-TH1 Battery Level'
|
||||
- platform: sgp40
|
||||
name: 'Workshop VOC'
|
||||
update_interval: 5s
|
||||
store_baseline: 'true'
|
||||
- platform: mcp3008
|
||||
update_interval: 5s
|
||||
mcp3008_id: 'mcp3008_hub'
|
||||
id: freezer_temp_source
|
||||
reference_voltage: 3.19
|
||||
number: 0
|
||||
time:
|
||||
- platform: homeassistant
|
||||
on_time:
|
||||
|
||||
Reference in New Issue
Block a user