ESPHome 2025.5.0
Loading...
Searching...
No Matches
hm3301.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "hm3301.h"
3
4namespace esphome {
5namespace hm3301 {
6
7static const char *const TAG = "hm3301.sensor";
8
9static const uint8_t PM_1_0_VALUE_INDEX = 5;
10static const uint8_t PM_2_5_VALUE_INDEX = 6;
11static 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
85}
86
87bool 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
96uint16_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
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
AbstractAQICalculator * get_calculator(AQICalculatorType type)
virtual uint16_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value)=0
AQICalculatorType aqi_calc_type_
Definition hm3301.h:44
uint16_t get_sensor_value_(const uint8_t *data, uint8_t i)
Definition hm3301.cpp:96
float get_setup_priority() const override
Definition hm3301.cpp:35
sensor::Sensor * aqi_sensor_
Definition hm3301.h:42
sensor::Sensor * pm_1_0_sensor_
Definition hm3301.h:39
sensor::Sensor * pm_2_5_sensor_
Definition hm3301.h:40
AQICalculatorFactory aqi_calculator_factory_
Definition hm3301.h:45
sensor::Sensor * pm_10_0_sensor_
Definition hm3301.h:41
bool validate_checksum_(const uint8_t *data)
Definition hm3301.cpp:87
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:190
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7