ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
sgp4x.h
Go to the documentation of this file.
1#pragma once
2
3#include <cinttypes>
4#include <cmath>
5
11#include <VOCGasIndexAlgorithm.h>
12#include <NOxGasIndexAlgorithm.h>
13
14namespace esphome::sgp4x {
15
17 float state0;
18 float state1;
19};
20
22
31
32// commands and constants
33static const uint8_t SGP40_FEATURESET = 0x0020; // can measure VOC
34static const uint8_t SGP41_FEATURESET = 0x0040; // can measure VOC and NOX
35// Commands
36static const uint16_t SGP4X_CMD_GET_SERIAL_ID = 0x3682;
37static const uint16_t SGP4X_CMD_GET_FEATURESET = 0x202f;
38static const uint16_t SGP4X_CMD_SELF_TEST = 0x280e;
39static const uint16_t SGP40_CMD_MEASURE_RAW = 0x260F;
40static const uint16_t SGP41_CMD_MEASURE_RAW = 0x2619;
41static const uint16_t SGP41_CMD_NOX_CONDITIONING = 0x2612;
42
43// Shortest time interval of 3H for storing baseline values.
44// Prevents wear of the flash because of too many write operations
46static const uint16_t SGP4X_SELF_TEST_TIME = 320; // maximum self-test duration for both SGP40 and SGP41
47static const uint16_t SGP40_MEASURE_TIME = 30;
48static const uint16_t SGP41_MEASURE_TIME = 55;
49// Once the store interval has passed, store only if the baseline drifted from the stored copy by more than these
50// state0 is mean of variance estimator, hence can have larger absolute values and a larger diff threshold
51const float MAXIMUM_STORAGE_DIFF_STATE0 = 50.0f;
52// state1 is std of variance estimator, so it typically has smaller absolute values than state0, hence we use a smaller
53// diff threshold
54const float MAXIMUM_STORAGE_DIFF_STATE1 = 5.0f;
55
56class SGP4xComponent;
57
59class SGP4xComponent final : public PollingComponent,
60 public sensor::Sensor,
62 public:
63 // SGP4xComponent() {};
64 // Input sensors for humidity and temperature compensation
65 SUB_SENSOR(humidity)
67
68 void setup() override;
69 void update() override;
70 void take_sample();
71 void dump_config() override;
72 void set_store_baseline(bool store_baseline) { store_baseline_ = store_baseline; }
74 SUB_SENSOR(nox)
75 void set_voc_algorithm_tuning(uint16_t index_offset, uint16_t learning_time_offset_hours,
81 }
89
90 protected:
91 void self_test_();
92
94
96 void measure_raw_();
97 uint16_t voc_sraw_;
98 uint16_t nox_sraw_;
99
102
104
105 VOCGasIndexAlgorithm voc_algorithm_;
106 optional<GasTuning> voc_tuning_params_;
109 int32_t voc_index_ = 0;
110
111 int32_t nox_index_ = 0;
112 NOxGasIndexAlgorithm nox_algorithm_;
113 optional<GasTuning> nox_tuning_params_;
114
116 uint8_t samples_read_ = 0;
117 uint8_t samples_to_stabilize_ = static_cast<int8_t>(GasIndexAlgorithm_INITIAL_BLACKOUT) * 2;
119
120 optional<uint32_t> nox_conditioning_start_{};
124};
125} // namespace esphome::sgp4x
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
This class simplifies creating components that periodically check a state.
Definition component.h:532
Base-class for all sensors.
Definition sensor.h:48
This class implements support for the Sensirion sgp4x i2c GAS (VOC) sensors.
Definition sgp4x.h:61
SUB_SENSOR(voc) SUB_SENSOR(nox) void set_voc_algorithm_tuning(uint16_t index_offset
SGP4xBaselines voc_baselines_storage_
Definition sgp4x.h:123
uint16_t uint16_t uint16_t uint16_t std_initial
Definition sgp4x.h:77
uint16_t uint16_t learning_time_gain_hours
Definition sgp4x.h:76
optional< uint32_t > nox_conditioning_start_
Definition sgp4x.h:120
ESPPreferenceObject pref_
Definition sgp4x.h:121
void dump_config() override
Definition sgp4x.cpp:257
uint16_t uint16_t uint16_t gating_max_duration_minutes
Definition sgp4x.h:76
uint16_t learning_time_offset_hours
Definition sgp4x.h:75
void set_nox_algorithm_tuning(uint16_t index_offset, uint16_t learning_time_offset_hours, uint16_t learning_time_gain_hours, uint16_t gating_max_duration_minutes, uint16_t gain_factor)
Definition sgp4x.h:82
VOCGasIndexAlgorithm voc_algorithm_
Definition sgp4x.h:105
void set_store_baseline(bool store_baseline)
Definition sgp4x.h:72
optional< GasTuning > voc_tuning_params_
Definition sgp4x.h:106
NOxGasIndexAlgorithm nox_algorithm_
Definition sgp4x.h:112
SUB_SENSOR(humidity) SUB_SENSOR(temperature) void setup() override
optional< GasTuning > nox_tuning_params_
Definition sgp4x.h:113
uint16_t uint16_t uint16_t uint16_t uint16_t gain_factor
Definition sgp4x.h:77
const float MAXIMUM_STORAGE_DIFF_STATE0
Definition sgp4x.h:51
const float MAXIMUM_STORAGE_DIFF_STATE1
Definition sgp4x.h:54
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
Definition sgp4x.h:45
static void uint32_t
uint16_t gating_max_duration_minutes
Definition sgp4x.h:27
uint16_t learning_time_gain_hours
Definition sgp4x.h:26
uint16_t learning_time_offset_hours
Definition sgp4x.h:25
uint16_t temperature
Definition sun_gtil2.cpp:12