ESPHome 2026.1.0-dev
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 {
6namespace pmsx003 {
7
8static const char *const TAG = "pmsx003";
9
10static const uint8_t START_CHARACTER_1 = 0x42;
11static const uint8_t START_CHARACTER_2 = 0x4D;
12
13static const uint16_t PMS_STABILISING_MS = 30000; // time taken for the sensor to become stable after power on in ms
14
15static const uint16_t PMS_CMD_MEASUREMENT_MODE_PASSIVE =
16 0x0000; // use `PMS_CMD_MANUAL_MEASUREMENT` to trigger a measurement
17static const uint16_t PMS_CMD_MEASUREMENT_MODE_ACTIVE = 0x0001; // automatically perform measurements
18static const uint16_t PMS_CMD_SLEEP_MODE_SLEEP = 0x0000; // go to sleep mode
19static const uint16_t PMS_CMD_SLEEP_MODE_WAKEUP = 0x0001; // wake up from sleep mode
20
22
24 ESP_LOGCONFIG(TAG, "PMSX003:");
25 LOG_SENSOR(" ", "PM1.0STD", this->pm_1_0_std_sensor_);
26 LOG_SENSOR(" ", "PM2.5STD", this->pm_2_5_std_sensor_);
27 LOG_SENSOR(" ", "PM10.0STD", this->pm_10_0_std_sensor_);
28
29 LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_);
30 LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_);
31 LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_);
32
33 LOG_SENSOR(" ", "PM0.3um", this->pm_particles_03um_sensor_);
34 LOG_SENSOR(" ", "PM0.5um", this->pm_particles_05um_sensor_);
35 LOG_SENSOR(" ", "PM1.0um", this->pm_particles_10um_sensor_);
36 LOG_SENSOR(" ", "PM2.5um", this->pm_particles_25um_sensor_);
37 LOG_SENSOR(" ", "PM5.0um", this->pm_particles_50um_sensor_);
38 LOG_SENSOR(" ", "PM10.0um", this->pm_particles_100um_sensor_);
39
40 LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_);
41
42 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
43 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
44
45 if (this->update_interval_ <= PMS_STABILISING_MS) {
46 ESP_LOGCONFIG(TAG, " Mode: active continuous (sensor default)");
47 } else {
48 ESP_LOGCONFIG(TAG, " Mode: passive with sleep/wake cycles");
49 }
50
51 this->check_uart_settings(9600);
52}
53
55 const uint32_t now = App.get_loop_component_start_time();
56
57 // Initialize sensor mode on first loop
58 if (this->initialised_ == 0) {
59 if (this->update_interval_ > PMS_STABILISING_MS) {
60 // Long update interval: use passive mode with sleep/wake cycles
61 this->send_command_(PMS_CMD_MEASUREMENT_MODE, PMS_CMD_MEASUREMENT_MODE_PASSIVE);
62 this->send_command_(PMS_CMD_SLEEP_MODE, PMS_CMD_SLEEP_MODE_WAKEUP);
63 } else {
64 // Short/zero update interval: use active continuous mode
65 this->send_command_(PMS_CMD_MEASUREMENT_MODE, PMS_CMD_MEASUREMENT_MODE_ACTIVE);
66 }
67 this->initialised_ = 1;
68 }
69
70 // If we update less often than it takes the device to stabilise, spin the fan down
71 // rather than running it constantly. It does take some time to stabilise, so we
72 // need to keep track of what state we're in.
73 if (this->update_interval_ > PMS_STABILISING_MS) {
74 switch (this->state_) {
76 // Power on the sensor now so it'll be ready when we hit the update time
77 if (now - this->last_update_ < (this->update_interval_ - PMS_STABILISING_MS))
78 return;
79
81 this->send_command_(PMS_CMD_SLEEP_MODE, PMS_CMD_SLEEP_MODE_WAKEUP);
82 this->fan_on_time_ = now;
83 return;
85 // wait for the sensor to be stable
86 if (now - this->fan_on_time_ < PMS_STABILISING_MS)
87 return;
88 // consume any command responses that are in the serial buffer
89 while (this->available())
90 this->read_byte(&this->data_[0]);
91 // Trigger a new read
94 break;
96 // Just go ahead and read stuff
97 break;
98 }
99 } else if (now - this->last_update_ < this->update_interval_) {
100 // Otherwise just leave the sensor powered up and come back when we hit the update
101 // time
102 return;
103 }
104
105 if (now - this->last_transmission_ >= 500) {
106 // last transmission too long ago. Reset RX index.
107 this->data_index_ = 0;
108 }
109
110 if (this->available() == 0)
111 return;
112
113 this->last_transmission_ = now;
114 while (this->available() != 0) {
115 this->read_byte(&this->data_[this->data_index_]);
116 auto check = this->check_byte_();
117 if (!check.has_value()) {
118 // finished
119 this->parse_data_();
120 this->data_index_ = 0;
121 this->last_update_ = now;
122 } else if (!*check) {
123 // wrong data
124 this->data_index_ = 0;
125 } else {
126 // next byte
127 this->data_index_++;
128 }
129 }
130}
131
133 const uint8_t index = this->data_index_;
134 const uint8_t byte = this->data_[index];
135
136 if (index == 0 || index == 1) {
137 const uint8_t start_char = index == 0 ? START_CHARACTER_1 : START_CHARACTER_2;
138 if (byte == start_char) {
139 return true;
140 }
141
142 ESP_LOGW(TAG, "Start character %u mismatch: 0x%02X != 0x%02X", index + 1, byte, START_CHARACTER_1);
143 return false;
144 }
145
146 if (index == 2) {
147 return true;
148 }
149
150 const uint16_t payload_length = this->get_16_bit_uint_(2);
151 if (index == 3) {
152 if (this->check_payload_length_(payload_length)) {
153 return true;
154 } else {
155 ESP_LOGW(TAG, "Payload length %u doesn't match. Are you using the correct PMSX003 type?", payload_length);
156 return false;
157 }
158 }
159
160 // start (16bit) + length (16bit) + DATA (payload_length - 16bit) + checksum (16bit)
161 const uint16_t total_size = 4 + payload_length;
162
163 if (index < total_size - 1) {
164 return true;
165 }
166
167 // checksum is without checksum bytes
168 uint16_t checksum = 0;
169 for (uint16_t i = 0; i < total_size - 2; i++) {
170 checksum += this->data_[i];
171 }
172
173 const uint16_t check = this->get_16_bit_uint_(total_size - 2);
174 if (checksum != check) {
175 ESP_LOGW(TAG, "PMSX003 checksum mismatch! 0x%02X != 0x%02X", checksum, check);
176 return false;
177 }
178
179 return {};
180}
181
182bool PMSX003Component::check_payload_length_(uint16_t payload_length) {
183 switch (this->type_) {
185 // The expected payload length is typically 28 bytes.
186 // However, a 20-byte payload check was already present in the code.
187 // No official documentation was found confirming this.
188 // Retaining this check to avoid breaking existing behavior.
189 return payload_length == 28 || payload_length == 20; // 2*13+2
192 return payload_length == 28; // 2*13+2 (Data 13 not set/reserved)
194 return payload_length == 36; // 2*17+2 (Data 16 not set/reserved)
195 }
196 return false;
197}
198
200 uint8_t send_data[7] = {
201 START_CHARACTER_1, // Start Byte 1
202 START_CHARACTER_2, // Start Byte 2
203 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 // Calculate and publish AQI if sensor is configured
269 if (this->aqi_sensor_ != nullptr) {
271 int32_t aqi_value = calculator->get_aqi(pm_2_5_concentration, pm_10_0_concentration);
272 this->aqi_sensor_->publish_state(aqi_value);
273 }
274
275 if (this->type_ == PMSX003_TYPE_5003T) {
276 ESP_LOGD(TAG,
277 "Got PM0.3 Particles: %u Count/0.1L, PM0.5 Particles: %u Count/0.1L, PM1.0 Particles: %u Count/0.1L, "
278 "PM2.5 Particles %u Count/0.1L",
279 pm_particles_03um, pm_particles_05um, pm_particles_10um, pm_particles_25um);
280 } else {
281 // Note the pm particles 50um & 100um are not returned,
282 // as PMS5003T uses those data values for temperature and humidity.
283 const uint16_t pm_particles_50um = this->get_16_bit_uint_(24);
284 const uint16_t pm_particles_100um = this->get_16_bit_uint_(26);
285
286 ESP_LOGD(TAG,
287 "Got PM0.3 Particles: %u Count/0.1L, PM0.5 Particles: %u Count/0.1L, PM1.0 Particles: %u Count/0.1L, "
288 "PM2.5 Particles %u Count/0.1L, PM5.0 Particles: %u Count/0.1L, PM10.0 Particles %u Count/0.1L",
289 pm_particles_03um, pm_particles_05um, pm_particles_10um, pm_particles_25um, pm_particles_50um,
290 pm_particles_100um);
291
292 if (this->pm_particles_50um_sensor_ != nullptr)
293 this->pm_particles_50um_sensor_->publish_state(pm_particles_50um);
294 if (this->pm_particles_100um_sensor_ != nullptr)
295 this->pm_particles_100um_sensor_->publish_state(pm_particles_100um);
296 }
297
298 // Formaldehyde
299 if (this->type_ == PMSX003_TYPE_5003ST || this->type_ == PMSX003_TYPE_5003S) {
300 const uint16_t formaldehyde = this->get_16_bit_uint_(28);
301
302 ESP_LOGD(TAG, "Got Formaldehyde: %u µg/m^3", formaldehyde);
303
304 if (this->formaldehyde_sensor_ != nullptr)
305 this->formaldehyde_sensor_->publish_state(formaldehyde);
306 }
307
308 // Temperature and Humidity
309 if (this->type_ == PMSX003_TYPE_5003ST || this->type_ == PMSX003_TYPE_5003T) {
310 const uint8_t temperature_offset = (this->type_ == PMSX003_TYPE_5003T) ? 24 : 30;
311
312 const float temperature = static_cast<int16_t>(this->get_16_bit_uint_(temperature_offset)) / 10.0f;
313 const float humidity = this->get_16_bit_uint_(temperature_offset + 2) / 10.0f;
314
315 ESP_LOGD(TAG, "Got Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
316
317 if (this->temperature_sensor_ != nullptr)
318 this->temperature_sensor_->publish_state(temperature);
319 if (this->humidity_sensor_ != nullptr)
320 this->humidity_sensor_->publish_state(humidity);
321 }
322
323 // Firmware Version and Error Code
324 if (this->type_ == PMSX003_TYPE_5003ST) {
325 const uint8_t firmware_version = this->data_[36];
326 const uint8_t error_code = this->data_[37];
327
328 ESP_LOGD(TAG, "Got Firmware Version: 0x%02X, Error Code: 0x%02X", firmware_version, error_code);
329 }
330
331 // Spin down the sensor again if we aren't going to need it until more time has
332 // passed than it takes to stabilise
333 if (this->update_interval_ > PMS_STABILISING_MS) {
334 this->send_command_(PMS_CMD_SLEEP_MODE, PMS_CMD_SLEEP_MODE_SLEEP);
336 }
337
338 this->status_clear_warning();
339}
340
341} // namespace pmsx003
342} // namespace esphome
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()
AbstractAQICalculator * get_calculator(AQICalculatorType type)
virtual uint16_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value)=0
sensor::Sensor * pm_10_0_sensor_
Definition pmsx003.h:108
sensor::Sensor * pm_1_0_std_sensor_
Definition pmsx003.h:101
sensor::Sensor * pm_particles_100um_sensor_
Definition pmsx003.h:116
sensor::Sensor * pm_2_5_sensor_
Definition pmsx003.h:107
sensor::Sensor * pm_particles_03um_sensor_
Definition pmsx003.h:111
sensor::Sensor * formaldehyde_sensor_
Definition pmsx003.h:119
sensor::Sensor * pm_2_5_std_sensor_
Definition pmsx003.h:102
sensor::Sensor * humidity_sensor_
Definition pmsx003.h:123
aqi::AQICalculatorFactory aqi_calculator_factory_
Definition pmsx003.h:129
sensor::Sensor * pm_1_0_sensor_
Definition pmsx003.h:106
sensor::Sensor * pm_particles_10um_sensor_
Definition pmsx003.h:113
sensor::Sensor * pm_10_0_std_sensor_
Definition pmsx003.h:103
sensor::Sensor * pm_particles_25um_sensor_
Definition pmsx003.h:114
aqi::AQICalculatorType aqi_calc_type_
Definition pmsx003.h:128
sensor::Sensor * pm_particles_05um_sensor_
Definition pmsx003.h:112
sensor::Sensor * temperature_sensor_
Definition pmsx003.h:122
uint16_t get_16_bit_uint_(uint8_t start_index) const
Definition pmsx003.h:86
sensor::Sensor * pm_particles_50um_sensor_
Definition pmsx003.h:115
void send_command_(PMSX0003Command cmd, uint16_t data)
Definition pmsx003.cpp:199
bool check_payload_length_(uint16_t payload_length)
Definition pmsx003.cpp:182
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:77
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:12
bool read_byte(uint8_t *data)
Definition uart.h:34
void write_byte(uint8_t data)
Definition uart.h:18
@ PMS_CMD_MEASUREMENT_MODE
Definition pmsx003.h:13
@ PMS_CMD_MANUAL_MEASUREMENT
Definition pmsx003.h:15
@ PMSX003_STATE_STABILISING
Definition pmsx003.h:28
@ PMSX003_STATE_WAITING
Definition pmsx003.h:29
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Application App
Global storage of Application pointer - only one Application can exist.
uint16_t temperature
Definition sun_gtil2.cpp:12