ESPHome  2022.9.2
sgp4x.h
Go to the documentation of this file.
1 #pragma once
2 
8 #include <VOCGasIndexAlgorithm.h>
9 #include <NOxGasIndexAlgorithm.h>
10 
11 #include <cmath>
12 
13 namespace esphome {
14 namespace sgp4x {
15 
17  int32_t state0;
18  int32_t state1;
19 } PACKED; // NOLINT
20 
21 enum SgpType { SGP40, SGP41 };
22 
23 struct GasTuning {
24  uint16_t index_offset;
28  uint16_t std_initial;
29  uint16_t gain_factor;
30 };
31 
32 // commands and constants
33 static const uint8_t SGP40_FEATURESET = 0x0020; // can measure VOC
34 static const uint8_t SGP41_FEATURESET = 0x0040; // can measure VOC and NOX
35 // Commands
36 static const uint16_t SGP4X_CMD_GET_SERIAL_ID = 0x3682;
37 static const uint16_t SGP4X_CMD_GET_FEATURESET = 0x202f;
38 static const uint16_t SGP4X_CMD_SELF_TEST = 0x280e;
39 static const uint16_t SGP40_CMD_MEASURE_RAW = 0x260F;
40 static const uint16_t SGP41_CMD_MEASURE_RAW = 0x2619;
41 static const uint16_t SGP41_CMD_NOX_CONDITIONING = 0x2612;
42 static const uint8_t SGP41_SUBCMD_NOX_CONDITIONING = 0x12;
43 
44 // Shortest time interval of 3H for storing baseline values.
45 // Prevents wear of the flash because of too many write operations
46 const uint32_t SHORTEST_BASELINE_STORE_INTERVAL = 10800;
47 static const uint16_t SPG40_SELFTEST_TIME = 250; // 250 ms for self test
48 static const uint16_t SPG41_SELFTEST_TIME = 320; // 320 ms for self test
49 static const uint16_t SGP40_MEASURE_TIME = 30;
50 static const uint16_t SGP41_MEASURE_TIME = 55;
51 // Store anyway if the baseline difference exceeds the max storage diff value
52 const float MAXIMUM_STORAGE_DIFF = 50.0f;
53 
54 class SGP4xComponent;
55 
58  enum ErrorCode {
61  INVALID_ID,
62  UNSUPPORTED_ID,
64  SELF_TEST_FAILED,
65  UNKNOWN
66  } error_code_{UNKNOWN};
67 
68  public:
69  // SGP4xComponent() {};
70  void set_humidity_sensor(sensor::Sensor *humidity) { humidity_sensor_ = humidity; }
72 
73  void setup() override;
74  void update() override;
75  void update_gas_indices();
76  void dump_config() override;
77  float get_setup_priority() const override { return setup_priority::DATA; }
78  void set_store_baseline(bool store_baseline) { store_baseline_ = store_baseline; }
79  void set_voc_sensor(sensor::Sensor *voc_sensor) { voc_sensor_ = voc_sensor; }
80  void set_nox_sensor(sensor::Sensor *nox_sensor) { nox_sensor_ = nox_sensor; }
81  void set_voc_algorithm_tuning(uint16_t index_offset, uint16_t learning_time_offset_hours,
82  uint16_t learning_time_gain_hours, uint16_t gating_max_duration_minutes,
83  uint16_t std_initial, uint16_t gain_factor) {
84  voc_tuning_params_.value().index_offset = index_offset;
85  voc_tuning_params_.value().learning_time_offset_hours = learning_time_offset_hours;
86  voc_tuning_params_.value().learning_time_gain_hours = learning_time_gain_hours;
87  voc_tuning_params_.value().gating_max_duration_minutes = gating_max_duration_minutes;
88  voc_tuning_params_.value().std_initial = std_initial;
89  voc_tuning_params_.value().gain_factor = gain_factor;
90  }
91  void set_nox_algorithm_tuning(uint16_t index_offset, uint16_t learning_time_offset_hours,
92  uint16_t learning_time_gain_hours, uint16_t gating_max_duration_minutes,
93  uint16_t gain_factor) {
94  nox_tuning_params_.value().index_offset = index_offset;
95  nox_tuning_params_.value().learning_time_offset_hours = learning_time_offset_hours;
96  nox_tuning_params_.value().learning_time_gain_hours = learning_time_gain_hours;
97  nox_tuning_params_.value().gating_max_duration_minutes = gating_max_duration_minutes;
98  nox_tuning_params_.value().std_initial = 50;
99  nox_tuning_params_.value().gain_factor = gain_factor;
100  }
101 
102  protected:
103  void self_test_();
104 
106  sensor::Sensor *humidity_sensor_{nullptr};
107  sensor::Sensor *temperature_sensor_{nullptr};
108  int16_t sensirion_init_sensors_();
109 
110  bool measure_gas_indices_(int32_t &voc, int32_t &nox);
111  bool measure_raw_(uint16_t &voc_raw, uint16_t &nox_raw);
112 
113  SgpType sgp_type_{SGP40};
114  uint64_t serial_number_;
115  uint16_t featureset_;
116 
118  uint16_t self_test_time_;
119 
120  sensor::Sensor *voc_sensor_{nullptr};
121  VOCGasIndexAlgorithm voc_algorithm_;
123  float voc_state0_;
124  float voc_state1_;
125  int32_t voc_index_ = 0;
126 
127  sensor::Sensor *nox_sensor_{nullptr};
128  int32_t nox_index_ = 0;
129  NOxGasIndexAlgorithm nox_algorithm_;
131 
132  uint16_t measure_time_;
133  uint8_t samples_read_ = 0;
134  uint8_t samples_to_stabilize_ = static_cast<int8_t>(GasIndexAlgorithm_INITIAL_BLACKOUT) * 2;
135 
140 };
141 } // namespace sgp4x
142 } // namespace esphome
void setup()
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
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:91
void set_humidity_sensor(sensor::Sensor *humidity)
Definition: sgp4x.h:70
const float MAXIMUM_STORAGE_DIFF
Definition: sgp4x.h:52
This class implements support for the Sensirion sgp4x i2c GAS (VOC) sensors.
Definition: sgp4x.h:57
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
Definition: sgp4x.h:46
VOCGasIndexAlgorithm voc_algorithm_
Definition: sgp4x.h:121
float temperature
Definition: qmp6988.h:71
optional< GasTuning > nox_tuning_params_
Definition: sgp4x.h:130
ESPPreferenceObject pref_
Definition: sgp4x.h:137
This class simplifies creating components that periodically check a state.
Definition: component.h:266
struct esphome::sgp4x::SGP4xBaselines PACKED
uint16_t std_initial
Definition: sgp4x.h:28
optional< GasTuning > voc_tuning_params_
Definition: sgp4x.h:122
float get_setup_priority() const override
Definition: sgp4x.h:77
uint16_t index_offset
Definition: sgp4x.h:24
uint16_t gating_max_duration_minutes
Definition: sgp4x.h:27
uint16_t learning_time_offset_hours
Definition: sgp4x.h:25
uint32_t seconds_since_last_store_
Definition: sgp4x.h:138
void set_store_baseline(bool store_baseline)
Definition: sgp4x.h:78
SGP4xBaselines voc_baselines_storage_
Definition: sgp4x.h:139
void set_nox_sensor(sensor::Sensor *nox_sensor)
Definition: sgp4x.h:80
NOxGasIndexAlgorithm nox_algorithm_
Definition: sgp4x.h:129
void set_voc_sensor(sensor::Sensor *voc_sensor)
Definition: sgp4x.h:79
Implementation of a i2c functions for Sensirion sensors Sensirion data requires crc checking...
Definition: i2c_sensirion.h:15
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:81
void set_temperature_sensor(sensor::Sensor *temperature)
Definition: sgp4x.h:71
uint16_t learning_time_gain_hours
Definition: sgp4x.h:26
Definition: a4988.cpp:4
uint16_t gain_factor
Definition: sgp4x.h:29
Base-class for all sensors.
Definition: sensor.h:48