ESPHome 2026.11.0b6
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
6ESPHOME_LOG_TAG(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 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
57 return;
58 }
59}
60
62 if (this->read(this->data_buffer_, 12) != i2c::ERROR_OK) {
63 ESP_LOGW(TAG, "Read result failed");
64 this->status_set_warning();
65 return;
66 }
67
68 if (this->sensor_situation_ == this->data_buffer_[this->situation_value_index_]) {
69 return;
70 }
71
72 this->sensor_situation_ = this->data_buffer_[this->situation_value_index_];
73 ESP_LOGD(TAG, "Sensor situation: %s.", LOG_STR_ARG(pm2005_get_situation_string(this->sensor_situation_)));
74 if (this->sensor_situation_ == 2) {
75 this->status_set_warning();
76 return;
77 }
78 if (this->sensor_situation_ != 0x80) {
79 return;
80 }
81
82 const uint16_t pm1 = get_sensor_value(this->data_buffer_, this->pm_1_0_value_index_);
83 const uint16_t pm25 = get_sensor_value(this->data_buffer_, this->pm_2_5_value_index_);
84 const uint16_t pm10 = get_sensor_value(this->data_buffer_, this->pm_10_0_value_index_);
85 const uint16_t sensor_measuring_mode = get_sensor_value(this->data_buffer_, this->measuring_value_index_);
86 ESP_LOGD(TAG, "PM1.0: %d, PM2.5: %d, PM10: %d, Measuring mode: %s.", pm1, pm25, pm10,
87 LOG_STR_ARG(pm2005_get_measuring_mode_string(sensor_measuring_mode)));
88
89 PUBLISH_STATE(this->pm_1_0_sensor_, pm1);
90 PUBLISH_STATE(this->pm_2_5_sensor_, pm25);
91 PUBLISH_STATE(this->pm_10_0_sensor_, pm10);
92
94}
95
97 ESP_LOGCONFIG(TAG,
98 "PM2005:\n"
99 " Type: PM2%u05",
100 this->sensor_type_ == PM2105);
101
102 LOG_I2C_DEVICE(this);
103
104 LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_);
105 LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_);
106 LOG_SENSOR(" ", "PM10 ", this->pm_10_0_sensor_);
107}
108
109} // namespace esphome::pm2005
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:309
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
SUB_SENSOR(pm_1_0) SUB_SENSOR(pm_2_5) SUB_SENSOR(pm_10_0) protected uint8_t pm_1_0_value_index_
Definition pm2005.h:32
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
std::span< const uint8_t > data
ESPHOME_LOG_TAG(TAG, "pm2005")