ESPHome  2024.10.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  int16_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  GasTuning tuning_params;
74  tuning_params.index_offset = index_offset;
75  tuning_params.learning_time_offset_hours = learning_time_offset_hours;
76  tuning_params.learning_time_gain_hours = learning_time_gain_hours;
77  tuning_params.gating_max_duration_minutes = gating_max_duration_minutes;
78  tuning_params.std_initial = std_initial;
79  tuning_params.gain_factor = gain_factor;
80  voc_tuning_params_ = tuning_params;
81  }
82  void set_nox_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 gain_factor) {
85  GasTuning tuning_params;
86  tuning_params.index_offset = index_offset;
87  tuning_params.learning_time_offset_hours = learning_time_offset_hours;
88  tuning_params.learning_time_gain_hours = learning_time_gain_hours;
89  tuning_params.gating_max_duration_minutes = gating_max_duration_minutes;
90  tuning_params.std_initial = 50;
91  tuning_params.gain_factor = gain_factor;
92  nox_tuning_params_ = tuning_params;
93  }
94  void set_temperature_compensation(float offset, float normalized_offset_slope, uint16_t time_constant) {
95  TemperatureCompensation temp_comp;
96  temp_comp.offset = offset * 200;
97  temp_comp.normalized_offset_slope = normalized_offset_slope * 10000;
98  temp_comp.time_constant = time_constant;
99  temperature_compensation_ = temp_comp;
100  }
101  bool start_fan_cleaning();
102 
103  protected:
104  bool write_tuning_parameters_(uint16_t i2c_command, const GasTuning &tuning);
105  bool write_temperature_compensation_(const TemperatureCompensation &compensation);
107  bool initialized_{false};
108  sensor::Sensor *pm_1_0_sensor_{nullptr};
109  sensor::Sensor *pm_2_5_sensor_{nullptr};
110  sensor::Sensor *pm_4_0_sensor_{nullptr};
111  sensor::Sensor *pm_10_0_sensor_{nullptr};
112  // SEN54 and SEN55 only
113  sensor::Sensor *temperature_sensor_{nullptr};
114  sensor::Sensor *humidity_sensor_{nullptr};
115  sensor::Sensor *voc_sensor_{nullptr};
116  // SEN55 only
117  sensor::Sensor *nox_sensor_{nullptr};
118 
119  std::string product_name_;
120  uint8_t serial_number_[4];
131 };
132 
133 } // namespace sen5x
134 } // namespace esphome
void setup()
Sen5xBaselines voc_baselines_storage_
Definition: sen5x.h:122
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
float get_setup_priority() const override
Definition: sen5x.h:51
optional< RhtAccelerationMode > acceleration_mode_
Definition: sen5x.h:126
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:129
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:283
void set_acceleration_mode(RhtAccelerationMode mode)
Definition: sen5x.h:68
optional< GasTuning > voc_tuning_params_
Definition: sen5x.h:128
optional< uint32_t > auto_cleaning_interval_
Definition: sen5x.h:127
void set_auto_cleaning_interval(uint32_t auto_cleaning_interval)
Definition: sen5x.h:69
ESPPreferenceObject pref_
Definition: sen5x.h:125
void set_humidity_sensor(sensor::Sensor *humidity_sensor)
Definition: sen5x.h:65
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
uint32_t seconds_since_last_store_
Definition: sen5x.h:124
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:17
void set_temperature_compensation(float offset, float normalized_offset_slope, uint16_t time_constant)
Definition: sen5x.h:94
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:82
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
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
optional< TemperatureCompensation > temperature_compensation_
Definition: sen5x.h:130
Base-class for all sensors.
Definition: sensor.h:57
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
Definition: sen5x.h:23
const uint32_t MAXIMUM_STORAGE_DIFF
Definition: sen5x.h:25