ESPHome  2022.9.2
sen5x.h
Go to the documentation of this file.
1 #pragma once
2 
8 
9 namespace esphome {
10 namespace sen5x {
11 
12 enum ERRORCODE {
19 };
20 
21 // Shortest time interval of 3H for storing baseline values.
22 // Prevents wear of the flash because of too many write operations
23 const uint32_t SHORTEST_BASELINE_STORE_INTERVAL = 10800;
24 // Store anyway if the baseline difference exceeds the max storage diff value
25 const uint32_t MAXIMUM_STORAGE_DIFF = 50;
26 
28  int32_t state0;
29  int32_t state1;
30 } PACKED; // NOLINT
31 
33 
34 struct GasTuning {
35  uint16_t index_offset;
39  uint16_t std_initial;
40  uint16_t gain_factor;
41 };
42 
44  uint16_t offset;
46  uint16_t time_constant;
47 };
48 
50  public:
51  float get_setup_priority() const override { return setup_priority::DATA; }
52  void setup() override;
53  void dump_config() override;
54  void update() override;
55 
56  enum Sen5xType { SEN50, SEN54, SEN55, UNKNOWN };
57 
58  void set_pm_1_0_sensor(sensor::Sensor *pm_1_0) { pm_1_0_sensor_ = pm_1_0; }
59  void set_pm_2_5_sensor(sensor::Sensor *pm_2_5) { pm_2_5_sensor_ = pm_2_5; }
60  void set_pm_4_0_sensor(sensor::Sensor *pm_4_0) { pm_4_0_sensor_ = pm_4_0; }
61  void set_pm_10_0_sensor(sensor::Sensor *pm_10_0) { pm_10_0_sensor_ = pm_10_0; }
62 
63  void set_voc_sensor(sensor::Sensor *voc_sensor) { voc_sensor_ = voc_sensor; }
64  void set_nox_sensor(sensor::Sensor *nox_sensor) { nox_sensor_ = nox_sensor; }
65  void set_humidity_sensor(sensor::Sensor *humidity_sensor) { humidity_sensor_ = humidity_sensor; }
66  void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
67  void set_store_baseline(bool store_baseline) { store_baseline_ = store_baseline; }
68  void set_acceleration_mode(RhtAccelerationMode mode) { acceleration_mode_ = mode; }
69  void set_auto_cleaning_interval(uint32_t auto_cleaning_interval) { auto_cleaning_interval_ = auto_cleaning_interval; }
70  void set_voc_algorithm_tuning(uint16_t index_offset, uint16_t learning_time_offset_hours,
71  uint16_t learning_time_gain_hours, uint16_t gating_max_duration_minutes,
72  uint16_t std_initial, uint16_t gain_factor) {
73  voc_tuning_params_.value().index_offset = index_offset;
74  voc_tuning_params_.value().learning_time_offset_hours = learning_time_offset_hours;
75  voc_tuning_params_.value().learning_time_gain_hours = learning_time_gain_hours;
76  voc_tuning_params_.value().gating_max_duration_minutes = gating_max_duration_minutes;
77  voc_tuning_params_.value().std_initial = std_initial;
78  voc_tuning_params_.value().gain_factor = gain_factor;
79  }
80  void set_nox_algorithm_tuning(uint16_t index_offset, uint16_t learning_time_offset_hours,
81  uint16_t learning_time_gain_hours, uint16_t gating_max_duration_minutes,
82  uint16_t gain_factor) {
83  nox_tuning_params_.value().index_offset = index_offset;
84  nox_tuning_params_.value().learning_time_offset_hours = learning_time_offset_hours;
85  nox_tuning_params_.value().learning_time_gain_hours = learning_time_gain_hours;
86  nox_tuning_params_.value().gating_max_duration_minutes = gating_max_duration_minutes;
87  nox_tuning_params_.value().std_initial = 50;
88  nox_tuning_params_.value().gain_factor = gain_factor;
89  }
90  void set_temperature_compensation(float offset, float normalized_offset_slope, uint16_t time_constant) {
91  temperature_compensation_.value().offset = offset * 200;
92  temperature_compensation_.value().normalized_offset_slope = normalized_offset_slope * 100;
93  temperature_compensation_.value().time_constant = time_constant;
94  }
95  bool start_fan_cleaning();
96 
97  protected:
98  bool write_tuning_parameters_(uint16_t i2c_command, const GasTuning &tuning);
99  bool write_temperature_compensation_(const TemperatureCompensation &compensation);
101  bool initialized_{false};
102  sensor::Sensor *pm_1_0_sensor_{nullptr};
103  sensor::Sensor *pm_2_5_sensor_{nullptr};
104  sensor::Sensor *pm_4_0_sensor_{nullptr};
105  sensor::Sensor *pm_10_0_sensor_{nullptr};
106  // SEN54 and SEN55 only
107  sensor::Sensor *temperature_sensor_{nullptr};
108  sensor::Sensor *humidity_sensor_{nullptr};
109  sensor::Sensor *voc_sensor_{nullptr};
110  // SEN55 only
111  sensor::Sensor *nox_sensor_{nullptr};
112 
113  std::string product_name_;
114  uint8_t serial_number_[4];
125 };
126 
127 } // namespace sen5x
128 } // namespace esphome
void setup()
Sen5xBaselines voc_baselines_storage_
Definition: sen5x.h:116
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
float get_setup_priority() const override
Definition: sen5x.h:51
optional< RhtAccelerationMode > acceleration_mode_
Definition: sen5x.h:120
RhtAccelerationMode
Definition: sen5x.h:32
struct esphome::sen5x::Sen5xBaselines PACKED
uint16_t index_offset
Definition: sen5x.h:35
optional< GasTuning > nox_tuning_params_
Definition: sen5x.h:123
uint16_t gating_max_duration_minutes
Definition: sen5x.h:38
uint16_t learning_time_gain_hours
Definition: sen5x.h:37
void set_temperature_sensor(sensor::Sensor *temperature_sensor)
Definition: sen5x.h:66
uint16_t learning_time_offset_hours
Definition: sen5x.h:36
void set_pm_4_0_sensor(sensor::Sensor *pm_4_0)
Definition: sen5x.h:60
This class simplifies creating components that periodically check a state.
Definition: component.h:266
void set_acceleration_mode(RhtAccelerationMode mode)
Definition: sen5x.h:68
optional< GasTuning > voc_tuning_params_
Definition: sen5x.h:122
optional< uint32_t > auto_cleaning_interval_
Definition: sen5x.h:121
void set_auto_cleaning_interval(uint32_t auto_cleaning_interval)
Definition: sen5x.h:69
ESPPreferenceObject pref_
Definition: sen5x.h:119
void set_humidity_sensor(sensor::Sensor *humidity_sensor)
Definition: sen5x.h:65
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
uint32_t seconds_since_last_store_
Definition: sen5x.h:118
void set_voc_sensor(sensor::Sensor *voc_sensor)
Definition: sen5x.h:63
uint16_t std_initial
Definition: sen5x.h:39
void set_store_baseline(bool store_baseline)
Definition: sen5x.h:67
uint16_t gain_factor
Definition: sen5x.h:40
Implementation of a i2c functions for Sensirion sensors Sensirion data requires crc checking...
Definition: i2c_sensirion.h:15
void set_temperature_compensation(float offset, float normalized_offset_slope, uint16_t time_constant)
Definition: sen5x.h:90
void set_nox_sensor(sensor::Sensor *nox_sensor)
Definition: sen5x.h:64
void set_pm_2_5_sensor(sensor::Sensor *pm_2_5)
Definition: sen5x.h:59
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: sen5x.h:80
void set_pm_10_0_sensor(sensor::Sensor *pm_10_0)
Definition: sen5x.h:61
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: sen5x.h:70
void set_pm_1_0_sensor(sensor::Sensor *pm_1_0)
Definition: sen5x.h:58
Definition: a4988.cpp:4
optional< TemperatureCompensation > temperature_compensation_
Definition: sen5x.h:124
Base-class for all sensors.
Definition: sensor.h:48
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
Definition: sen5x.h:23
const uint32_t MAXIMUM_STORAGE_DIFF
Definition: sen5x.h:25