ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
pm2005.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "pm2005.h"
3
4namespace esphome::pm2005 {
5
6static const char *const TAG = "pm2005";
7
8// Converts a sensor situation to a human readable string
9static const LogString *pm2005_get_situation_string(int status) {
10 switch (status) {
11 case 1:
12 return LOG_STR("Close");
13 case 2:
14 return LOG_STR("Malfunction");
15 case 3:
16 return LOG_STR("Under detecting");
17 case 0x80:
18 return LOG_STR("Detecting completed");
19 default:
20 return LOG_STR("Invalid");
21 }
22}
23
24// Converts a sensor measuring mode to a human readable string
25static const LogString *pm2005_get_measuring_mode_string(int status) {
26 switch (status) {
27 case 2:
28 return LOG_STR("Single");
29 case 3:
30 return LOG_STR("Continuous");
31 case 5:
32 return LOG_STR("Dynamic");
33 default:
34 return LOG_STR("Timing");
35 }
36}
37
38static inline uint16_t get_sensor_value(const uint8_t *data, uint8_t i) { return data[i] * 0x100 + data[i + 1]; }
39
41 if (this->sensor_type_ == PM2005) {
42 this->situation_value_index_ = 3;
43 this->pm_1_0_value_index_ = 4;
44 this->pm_2_5_value_index_ = 6;
45 this->pm_10_0_value_index_ = 8;
46 this->measuring_value_index_ = 10;
47 } else {
48 this->situation_value_index_ = 2;
49 this->pm_1_0_value_index_ = 3;
50 this->pm_2_5_value_index_ = 5;
51 this->pm_10_0_value_index_ = 7;
52 this->measuring_value_index_ = 9;
53 }
54
55 if (this->read(this->data_buffer_, 12) != i2c::ERROR_OK) {
56 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
57 this->mark_failed();
58 return;
59 }
60}
61
63 if (this->read(this->data_buffer_, 12) != i2c::ERROR_OK) {
64 ESP_LOGW(TAG, "Read result failed");
65 this->status_set_warning();
66 return;
67 }
68
69 if (this->sensor_situation_ == this->data_buffer_[this->situation_value_index_]) {
70 return;
71 }
72
74 ESP_LOGD(TAG, "Sensor situation: %s.", LOG_STR_ARG(pm2005_get_situation_string(this->sensor_situation_)));
75 if (this->sensor_situation_ == 2) {
76 this->status_set_warning();
77 return;
78 }
79 if (this->sensor_situation_ != 0x80) {
80 return;
81 }
82
83 const uint16_t pm1 = get_sensor_value(this->data_buffer_, this->pm_1_0_value_index_);
84 const uint16_t pm25 = get_sensor_value(this->data_buffer_, this->pm_2_5_value_index_);
85 const uint16_t pm10 = get_sensor_value(this->data_buffer_, this->pm_10_0_value_index_);
86 const uint16_t sensor_measuring_mode = get_sensor_value(this->data_buffer_, this->measuring_value_index_);
87 ESP_LOGD(TAG, "PM1.0: %d, PM2.5: %d, PM10: %d, Measuring mode: %s.", pm1, pm25, pm10,
88 LOG_STR_ARG(pm2005_get_measuring_mode_string(sensor_measuring_mode)));
89
90 if (this->pm_1_0_sensor_ != nullptr) {
91 this->pm_1_0_sensor_->publish_state(pm1);
92 }
93 if (this->pm_2_5_sensor_ != nullptr) {
94 this->pm_2_5_sensor_->publish_state(pm25);
95 }
96 if (this->pm_10_0_sensor_ != nullptr) {
97 this->pm_10_0_sensor_->publish_state(pm10);
98 }
99
100 this->status_clear_warning();
101}
102
104 ESP_LOGCONFIG(TAG,
105 "PM2005:\n"
106 " Type: PM2%u05",
107 this->sensor_type_ == PM2105);
108
109 LOG_I2C_DEVICE(this);
110 if (this->is_failed()) {
111 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
112 }
113
114 LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_);
115 LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_);
116 LOG_SENSOR(" ", "PM10 ", this->pm_10_0_sensor_);
117}
118
119} // namespace esphome::pm2005
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
void status_clear_warning()
Definition component.h:306
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
sensor::Sensor * pm_10_0_sensor_
Definition pm2005.h:33
sensor::Sensor * pm_1_0_sensor_
Definition pm2005.h:31
sensor::Sensor * pm_2_5_sensor_
Definition pm2005.h:32
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14