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