14static constexpr uint32_t REFERENCE_PREFERENCE_VERSION = 0x00000010;
18 const LogString *conversion;
19 const LogString *reference;
25 return {LOG_STR(
"dfrobot"), LOG_STR(
"air reference"), LOG_STR(
"air_reference")};
26 return {LOG_STR(
"datasheet"), LOG_STR(
"R0"), LOG_STR(
"r0")};
31#ifdef USE_MICS_5524_DATASHEET
38 ESP_LOGD(TAG,
"'%s': ratio=%.4f -> %.1f ppm", LOG_STR_ARG(this->
get_log_name()), ratio, ppm);
56 ESP_LOGI(TAG,
"using the %s from the configuration (%.4f) - the sensor is not calibrated at boot",
59 ESP_LOGI(TAG,
"restored the %s from flash (%.4f)", LOG_STR_ARG(model_strings(this->
conversion_).reference),
68 ESP_LOGI(TAG,
"warm-up finished, readings are published from now on");
76 ESP_LOGI(TAG,
"warm-up of %" PRIu32
" s - no readings published before that", this->
warmup_time_ / 1000);
82 ESP_LOGI(TAG,
"%s already known (%.4f) - automatic calibration skipped",
90 ESP_LOGI(TAG,
"clean-air calibration of the %s in %" PRIu32
" s - the sensor must be in clean air",
91 LOG_STR_ARG(model_strings(this->
conversion_).reference), boot_delay / 1000);
95 "no %s available, the PPM values stay unknown\n set it in the configuration or enable 'calibration:'",
96 LOG_STR_ARG(model_strings(this->
conversion_).reference));
106 " Conversion: %s, VCC: %.2f V, AO multiplier: x%.3f, AO samples per reading: %u\n"
107 " Warm-up: %" PRIu32
" s, calibration: %s, sample spacing: %" PRIu32
" ms",
112 LOG_UPDATE_INTERVAL(
this);
115 ESP_LOGCONFIG(TAG,
" Load resistor: %.1f kOhm", this->
rl_);
118 ESP_LOGCONFIG(TAG,
" %s: %.4f", LOG_STR_ARG(model_strings(this->
conversion_).reference), this->
reference_);
120 ESP_LOGCONFIG(TAG,
" %s: not available yet", LOG_STR_ARG(model_strings(this->
conversion_).reference));
122 LOG_SENSOR(
" ",
"Voltage", this->voltage_sensor_);
123 LOG_SENSOR(
" ",
"Ratio", this->ratio_sensor_);
124 LOG_SENSOR(
" ",
"RS", this->rs_sensor_);
125#ifdef MICS_5524_GAS_COUNT
126 for (
auto *gas : this->
gases_) {
127 LOG_SENSOR(
" ",
"Gas", gas);
128 ESP_LOGCONFIG(TAG,
" Range: %.1f - %.1f ppm", gas->get_min_ppm(), gas->get_max_ppm());
133float MiCS5524Component::sample_voltage_() {
136 for (uint8_t i = 0; i < this->
samples_; i++)
143#ifdef USE_MICS_5524_DATASHEET
147 if (!std::isnan(
value)) {
154 ESP_LOGW(TAG,
"analog output reads %.4f V with vcc %.2f V\n check the AO wiring, 'vcc:' and 'voltage_multiplier:'",
155 voltage, this->
vcc_);
162#ifdef MICS_5524_GAS_COUNT
163 for (
auto *gas : this->
gases_) {
170 PUBLISH_STATE(this->voltage_sensor_, voltage);
171 PUBLISH_STATE(this->ratio_sensor_, ratio);
173 PUBLISH_STATE(this->rs_sensor_, rs);
177 if (!this->
has_reference_() && this->voltage_sensor_ ==
nullptr && this->ratio_sensor_ ==
nullptr &&
178 this->rs_sensor_ ==
nullptr)
181 const float voltage = this->sample_voltage_();
198 ESP_LOGD(TAG,
"V_AO=%.4f V, ratio=%.4f", voltage, ratio);
213 ESP_LOGW(TAG,
"calibration already running");
217 ESP_LOGW(TAG,
"calibration already requested");
223 ESP_LOGI(TAG,
"calibration requested - waiting for the warm-up window to end");
227 ESP_LOGI(TAG,
"calibration requested - keep the sensor in clean air");
260 ESP_LOGI(TAG,
"calibrating the %s, %" PRIu32
" samples - keep the sensor in clean air",
268 const float reference = this->
model_value_(this->sample_voltage_());
269 if (!std::isnan(reference)) {
287 const uint32_t required = std::max<uint32_t>((attempts + 1) / 2, 2);
289 if (
valid < required) {
290 ESP_LOGW(TAG,
"calibration failed, %" PRIu32
" of %" PRIu32
" samples were valid",
valid, attempts);
299 ESP_LOGW(TAG,
"clean-air samples spread %.4f around %.4f (%.4f - %.4f)\n the sensor was not settled in clean air",
310 const ModelStrings strings = model_strings(this->
conversion_);
312 "%s = %.4f (%" PRIu32
"/%" PRIu32
" samples valid)%s\n"
313 " hard-code '%s: %.4f' to skip the calibration at boot",
314 LOG_STR_ARG(strings.reference), this->reference_,
valid, attempts,
315 stored ? LOG_STR_LITERAL(
", stored in flash")
316 : this->
persist_ ? LOG_STR_LITERAL(
"")
317 : LOG_STR_LITERAL(
", kept until reboot"),
318 LOG_STR_ARG(strings.key), this->reference_);
327 ESP_LOGW(TAG,
"retrying the calibration in %" PRIu32
" s",
delay / 1000);
332 ESP_LOGW(TAG,
"could not store the %s in flash", LOG_STR_ARG(model_strings(this->
conversion_).reference));
337 ESP_LOGD(TAG,
"%s %.4f saved", LOG_STR_ARG(model_strings(this->
conversion_).reference), this->
reference_);
345 if (!std::isfinite(stored) || stored <= 0.0f) {
346 ESP_LOGW(TAG,
"ignoring the stored %s %.4f, calibrating again",
347 LOG_STR_ARG(model_strings(this->
conversion_).reference), stored);
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void status_set_warning()
void enable_loop()
Enable this component's loop.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void disable_loop()
Disable this component's loop.
void status_clear_warning()
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
virtual void digital_write(bool value)=0
bool input_fault_
The out-of-range warning has been logged; the next usable reading clears it.
void begin_calibration_()
uint32_t calibration_samples_
bool warming_up_
Set from setup() until the warm-up timeout fires.
bool save_reference_()
True when the reference reached the storage.
void publish_diagnostics_(float voltage, float ratio, float rs)
bool has_reference_() const
uint32_t calibration_count_
uint32_t sample_interval_
Spacing of the clean-air calibration samples (ms); the ~16 ms loop tick is the floor.
static constexpr uint32_t CALIBRATION_START_ID
bool calibration_pending_
A calibration waits for its start time; calibrating_ takes over from there.
ESPPreferenceObject reference_pref_
void finish_calibration_()
uint32_t calibration_delay_
float reference_
Clean-air reference, x_air in volts or R0 in kOhm, 0 while there is none.
void publish_ratio_(float ratio)
void request_calibration()
Deferred until the warm-up window ends.
StaticVector< MiCS5524GasSensor *, MICS_5524_GAS_COUNT > gases_
bool is_vendor_model_() const
float voltage_multiplier_
void schedule_calibration_retry_()
SUB_SENSOR(voltage) SUB_SENSOR(ratio) SUB_SENSOR(rs) protected float model_value_(float voltage)
Value of the active model that the reference is taken from, x = VCC - V_AO (vendor) or RS (datasheet)...
micsmath::ConversionModel conversion_
void set_calibration(uint32_t delay, uint32_t samples, uint32_t sample_interval, bool persist, bool at_boot)
Calibration parameters: start after delay ms, average samples readings sample_interval ms apart; pers...
void dump_config() override
voltage_sampler::VoltageSampler * source_
uint32_t calibration_last_sample_
void schedule_calibration_(uint32_t delay)
Start a calibration after delay ms; every start goes through here.
static constexpr uint32_t CALIBRATION_RETRY_MIN_MS
Floor of the delay between two calibration attempts (ms).
uint32_t calibration_attempts_
void process_calibration_()
float calibration_min_
Smallest and largest clean-air sample of the run, see calibration_is_stable().
void publish_ratio(float ratio, micsmath::ConversionModel model)
Publish the concentration for ratio with the formula of model; NAN publishes unknown.
void publish_state(float state)
Publish a new state to the front-end.
virtual float sample()=0
Get a voltage reading, in V.
bool calibration_is_stable(float min_value, float max_value, float mean)
True when the clean-air samples spread at most MAX_CALIBRATION_SPREAD of their mean.
float ratio_from_rs(float rs, float r0)
ratio = RS / R0 (NaN when either value is unusable); 0 is the saturated end.
float air_value_from_voltage(float voltage, float vcc)
x = VCC - V_ao: 0 at VCC (a saturated sensor, never clean air).
float ppm_from_power_law(float a, float b, float ratio)
ppm = a * ratio^b; NaN for a ratio or coefficient that cannot produce a reading.
float clamp_ppm(float ppm, float min_ppm, float max_ppm)
Clamp to the configured range: below min_ppm counts as not detected and reads 0, 0 itself never chang...
float rs_from_voltage(float voltage, float vcc, float rl)
RS = (VCC * RL) / V - RL in kOhm: 0 at VCC (a saturated sensor).
float ratio_from_air_value(float x, float x_air)
ratio = x / x_air (NaN when either value is unusable).
ConversionModel
Generated through CONVERSION_MODELS in init.py.
@ CONVERSION_MODEL_DFROBOT
@ CONVERSION_MODEL_DATASHEET
float ppm_from_vendor_curve(float ratio, const VendorCurve &curve)
ratio -> ppm as DFRobot_MICS getGasData(): 0 above the threshold or below the vendor range,...
ESPHOME_LOG_TAG(TAG, "mics_5524")
ESPPreferences * global_preferences
void HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
bool sync()
Commit pending writes to flash.