ESPHome 2025.5.0
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 {
15namespace sgp4x {
16
18 int32_t state0;
19 int32_t state1;
20} PACKED; // NOLINT
21
23
32
33// commands and constants
34static const uint8_t SGP40_FEATURESET = 0x0020; // can measure VOC
35static const uint8_t SGP41_FEATURESET = 0x0040; // can measure VOC and NOX
36// Commands
37static const uint16_t SGP4X_CMD_GET_SERIAL_ID = 0x3682;
38static const uint16_t SGP4X_CMD_GET_FEATURESET = 0x202f;
39static const uint16_t SGP4X_CMD_SELF_TEST = 0x280e;
40static const uint16_t SGP40_CMD_MEASURE_RAW = 0x260F;
41static const uint16_t SGP41_CMD_MEASURE_RAW = 0x2619;
42static const uint16_t SGP41_CMD_NOX_CONDITIONING = 0x2612;
43static const uint8_t SGP41_SUBCMD_NOX_CONDITIONING = 0x12;
44
45// Shortest time interval of 3H for storing baseline values.
46// Prevents wear of the flash because of too many write operations
47const uint32_t SHORTEST_BASELINE_STORE_INTERVAL = 10800;
48static const uint16_t SPG40_SELFTEST_TIME = 250; // 250 ms for self test
49static const uint16_t SPG41_SELFTEST_TIME = 320; // 320 ms for self test
50static const uint16_t SGP40_MEASURE_TIME = 30;
51static const uint16_t SGP41_MEASURE_TIME = 55;
52// Store anyway if the baseline difference exceeds the max storage diff value
53const float MAXIMUM_STORAGE_DIFF = 50.0f;
54
55class SGP4xComponent;
56
59 enum ErrorCode {
60 COMMUNICATION_FAILED,
61 MEASUREMENT_INIT_FAILED,
62 INVALID_ID,
63 UNSUPPORTED_ID,
64 SERIAL_NUMBER_IDENTIFICATION_FAILED,
65 SELF_TEST_FAILED,
66 UNKNOWN
67 } error_code_{UNKNOWN};
68
69 public:
70 // SGP4xComponent() {};
71 void set_humidity_sensor(sensor::Sensor *humidity) { humidity_sensor_ = humidity; }
73
74 void setup() override;
75 void update() override;
76 void take_sample();
77 void dump_config() override;
78 float get_setup_priority() const override { return setup_priority::DATA; }
79 void set_store_baseline(bool store_baseline) { store_baseline_ = store_baseline; }
80 void set_voc_sensor(sensor::Sensor *voc_sensor) { voc_sensor_ = voc_sensor; }
81 void set_nox_sensor(sensor::Sensor *nox_sensor) { nox_sensor_ = nox_sensor; }
82 void set_voc_algorithm_tuning(uint16_t index_offset, uint16_t learning_time_offset_hours,
83 uint16_t learning_time_gain_hours, uint16_t gating_max_duration_minutes,
84 uint16_t std_initial, uint16_t gain_factor) {
85 voc_tuning_params_.value().index_offset = index_offset;
86 voc_tuning_params_.value().learning_time_offset_hours = learning_time_offset_hours;
87 voc_tuning_params_.value().learning_time_gain_hours = learning_time_gain_hours;
88 voc_tuning_params_.value().gating_max_duration_minutes = gating_max_duration_minutes;
89 voc_tuning_params_.value().std_initial = std_initial;
90 voc_tuning_params_.value().gain_factor = gain_factor;
91 }
92 void set_nox_algorithm_tuning(uint16_t index_offset, uint16_t learning_time_offset_hours,
93 uint16_t learning_time_gain_hours, uint16_t gating_max_duration_minutes,
94 uint16_t gain_factor) {
95 nox_tuning_params_.value().index_offset = index_offset;
96 nox_tuning_params_.value().learning_time_offset_hours = learning_time_offset_hours;
97 nox_tuning_params_.value().learning_time_gain_hours = learning_time_gain_hours;
98 nox_tuning_params_.value().gating_max_duration_minutes = gating_max_duration_minutes;
99 nox_tuning_params_.value().std_initial = 50;
100 nox_tuning_params_.value().gain_factor = gain_factor;
101 }
102
103 protected:
104 void self_test_();
105
110
111 void update_gas_indices_();
112 void measure_raw_();
113 uint16_t voc_sraw_;
114 uint16_t nox_sraw_;
115
118 uint16_t featureset_;
119
122
124 VOCGasIndexAlgorithm voc_algorithm_;
128 int32_t voc_index_ = 0;
129
131 int32_t nox_index_ = 0;
132 NOxGasIndexAlgorithm nox_algorithm_;
134
136 uint8_t samples_read_ = 0;
137 uint8_t samples_to_stabilize_ = static_cast<int8_t>(GasIndexAlgorithm_INITIAL_BLACKOUT) * 2;
138
143};
144} // namespace sgp4x
145} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:301
Implementation of a i2c functions for Sensirion sensors Sensirion data requires crc checking.
Base-class for all sensors.
Definition sensor.h:57
This class implements support for the Sensirion sgp4x i2c GAS (VOC) sensors.
Definition sgp4x.h:58
void set_temperature_sensor(sensor::Sensor *temperature)
Definition sgp4x.h:72
SGP4xBaselines voc_baselines_storage_
Definition sgp4x.h:142
void set_nox_sensor(sensor::Sensor *nox_sensor)
Definition sgp4x.h:81
void set_humidity_sensor(sensor::Sensor *humidity)
Definition sgp4x.h:71
float get_setup_priority() const override
Definition sgp4x.h:78
ESPPreferenceObject pref_
Definition sgp4x.h:140
void dump_config() override
Definition sgp4x.cpp:270
sensor::Sensor * humidity_sensor_
Input sensor for humidity and temperature compensation.
Definition sgp4x.h:107
void set_voc_sensor(sensor::Sensor *voc_sensor)
Definition sgp4x.h:80
sensor::Sensor * voc_sensor_
Definition sgp4x.h:123
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:92
VOCGasIndexAlgorithm voc_algorithm_
Definition sgp4x.h:124
void set_store_baseline(bool store_baseline)
Definition sgp4x.h:79
sensor::Sensor * temperature_sensor_
Definition sgp4x.h:108
optional< GasTuning > voc_tuning_params_
Definition sgp4x.h:125
NOxGasIndexAlgorithm nox_algorithm_
Definition sgp4x.h:132
optional< GasTuning > nox_tuning_params_
Definition sgp4x.h:133
sensor::Sensor * nox_sensor_
Definition sgp4x.h:130
void set_voc_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 std_initial, uint16_t gain_factor)
Definition sgp4x.h:82
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
Definition sgp4x.h:47
struct esphome::sgp4x::SGP4xBaselines PACKED
const float MAXIMUM_STORAGE_DIFF
Definition sgp4x.h:53
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t gating_max_duration_minutes
Definition sgp4x.h:28
uint16_t learning_time_gain_hours
Definition sgp4x.h:27
uint16_t learning_time_offset_hours
Definition sgp4x.h:26
uint16_t temperature
Definition sun_gtil2.cpp:12