ESPHome  2022.5.1
sgp40.h
Go to the documentation of this file.
1 #pragma once
2 
9 
10 #include <cmath>
11 
12 namespace esphome {
13 namespace sgp40 {
14 
16  int32_t state0;
17  int32_t state1;
18 } PACKED; // NOLINT
19 
20 // commands and constants
21 static const uint8_t SGP40_FEATURESET = 0x0020;
22 static const uint8_t SGP40_CRC8_POLYNOMIAL = 0x31;
23 static const uint8_t SGP40_CRC8_INIT = 0xFF;
24 static const uint8_t SGP40_WORD_LEN = 2;
25 
26 // Commands
27 
28 static const uint16_t SGP40_CMD_GET_SERIAL_ID = 0x3682;
29 static const uint16_t SGP40_CMD_GET_FEATURESET = 0x202f;
30 static const uint16_t SGP40_CMD_SELF_TEST = 0x280e;
31 static const uint16_t SGP40_CMD_MEASURE_RAW = 0x260F;
32 
33 // Shortest time interval of 3H for storing baseline values.
34 // Prevents wear of the flash because of too many write operations
35 const uint32_t SHORTEST_BASELINE_STORE_INTERVAL = 10800;
36 
37 // Store anyway if the baseline difference exceeds the max storage diff value
38 const uint32_t MAXIMUM_STORAGE_DIFF = 50;
39 
40 class SGP40Component;
41 
44  public:
45  void set_humidity_sensor(sensor::Sensor *humidity) { humidity_sensor_ = humidity; }
47 
48  void setup() override;
49  void update() override;
50  void update_voc_index();
51  void dump_config() override;
52  float get_setup_priority() const override { return setup_priority::DATA; }
53  void set_store_baseline(bool store_baseline) { store_baseline_ = store_baseline; }
54 
55  protected:
57  sensor::Sensor *humidity_sensor_{nullptr};
58  sensor::Sensor *temperature_sensor_{nullptr};
59  int16_t sensirion_init_sensors_();
60  int16_t sgp40_probe_();
61  uint64_t serial_number_;
62  uint16_t featureset_;
63  int32_t measure_voc_index_();
64  uint8_t generate_crc_(const uint8_t *data, uint8_t datalen);
65  uint16_t measure_raw_();
72  int32_t state0_;
73  int32_t state1_;
74  int32_t voc_index_ = 0;
75  uint8_t samples_read_ = 0;
76  uint8_t samples_to_stabalize_ = static_cast<int8_t>(VOC_ALGORITHM_INITIAL_BLACKOUT) * 2;
77 
83  void self_test_();
84  enum ErrorCode {
89  UNKNOWN
90  } error_code_{UNKNOWN};
91 };
92 } // namespace sgp40
93 } // namespace esphome
void setup()
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
ESPPreferenceObject pref_
Definition: sgp40.h:66
float get_setup_priority() const override
Definition: sgp40.h:52
void set_store_baseline(bool store_baseline)
Definition: sgp40.h:53
void set_humidity_sensor(sensor::Sensor *humidity)
Definition: sgp40.h:45
float temperature
Definition: qmp6988.h:71
This class simplifies creating components that periodically check a state.
Definition: component.h:266
Struct to hold all the states of the VOC algorithm.
SGP40Baselines baselines_storage_
Definition: sgp40.h:68
struct esphome::sgp40::SGP40Baselines PACKED
Implementation of a i2c functions for Sensirion sensors Sensirion data requires crc checking...
Definition: i2c_sensirion.h:15
VocAlgorithmParams voc_algorithm_params_
Definition: sgp40.h:69
const uint32_t MAXIMUM_STORAGE_DIFF
Definition: sgp40.h:38
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
Definition: sgp40.h:35
Definition: a4988.cpp:4
This class implements support for the Sensirion sgp40 i2c GAS (VOC) sensors.
Definition: sgp40.h:43
void set_temperature_sensor(sensor::Sensor *temperature)
Definition: sgp40.h:46
Base-class for all sensors.
Definition: sensor.h:47
uint32_t seconds_since_last_store_
Definition: sgp40.h:67