ESPHome 2026.2.1
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
54 const uint32_t now = App.get_loop_component_start_time();
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 } else if (now - this->last_update_ < this->update_interval_) {
99 // Otherwise just leave the sensor powered up and come back when we hit the update
100 // time
101 return;
102 }
103
104 if (now - this->last_transmission_ >= 500) {
105 // last transmission too long ago. Reset RX index.
106 this->data_index_ = 0;
107 }
108
109 if (this->available() == 0)
110 return;
111
112 this->last_transmission_ = now;
113 while (this->available() != 0) {
114 this->read_byte(&this->data_[this->data_index_]);
115 auto check = this->check_byte_();
116 if (!check.has_value()) {
117 // finished
118 this->parse_data_();
119 this->data_index_ = 0;
120 this->last_update_ = now;
121 } else if (!*check) {
122 // wrong data
123 this->data_index_ = 0;
124 } else {
125 // next byte
126 this->data_index_++;
127 }
128 }
129}
130
132 const uint8_t index = this->data_index_;
133 const uint8_t byte = this->data_[index];
134
135 if (index == 0 || index == 1) {
136 const uint8_t start_char = index == 0 ? START_CHARACTER_1 : START_CHARACTER_2;
137 if (byte == start_char) {
138 return true;
139 }
140
141 ESP_LOGW(TAG, "Start character %u mismatch: 0x%02X != 0x%02X", index + 1, byte, START_CHARACTER_1);
142 return false;
143 }
144
145 if (index == 2) {
146 return true;
147 }
148
149 const uint16_t payload_length = this->get_16_bit_uint_(2);
150 if (index == 3) {
151 if (this->check_payload_length_(payload_length)) {
152 return true;
153 } else {
154 ESP_LOGW(TAG, "Payload length %u doesn't match. Are you using the correct PMSX003 type?", payload_length);
155 return false;
156 }
157 }
158
159 // start (16bit) + length (16bit) + DATA (payload_length - 16bit) + checksum (16bit)
160 const uint16_t total_size = 4 + payload_length;
161
162 if (index < total_size - 1) {
163 return true;
164 }
165
166 // checksum is without checksum bytes
167 uint16_t checksum = 0;
168 for (uint16_t i = 0; i < total_size - 2; i++) {
169 checksum += this->data_[i];
170 }
171
172 const uint16_t check = this->get_16_bit_uint_(total_size - 2);
173 if (checksum != check) {
174 ESP_LOGW(TAG, "PMSX003 checksum mismatch! 0x%02X != 0x%02X", checksum, check);
175 return false;
176 }
177
178 return {};
179}
180
181bool PMSX003Component::check_payload_length_(uint16_t payload_length) {
182 // https://avaldebe.github.io/PyPMS/sensors/Plantower/
183 switch (this->type_) {
184 case Type::PMS1003:
185 return payload_length == 28; // 2*13+2
186 case Type::PMS3003: // Data 7/8/9 not set/reserved
187 return payload_length == 20; // 2*9+2
188 case Type::PMSX003: // Data 13 not set/reserved
189 // Deprecated: Length 20 is for PMS3003 backwards compatibility
190 return payload_length == 28 || payload_length == 20; // 2*13+2
191 case Type::PMS5003S:
192 case Type::PMS5003T: // Data 13 not set/reserved
193 return payload_length == 28; // 2*13+2
194 case Type::PMS5003ST: // Data 16 not set/reserved
195 return payload_length == 36; // 2*17+2
196 case Type::PMS9003M:
197 return payload_length == 28; // 2*13+2
198 }
199 return false;
200}
201
202void PMSX003Component::send_command_(Command cmd, uint16_t data) {
203 uint8_t send_data[7] = {
204 START_CHARACTER_1, // Start Byte 1
205 START_CHARACTER_2, // Start Byte 2
206 static_cast<uint8_t>(cmd), // Command
207 uint8_t((data >> 8) & 0xFF), // Data 1
208 uint8_t((data >> 0) & 0xFF), // Data 2
209 0, // Verify Byte 1
210 0, // Verify Byte 2
211 };
212
213 // Calculate checksum
214 uint16_t checksum = 0;
215 for (uint8_t i = 0; i < 5; i++) {
216 checksum += send_data[i];
217 }
218 send_data[5] = (checksum >> 8) & 0xFF; // Verify Byte 1
219 send_data[6] = (checksum >> 0) & 0xFF; // Verify Byte 2
220
221 for (auto send_byte : send_data) {
222 this->write_byte(send_byte);
223 }
224}
225
227 // Particle Matter
228 const uint16_t pm_1_0_std_concentration = this->get_16_bit_uint_(4);
229 const uint16_t pm_2_5_std_concentration = this->get_16_bit_uint_(6);
230 const uint16_t pm_10_0_std_concentration = this->get_16_bit_uint_(8);
231
232 const uint16_t pm_1_0_concentration = this->get_16_bit_uint_(10);
233 const uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12);
234 const uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14);
235
236 const uint16_t pm_particles_03um = this->get_16_bit_uint_(16);
237 const uint16_t pm_particles_05um = this->get_16_bit_uint_(18);
238 const uint16_t pm_particles_10um = this->get_16_bit_uint_(20);
239 const uint16_t pm_particles_25um = this->get_16_bit_uint_(22);
240
241 ESP_LOGD(TAG,
242 "Got PM1.0 Standard Concentration: %u µg/m³, PM2.5 Standard Concentration %u µg/m³, PM10.0 Standard "
243 "Concentration: %u µg/m³, PM1.0 Concentration: %u µg/m³, PM2.5 Concentration %u µg/m³, PM10.0 "
244 "Concentration: %u µg/m³",
245 pm_1_0_std_concentration, pm_2_5_std_concentration, pm_10_0_std_concentration, pm_1_0_concentration,
246 pm_2_5_concentration, pm_10_0_concentration);
247
248 if (this->pm_1_0_std_sensor_ != nullptr)
249 this->pm_1_0_std_sensor_->publish_state(pm_1_0_std_concentration);
250 if (this->pm_2_5_std_sensor_ != nullptr)
251 this->pm_2_5_std_sensor_->publish_state(pm_2_5_std_concentration);
252 if (this->pm_10_0_std_sensor_ != nullptr)
253 this->pm_10_0_std_sensor_->publish_state(pm_10_0_std_concentration);
254
255 if (this->pm_1_0_sensor_ != nullptr)
256 this->pm_1_0_sensor_->publish_state(pm_1_0_concentration);
257 if (this->pm_2_5_sensor_ != nullptr)
258 this->pm_2_5_sensor_->publish_state(pm_2_5_concentration);
259 if (this->pm_10_0_sensor_ != nullptr)
260 this->pm_10_0_sensor_->publish_state(pm_10_0_concentration);
261
262 if (this->pm_particles_03um_sensor_ != nullptr)
263 this->pm_particles_03um_sensor_->publish_state(pm_particles_03um);
264 if (this->pm_particles_05um_sensor_ != nullptr)
265 this->pm_particles_05um_sensor_->publish_state(pm_particles_05um);
266 if (this->pm_particles_10um_sensor_ != nullptr)
267 this->pm_particles_10um_sensor_->publish_state(pm_particles_10um);
268 if (this->pm_particles_25um_sensor_ != nullptr)
269 this->pm_particles_25um_sensor_->publish_state(pm_particles_25um);
270
271 if (this->type_ == Type::PMS5003T) {
272 ESP_LOGD(TAG,
273 "Got PM0.3 Particles: %u Count/0.1L, PM0.5 Particles: %u Count/0.1L, PM1.0 Particles: %u Count/0.1L, "
274 "PM2.5 Particles %u Count/0.1L",
275 pm_particles_03um, pm_particles_05um, pm_particles_10um, pm_particles_25um);
276 } else {
277 // Note the pm particles 50um & 100um are not returned,
278 // as PMS5003T uses those data values for temperature and humidity.
279 const uint16_t pm_particles_50um = this->get_16_bit_uint_(24);
280 const uint16_t pm_particles_100um = this->get_16_bit_uint_(26);
281
282 ESP_LOGD(TAG,
283 "Got PM0.3 Particles: %u Count/0.1L, PM0.5 Particles: %u Count/0.1L, PM1.0 Particles: %u Count/0.1L, "
284 "PM2.5 Particles %u Count/0.1L, PM5.0 Particles: %u Count/0.1L, PM10.0 Particles %u Count/0.1L",
285 pm_particles_03um, pm_particles_05um, pm_particles_10um, pm_particles_25um, pm_particles_50um,
286 pm_particles_100um);
287
288 if (this->pm_particles_50um_sensor_ != nullptr)
289 this->pm_particles_50um_sensor_->publish_state(pm_particles_50um);
290 if (this->pm_particles_100um_sensor_ != nullptr)
291 this->pm_particles_100um_sensor_->publish_state(pm_particles_100um);
292 }
293
294 // Formaldehyde
295 if (this->type_ == Type::PMS5003S || this->type_ == Type::PMS5003ST) {
296 const uint16_t formaldehyde = this->get_16_bit_uint_(28);
297
298 ESP_LOGD(TAG, "Got Formaldehyde: %u µg/m^3", formaldehyde);
299
300 if (this->formaldehyde_sensor_ != nullptr)
301 this->formaldehyde_sensor_->publish_state(formaldehyde);
302 }
303
304 // Temperature and Humidity
305 if (this->type_ == Type::PMS5003T || this->type_ == Type::PMS5003ST) {
306 const uint8_t temperature_offset = (this->type_ == Type::PMS5003T) ? 24 : 30;
307
308 const float temperature = static_cast<int16_t>(this->get_16_bit_uint_(temperature_offset)) / 10.0f;
309 const float humidity = this->get_16_bit_uint_(temperature_offset + 2) / 10.0f;
310
311 ESP_LOGD(TAG, "Got Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
312
313 if (this->temperature_sensor_ != nullptr)
314 this->temperature_sensor_->publish_state(temperature);
315 if (this->humidity_sensor_ != nullptr)
316 this->humidity_sensor_->publish_state(humidity);
317 }
318
319 // Firmware Version and Error Code
320 if (this->type_ == Type::PMS1003 || this->type_ == Type::PMS5003ST || this->type_ == Type::PMS9003M) {
321 const uint8_t firmware_error_code_offset = (this->type_ == Type::PMS5003ST) ? 36 : 28;
322 const uint8_t firmware_version = this->data_[firmware_error_code_offset];
323 const uint8_t error_code = this->data_[firmware_error_code_offset + 1];
324
325 ESP_LOGD(TAG, "Got Firmware Version: 0x%02X, Error Code: 0x%02X", firmware_version, error_code);
326 }
327
328 // Spin down the sensor again if we aren't going to need it until more time has
329 // passed than it takes to stabilise
330 if (this->update_interval_ > STABILISING_MS) {
331 this->send_command_(Command::SLEEP_MODE, CMD_SLEEP_MODE_SLEEP);
332 this->state_ = State::IDLE;
333 }
334
335 this->status_clear_warning();
336}
337
338} // 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()
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:202
sensor::Sensor * pm_particles_50um_sensor_
Definition pmsx003.h:111
bool check_payload_length_(uint16_t payload_length)
Definition pmsx003.cpp:181
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.
uint16_t temperature
Definition sun_gtil2.cpp:12