ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up PM2005...");
44
45 this->situation_value_index_ = 3;
46 this->pm_1_0_value_index_ = 4;
47 this->pm_2_5_value_index_ = 6;
48 this->pm_10_0_value_index_ = 8;
49 this->measuring_value_index_ = 10;
50 } else {
51 ESP_LOGCONFIG(TAG, "Setting up PM2105...");
52
53 this->situation_value_index_ = 2;
54 this->pm_1_0_value_index_ = 3;
55 this->pm_2_5_value_index_ = 5;
56 this->pm_10_0_value_index_ = 7;
57 this->measuring_value_index_ = 9;
58 }
59
60 if (this->read(this->data_buffer_, 12) != i2c::ERROR_OK) {
61 ESP_LOGE(TAG, "Communication failed!");
62 this->mark_failed();
63 return;
64 }
65}
66
68 if (this->read(this->data_buffer_, 12) != i2c::ERROR_OK) {
69 ESP_LOGW(TAG, "Read result failed.");
70 this->status_set_warning();
71 return;
72 }
73
74 if (this->sensor_situation_ == this->data_buffer_[this->situation_value_index_]) {
75 return;
76 }
77
79 ESP_LOGD(TAG, "Sensor situation: %s.", LOG_STR_ARG(pm2005_get_situation_string(this->sensor_situation_)));
80 if (this->sensor_situation_ == 2) {
81 this->status_set_warning();
82 return;
83 }
84 if (this->sensor_situation_ != 0x80) {
85 return;
86 }
87
88 uint16_t pm1 = get_sensor_value(this->data_buffer_, this->pm_1_0_value_index_);
89 uint16_t pm25 = get_sensor_value(this->data_buffer_, this->pm_2_5_value_index_);
90 uint16_t pm10 = get_sensor_value(this->data_buffer_, this->pm_10_0_value_index_);
91 uint16_t sensor_measuring_mode = get_sensor_value(this->data_buffer_, this->measuring_value_index_);
92 ESP_LOGD(TAG, "PM1.0: %d, PM2.5: %d, PM10: %d, Measuring mode: %s.", pm1, pm25, pm10,
93 LOG_STR_ARG(pm2005_get_measuring_mode_string(sensor_measuring_mode)));
94
95 if (this->pm_1_0_sensor_ != nullptr) {
96 this->pm_1_0_sensor_->publish_state(pm1);
97 }
98 if (this->pm_2_5_sensor_ != nullptr) {
99 this->pm_2_5_sensor_->publish_state(pm25);
100 }
101 if (this->pm_10_0_sensor_ != nullptr) {
102 this->pm_10_0_sensor_->publish_state(pm10);
103 }
104
105 this->status_clear_warning();
106}
107
109 ESP_LOGCONFIG(TAG, "PM2005:");
110 ESP_LOGCONFIG(TAG, " Type: PM2%u05", this->sensor_type_ == PM2105);
111
112 LOG_I2C_DEVICE(this);
113 if (this->is_failed()) {
114 ESP_LOGE(TAG, "Communication with PM2%u05 failed!", this->sensor_type_ == PM2105);
115 }
116
117 LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_);
118 LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_);
119 LOG_SENSOR(" ", "PM10 ", this->pm_10_0_sensor_);
120}
121
122} // namespace pm2005
123} // 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="unspecified")
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:39
@ 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