ESPHome  2024.10.2
hm3301.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 #include "hm3301.h"
3 
4 namespace esphome {
5 namespace hm3301 {
6 
7 static const char *const TAG = "hm3301.sensor";
8 
9 static const uint8_t PM_1_0_VALUE_INDEX = 5;
10 static const uint8_t PM_2_5_VALUE_INDEX = 6;
11 static const uint8_t PM_10_0_VALUE_INDEX = 7;
12 
14  ESP_LOGCONFIG(TAG, "Setting up HM3301...");
15  if (i2c::ERROR_OK != this->write(&SELECT_COMM_CMD, 1)) {
16  error_code_ = ERROR_COMM;
17  this->mark_failed();
18  return;
19  }
20 }
21 
23  ESP_LOGCONFIG(TAG, "HM3301:");
24  LOG_I2C_DEVICE(this);
25  if (error_code_ == ERROR_COMM) {
26  ESP_LOGE(TAG, "Communication with HM3301 failed!");
27  }
28 
29  LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_);
30  LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_);
31  LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_);
32  LOG_SENSOR(" ", "AQI", this->aqi_sensor_);
33 }
34 
36 
38  if (this->read(data_buffer_, 29) != i2c::ERROR_OK) {
39  ESP_LOGW(TAG, "Read result failed");
40  this->status_set_warning();
41  return;
42  }
43 
44  if (!this->validate_checksum_(data_buffer_)) {
45  ESP_LOGW(TAG, "Checksum validation failed");
46  this->status_set_warning();
47  return;
48  }
49 
50  int16_t pm_1_0_value = -1;
51  if (this->pm_1_0_sensor_ != nullptr) {
52  pm_1_0_value = get_sensor_value_(data_buffer_, PM_1_0_VALUE_INDEX);
53  }
54 
55  int16_t pm_2_5_value = -1;
56  if (this->pm_2_5_sensor_ != nullptr) {
57  pm_2_5_value = get_sensor_value_(data_buffer_, PM_2_5_VALUE_INDEX);
58  }
59 
60  int16_t pm_10_0_value = -1;
61  if (this->pm_10_0_sensor_ != nullptr) {
62  pm_10_0_value = get_sensor_value_(data_buffer_, PM_10_0_VALUE_INDEX);
63  }
64 
65  int16_t aqi_value = -1;
66  if (this->aqi_sensor_ != nullptr && pm_2_5_value != -1 && pm_10_0_value != -1) {
68  aqi_value = calculator->get_aqi(pm_2_5_value, pm_10_0_value);
69  }
70 
71  if (pm_1_0_value != -1) {
72  this->pm_1_0_sensor_->publish_state(pm_1_0_value);
73  }
74  if (pm_2_5_value != -1) {
75  this->pm_2_5_sensor_->publish_state(pm_2_5_value);
76  }
77  if (pm_10_0_value != -1) {
78  this->pm_10_0_sensor_->publish_state(pm_10_0_value);
79  }
80  if (aqi_value != -1) {
81  this->aqi_sensor_->publish_state(aqi_value);
82  }
83 
84  this->status_clear_warning();
85 }
86 
87 bool HM3301Component::validate_checksum_(const uint8_t *data) {
88  uint8_t sum = 0;
89  for (int i = 0; i < 28; i++) {
90  sum += data[i];
91  }
92 
93  return sum == data[28];
94 }
95 
96 uint16_t HM3301Component::get_sensor_value_(const uint8_t *data, uint8_t i) {
97  return (uint16_t) data[i * 2] << 8 | data[i * 2 + 1];
98 }
99 
100 } // namespace hm3301
101 } // namespace esphome
AbstractAQICalculator * get_calculator(AQICalculatorType type)
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
AQICalculatorFactory aqi_calculator_factory_
Definition: hm3301.h:45
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
sensor::Sensor * pm_2_5_sensor_
Definition: hm3301.h:40
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
sensor::Sensor * pm_1_0_sensor_
Definition: hm3301.h:39
uint16_t get_sensor_value_(const uint8_t *data, uint8_t i)
Definition: hm3301.cpp:96
sensor::Sensor * pm_10_0_sensor_
Definition: hm3301.h:41
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
sensor::Sensor * aqi_sensor_
Definition: hm3301.h:42
No error found during execution of method.
Definition: i2c_bus.h:13
void status_clear_warning()
Definition: component.cpp:166
virtual uint16_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value)=0
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
bool validate_checksum_(const uint8_t *data)
Definition: hm3301.cpp:87
float get_setup_priority() const override
Definition: hm3301.cpp:35
AQICalculatorType aqi_calc_type_
Definition: hm3301.h:44
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void dump_config() override
Definition: hm3301.cpp:22