ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
mics_5524.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5
9#include "esphome/core/gpio.h"
12
13#include "mics_math.h"
14
15namespace esphome::mics_5524 {
16
19 public:
21 MiCS5524GasSensor(float p0, float p1, float min_ppm, float max_ppm)
22 : p0_(p0), p1_(p1), min_ppm_(min_ppm), max_ppm_(max_ppm) {}
23
25 void publish_ratio(float ratio, micsmath::ConversionModel model);
26
27 float get_min_ppm() const { return this->min_ppm_; }
28 float get_max_ppm() const { return this->max_ppm_; }
29
30 protected:
31 float p0_;
32 float p1_;
33 float min_ppm_;
34 float max_ppm_;
35};
36
41 public:
44
45 void setup() override;
46 void loop() override;
47 void update() override;
48 void dump_config() override;
49
50#ifdef MICS_5524_GAS_COUNT
51 void register_gas_sensor(MiCS5524GasSensor *gas) { this->gases_.push_back(gas); }
52#endif
53
54 void set_conversion(micsmath::ConversionModel conversion) { this->conversion_ = conversion; }
55 void set_vcc(float vcc) { this->vcc_ = vcc; }
56 void set_rl(float rl) { this->rl_ = rl; }
57 void set_voltage_multiplier(float multiplier) { this->voltage_multiplier_ = multiplier; }
58 void set_samples(uint8_t samples) { this->samples_ = samples; }
59 void set_warmup_time(uint32_t warmup) { this->warmup_time_ = warmup; }
63 void set_enable_pin(GPIOPin *pin) { this->enable_pin_ = pin; }
66 void set_calibration(uint32_t delay, uint32_t samples, uint32_t sample_interval, bool persist, bool at_boot);
68 void set_reference(float reference) { this->reference_ = reference; }
71 SUB_SENSOR(voltage)
72 SUB_SENSOR(ratio)
73 // RS only exists in the datasheet model.
74 SUB_SENSOR(rs)
75
76 protected:
78 float sample_voltage_();
81 float model_value_(float voltage);
82 void publish_ratio_(float ratio);
83 void publish_diagnostics_(float voltage, float ratio, float rs);
84
87 void begin_calibration_();
92 bool save_reference_();
93 bool load_reference_();
95 bool has_reference_() const { return this->reference_ > 0.0f; }
96
98 float vcc_{5.0f};
99 float rl_{10.0f};
103
108
109 static constexpr uint32_t CALIBRATION_START_ID = 0;
111 static constexpr uint32_t CALIBRATION_RETRY_MIN_MS = 10000;
112
116 float calibration_sum_{0.0f};
118 float calibration_min_{0.0f};
119 float calibration_max_{0.0f};
120
122 float reference_{0.0f};
123
126
127 // Sized by code generation; the host unit tests run none, so the list compiles out there.
128#ifdef MICS_5524_GAS_COUNT
130#endif
131
133 uint8_t samples_{4};
134 bool persist_{false};
137 bool warming_up_{false};
138 bool calibrating_{false};
142 bool input_fault_{false};
143};
144
145} // namespace esphome::mics_5524
This class simplifies creating components that periodically check a state.
Definition component.h:532
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:304
The MiCS-5524 breakout: one analog output, converted for every configured gas against the clean-air r...
Definition mics_5524.h:40
bool input_fault_
The out-of-range warning has been logged; the next usable reading clears it.
Definition mics_5524.h:142
void set_voltage_multiplier(float multiplier)
Definition mics_5524.h:57
bool warming_up_
Set from setup() until the warm-up timeout fires.
Definition mics_5524.h:137
bool save_reference_()
True when the reference reached the storage.
void publish_diagnostics_(float voltage, float ratio, float rs)
MiCS5524Component(voltage_sampler::VoltageSampler *source)
source must outlive this component, which code generation guarantees.
Definition mics_5524.h:43
void set_enable_pin(GPIOPin *pin)
Driven active in setup(); EN of the breakout is active low, so inverted: true turns that on.
Definition mics_5524.h:63
void register_gas_sensor(MiCS5524GasSensor *gas)
Definition mics_5524.h:51
uint32_t sample_interval_
Spacing of the clean-air calibration samples (ms); the ~16 ms loop tick is the floor.
Definition mics_5524.h:107
static constexpr uint32_t CALIBRATION_START_ID
Definition mics_5524.h:109
bool calibration_pending_
A calibration waits for its start time; calibrating_ takes over from there.
Definition mics_5524.h:140
ESPPreferenceObject reference_pref_
Definition mics_5524.h:125
float reference_
Clean-air reference, x_air in volts or R0 in kOhm, 0 while there is none.
Definition mics_5524.h:122
void request_calibration()
Deferred until the warm-up window ends.
StaticVector< MiCS5524GasSensor *, MICS_5524_GAS_COUNT > gases_
Definition mics_5524.h:129
void set_warmup_time(uint32_t warmup)
Definition mics_5524.h:59
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_
Definition mics_5524.h:132
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 set_conversion(micsmath::ConversionModel conversion)
Definition mics_5524.h:54
voltage_sampler::VoltageSampler * source_
Definition mics_5524.h:97
void set_preference_key(uint32_t key)
Base of the flash key of the stored reference, a hash of the component ID and the front end.
Definition mics_5524.h:61
void set_samples(uint8_t samples)
Definition mics_5524.h:58
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).
Definition mics_5524.h:111
void set_reference(float reference)
Clean-air reference from the configuration, x_air (dfrobot) or R0 (datasheet).
Definition mics_5524.h:68
float calibration_min_
Smallest and largest clean-air sample of the run, see calibration_is_stable().
Definition mics_5524.h:118
One gas of a MiCS-5524: converts the measured ratio with the curve of that gas.
Definition mics_5524.h:18
MiCS5524GasSensor(float p0, float p1, float min_ppm, float max_ppm)
p0/p1 are threshold and gain (dfrobot) or a and b (datasheet), from coefficients.py.
Definition mics_5524.h:21
void publish_ratio(float ratio, micsmath::ConversionModel model)
Publish the concentration for ratio with the formula of model; NAN publishes unknown.
Definition mics_5524.cpp:29
Base-class for all sensors.
Definition sensor.h:48
Abstract interface for components to request voltage (usually ADC readings)
const char * key
ConversionModel
Generated through CONVERSION_MODELS in init.py.
Definition mics_math.h:14
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t