ESPHome 2026.3.3
Loading...
Searching...
No Matches
pmsx003.cpp
Go to the documentation of this file.
1#include "pmsx003.h"
2#include "esphome/core/log.h"
4
5namespace esphome::pmsx003 {
6
7static const char *const TAG = "pmsx003";
8
9static const uint8_t START_CHARACTER_1 = 0x42;
10static const uint8_t START_CHARACTER_2 = 0x4D;
11
12static const uint16_t STABILISING_MS = 30000; // time taken for the sensor to become stable after power on in ms
13
14static const uint16_t CMD_MEASUREMENT_MODE_PASSIVE =
15 0x0000; // use `Command::MANUAL_MEASUREMENT` to trigger a measurement
16static const uint16_t CMD_MEASUREMENT_MODE_ACTIVE = 0x0001; // automatically perform measurements
17static const uint16_t CMD_SLEEP_MODE_SLEEP = 0x0000; // go to sleep mode
18static const uint16_t CMD_SLEEP_MODE_WAKEUP = 0x0001; // wake up from sleep mode
19
21
23 ESP_LOGCONFIG(TAG, "PMSX003:");
24 LOG_SENSOR(" ", "PM1.0STD", this->pm_1_0_std_sensor_);
25 LOG_SENSOR(" ", "PM2.5STD", this->pm_2_5_std_sensor_);
26 LOG_SENSOR(" ", "PM10.0STD", this->pm_10_0_std_sensor_);
27
28 LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_);
29 LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_);
30 LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_);
31
32 LOG_SENSOR(" ", "PM0.3um", this->pm_particles_03um_sensor_);
33 LOG_SENSOR(" ", "PM0.5um", this->pm_particles_05um_sensor_);
34 LOG_SENSOR(" ", "PM1.0um", this->pm_particles_10um_sensor_);
35 LOG_SENSOR(" ", "PM2.5um", this->pm_particles_25um_sensor_);
36 LOG_SENSOR(" ", "PM5.0um", this->pm_particles_50um_sensor_);
37 LOG_SENSOR(" ", "PM10.0um", this->pm_particles_100um_sensor_);
38
39 LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_);
40
41 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
42 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
43
44 if (this->update_interval_ <= STABILISING_MS) {
45 ESP_LOGCONFIG(TAG, " Mode: active continuous (sensor default)");
46 } else {
47 ESP_LOGCONFIG(TAG, " Mode: passive with sleep/wake cycles");
48 }
49
50 this->check_uart_settings(9600);
51}
52
55
56 // Initialize sensor mode on first loop
57 if (!this->initialised_) {
58 if (this->update_interval_ > STABILISING_MS) {
59 // Long update interval: use passive mode with sleep/wake cycles
60 this->send_command_(Command::MEASUREMENT_MODE, CMD_MEASUREMENT_MODE_PASSIVE);
61 this->send_command_(Command::SLEEP_MODE, CMD_SLEEP_MODE_WAKEUP);
62 } else {
63 // Short/zero update interval: use active continuous mode
64 this->send_command_(Command::MEASUREMENT_MODE, CMD_MEASUREMENT_MODE_ACTIVE);
65 }
66 this->initialised_ = true;
67 }
68
69 // If we update less often than it takes the device to stabilise, spin the fan down
70 // rather than running it constantly. It does take some time to stabilise, so we
71 // need to keep track of what state we're in.
72 if (this->update_interval_ > STABILISING_MS) {
73 switch (this->state_) {
74 case State::IDLE:
75 // Power on the sensor now so it'll be ready when we hit the update time
76 if (now - this->last_update_ < (this->update_interval_ - STABILISING_MS))
77 return;
78
80 this->send_command_(Command::SLEEP_MODE, CMD_SLEEP_MODE_WAKEUP);
81 this->fan_on_time_ = now;
82 return;
84 // wait for the sensor to be stable
85 if (now - this->fan_on_time_ < STABILISING_MS)
86 return;
87 // consume any command responses that are in the serial buffer
88 while (this->available())
89 this->read_byte(&this->data_[0]);
90 // Trigger a new read
92 this->state_ = State::WAITING;
93 break;
94 case State::WAITING:
95 // Just go ahead and read stuff
96 break;
97 }
98 }
99
100 if (now - this->last_transmission_ >= 500) {
101 // last transmission too long ago. Reset RX index.
102 this->data_index_ = 0;
103 }
104
105 if (this->available() == 0)
106 return;
107
108 this->last_transmission_ = now;
109 while (this->available() != 0) {
110 this->read_byte(&this->data_[this->data_index_]);
111 auto check = this->check_byte_();
112 if (!check.has_value()) {
113 if (this->update_interval_ > STABILISING_MS || now - this->last_update_ >= this->update_interval_) {
114 this->parse_data_();
115 this->last_update_ = now;
116 }
117 this->data_index_ = 0;
118 } else if (!*check) {
119 // wrong data
120 this->data_index_ = 0;
121 } else {
122 // next byte
123 this->data_index_++;
124 }
125 }
126}
127
129 const uint8_t index = this->data_index_;
130 const uint8_t byte = this->data_[index];
131
132 if (index == 0 || index == 1) {
133 const uint8_t start_char = index == 0 ? START_CHARACTER_1 : START_CHARACTER_2;
134 if (byte == start_char) {
135 return true;
136 }
137
138 ESP_LOGW(TAG, "Start character %u mismatch: 0x%02X != 0x%02X", index + 1, byte, start_char);
139 return false;
140 }
141
142 if (index == 2) {
143 return true;
144 }
145
146 const uint16_t payload_length = this->get_16_bit_uint_(2);
147 if (index == 3) {
148 if (this->check_payload_length_(payload_length)) {
149 return true;
150 } else {
151 ESP_LOGW(TAG, "Payload length %u doesn't match. Are you using the correct PMSX003 type?", payload_length);
152 return false;
153 }
154 }
155
156 // start (16bit) + length (16bit) + DATA (payload_length - 16bit) + checksum (16bit)
157 const uint16_t total_size = 4 + payload_length;
158
159 if (index < total_size - 1) {
160 return true;
161 }
162
163 // checksum is without checksum bytes
164 uint16_t checksum = 0;
165 for (uint16_t i = 0; i < total_size - 2; i++) {
166 checksum += this->data_[i];
167 }
168
169 const uint16_t check = this->get_16_bit_uint_(total_size - 2);
170 if (checksum != check) {
171 ESP_LOGW(TAG, "PMSX003 checksum mismatch! 0x%02X != 0x%02X", checksum, check);
172 return false;
173 }
174
175 return {};
176}
177
178bool PMSX003Component::check_payload_length_(uint16_t payload_length) {
179 // https://avaldebe.github.io/PyPMS/sensors/Plantower/
180 switch (this->type_) {
181 case Type::PMS1003:
182 return payload_length == 28; // 2*13+2
183 case Type::PMS3003: // Data 7/8/9 not set/reserved
184 return payload_length == 20; // 2*9+2
185 case Type::PMSX003: // Data 13 not set/reserved
186 // Deprecated: Length 20 is for PMS3003 backwards compatibility
187 return payload_length == 28 || payload_length == 20; // 2*13+2
188 case Type::PMS5003S:
189 case Type::PMS5003T: // Data 13 not set/reserved
190 return payload_length == 28; // 2*13+2
191 case Type::PMS5003ST: // Data 16 not set/reserved
192 return payload_length == 36; // 2*17+2
193 case Type::PMS9003M:
194 return payload_length == 28; // 2*13+2
195 }
196 return false;
197}
198
199void PMSX003Component::send_command_(Command cmd, uint16_t data) {
200 uint8_t send_data[7] = {
201 START_CHARACTER_1, // Start Byte 1
202 START_CHARACTER_2, // Start Byte 2
203 static_cast<uint8_t>(cmd), // Command
204 uint8_t((data >> 8) & 0xFF), // Data 1
205 uint8_t((data >> 0) & 0xFF), // Data 2
206 0, // Verify Byte 1
207 0, // Verify Byte 2
208 };
209
210 // Calculate checksum
211 uint16_t checksum = 0;
212 for (uint8_t i = 0; i < 5; i++) {
213 checksum += send_data[i];
214 }
215 send_data[5] = (checksum >> 8) & 0xFF; // Verify Byte 1
216 send_data[6] = (checksum >> 0) & 0xFF; // Verify Byte 2
217
218 for (auto send_byte : send_data) {
219 this->write_byte(send_byte);
220 }
221}
222
224 // Particle Matter
225 const uint16_t pm_1_0_std_concentration = this->get_16_bit_uint_(4);
226 const uint16_t pm_2_5_std_concentration = this->get_16_bit_uint_(6);
227 const uint16_t pm_10_0_std_concentration = this->get_16_bit_uint_(8);
228
229 const uint16_t pm_1_0_concentration = this->get_16_bit_uint_(10);
230 const uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12);
231 const uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14);
232
233 const uint16_t pm_particles_03um = this->get_16_bit_uint_(16);
234 const uint16_t pm_particles_05um = this->get_16_bit_uint_(18);
235 const uint16_t pm_particles_10um = this->get_16_bit_uint_(20);
236 const uint16_t pm_particles_25um = this->get_16_bit_uint_(22);
237
238 ESP_LOGD(TAG,
239 "Got PM1.0 Standard Concentration: %u µg/m³, PM2.5 Standard Concentration %u µg/m³, PM10.0 Standard "
240 "Concentration: %u µg/m³, PM1.0 Concentration: %u µg/m³, PM2.5 Concentration %u µg/m³, PM10.0 "
241 "Concentration: %u µg/m³",
242 pm_1_0_std_concentration, pm_2_5_std_concentration, pm_10_0_std_concentration, pm_1_0_concentration,
243 pm_2_5_concentration, pm_10_0_concentration);
244
245 if (this->pm_1_0_std_sensor_ != nullptr)
246 this->pm_1_0_std_sensor_->publish_state(pm_1_0_std_concentration);
247 if (this->pm_2_5_std_sensor_ != nullptr)
248 this->pm_2_5_std_sensor_->publish_state(pm_2_5_std_concentration);
249 if (this->pm_10_0_std_sensor_ != nullptr)
250 this->pm_10_0_std_sensor_->publish_state(pm_10_0_std_concentration);
251
252 if (this->pm_1_0_sensor_ != nullptr)
253 this->pm_1_0_sensor_->publish_state(pm_1_0_concentration);
254 if (this->pm_2_5_sensor_ != nullptr)
255 this->pm_2_5_sensor_->publish_state(pm_2_5_concentration);
256 if (this->pm_10_0_sensor_ != nullptr)
257 this->pm_10_0_sensor_->publish_state(pm_10_0_concentration);
258
259 if (this->pm_particles_03um_sensor_ != nullptr)
260 this->pm_particles_03um_sensor_->publish_state(pm_particles_03um);
261 if (this->pm_particles_05um_sensor_ != nullptr)
262 this->pm_particles_05um_sensor_->publish_state(pm_particles_05um);
263 if (this->pm_particles_10um_sensor_ != nullptr)
264 this->pm_particles_10um_sensor_->publish_state(pm_particles_10um);
265 if (this->pm_particles_25um_sensor_ != nullptr)
266 this->pm_particles_25um_sensor_->publish_state(pm_particles_25um);
267
268 if (this->type_ == Type::PMS5003T) {
269 ESP_LOGD(TAG,
270 "Got PM0.3 Particles: %u Count/0.1L, PM0.5 Particles: %u Count/0.1L, PM1.0 Particles: %u Count/0.1L, "
271 "PM2.5 Particles %u Count/0.1L",
272 pm_particles_03um, pm_particles_05um, pm_particles_10um, pm_particles_25um);
273 } else {
274 // Note the pm particles 50um & 100um are not returned,
275 // as PMS5003T uses those data values for temperature and humidity.
276 const uint16_t pm_particles_50um = this->get_16_bit_uint_(24);
277 const uint16_t pm_particles_100um = this->get_16_bit_uint_(26);
278
279 ESP_LOGD(TAG,
280 "Got PM0.3 Particles: %u Count/0.1L, PM0.5 Particles: %u Count/0.1L, PM1.0 Particles: %u Count/0.1L, "
281 "PM2.5 Particles %u Count/0.1L, PM5.0 Particles: %u Count/0.1L, PM10.0 Particles %u Count/0.1L",
282 pm_particles_03um, pm_particles_05um, pm_particles_10um, pm_particles_25um, pm_particles_50um,
283 pm_particles_100um);
284
285 if (this->pm_particles_50um_sensor_ != nullptr)
286 this->pm_particles_50um_sensor_->publish_state(pm_particles_50um);
287 if (this->pm_particles_100um_sensor_ != nullptr)
288 this->pm_particles_100um_sensor_->publish_state(pm_particles_100um);
289 }
290
291 // Formaldehyde
292 if (this->type_ == Type::PMS5003S || this->type_ == Type::PMS5003ST) {
293 const uint16_t formaldehyde = this->get_16_bit_uint_(28);
294
295 ESP_LOGD(TAG, "Got Formaldehyde: %u µg/m^3", formaldehyde);
296
297 if (this->formaldehyde_sensor_ != nullptr)
298 this->formaldehyde_sensor_->publish_state(formaldehyde);
299 }
300
301 // Temperature and Humidity
302 if (this->type_ == Type::PMS5003T || this->type_ == Type::PMS5003ST) {
303 const uint8_t temperature_offset = (this->type_ == Type::PMS5003T) ? 24 : 30;
304
305 const float temperature = static_cast<int16_t>(this->get_16_bit_uint_(temperature_offset)) / 10.0f;
306 const float humidity = this->get_16_bit_uint_(temperature_offset + 2) / 10.0f;
307
308 ESP_LOGD(TAG, "Got Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
309
310 if (this->temperature_sensor_ != nullptr)
311 this->temperature_sensor_->publish_state(temperature);
312 if (this->humidity_sensor_ != nullptr)
313 this->humidity_sensor_->publish_state(humidity);
314 }
315
316 // Firmware Version and Error Code
317 if (this->type_ == Type::PMS1003 || this->type_ == Type::PMS5003ST || this->type_ == Type::PMS9003M) {
318 const uint8_t firmware_error_code_offset = (this->type_ == Type::PMS5003ST) ? 36 : 28;
319 const uint8_t firmware_version = this->data_[firmware_error_code_offset];
320 const uint8_t error_code = this->data_[firmware_error_code_offset + 1];
321
322 ESP_LOGD(TAG, "Got Firmware Version: 0x%02X, Error Code: 0x%02X", firmware_version, error_code);
323 }
324
325 // Spin down the sensor again if we aren't going to need it until more time has
326 // passed than it takes to stabilise
327 if (this->update_interval_ > STABILISING_MS) {
328 this->send_command_(Command::SLEEP_MODE, CMD_SLEEP_MODE_SLEEP);
329 this->state_ = State::IDLE;
330 }
331
332 this->status_clear_warning();
333}
334
335} // namespace esphome::pmsx003
uint8_t checksum
Definition bl0906.h:3
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void status_clear_warning()
Definition component.h:254
sensor::Sensor * pm_10_0_sensor_
Definition pmsx003.h:104
sensor::Sensor * pm_1_0_std_sensor_
Definition pmsx003.h:97
sensor::Sensor * pm_particles_100um_sensor_
Definition pmsx003.h:112
sensor::Sensor * pm_2_5_sensor_
Definition pmsx003.h:103
sensor::Sensor * pm_particles_03um_sensor_
Definition pmsx003.h:107
sensor::Sensor * formaldehyde_sensor_
Definition pmsx003.h:115
sensor::Sensor * pm_2_5_std_sensor_
Definition pmsx003.h:98
sensor::Sensor * humidity_sensor_
Definition pmsx003.h:119
sensor::Sensor * pm_1_0_sensor_
Definition pmsx003.h:102
sensor::Sensor * pm_particles_10um_sensor_
Definition pmsx003.h:109
sensor::Sensor * pm_10_0_std_sensor_
Definition pmsx003.h:99
sensor::Sensor * pm_particles_25um_sensor_
Definition pmsx003.h:110
sensor::Sensor * pm_particles_05um_sensor_
Definition pmsx003.h:108
sensor::Sensor * temperature_sensor_
Definition pmsx003.h:118
uint16_t get_16_bit_uint_(uint8_t start_index) const
Definition pmsx003.h:82
void send_command_(Command cmd, uint16_t data)
Definition pmsx003.cpp:199
sensor::Sensor * pm_particles_50um_sensor_
Definition pmsx003.h:111
bool check_payload_length_(uint16_t payload_length)
Definition pmsx003.cpp:178
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:16
bool read_byte(uint8_t *data)
Definition uart.h:34
void write_byte(uint8_t data)
Definition uart.h:18
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12