ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
d01.cpp
Go to the documentation of this file.
1#include "d01.h"
2#include "esphome/core/log.h"
3
4// uart specification for d01 sensor from https://manuals.plus/ae/1005006417362019:
5//
6// A frame of serial output data includes 4 bytes, formatted as follows:
7// __Characteristic byte: Fixed value 0xA5.
8// __Data byte: DATAH is the high 7 bits of the concentration value, and DATAL is the low 7 bits of the concentration
9// value.
10// __Check byte: The low 7 bits of the sum of all bytes before the check byte.
11//
12// If the serial output is 4 bytes of data: 0*A5 0*01 0*2C 0*52, then DATAH = 0*01 = 1, DATAL = 0*2C = 44.
13// Concentration value = 1*128 + 44 = 172 µg/m³.
14//
15// The PM2.5 dust concentration value obtained from the dust sensor needs to be calibrated with a K value coefficient
16// based on the TSI instrument's photometric method. It is generally recommended to use 0.4.
17
18namespace esphome::d01 {
19
20static const char *const TAG = "d01";
21
22static const uint8_t D01_FRAME_HEADER = 0xA5;
23
24void D01SensorComponent::dump_config() { LOG_SENSOR(" ", "D01 PM2.5", this); }
25
27 uint8_t buf[4];
28 while (this->available() >= 4) {
29 if (this->peek() != D01_FRAME_HEADER) {
30 this->read();
31 continue;
32 }
33 this->read_array(buf, 4);
34 uint8_t sum = (buf[0] + buf[1] + buf[2]) & 0x7F;
35 if (sum != buf[3]) {
36 ESP_LOGW(TAG, "checksum mismatch");
37 continue;
38 }
39 uint16_t latest_concentration = (buf[1] & 0x7F) * 128 + (buf[2] & 0x7F);
40 ESP_LOGV(TAG, "Unadjusted PM2.5 Concentration: %d µg/m³", latest_concentration);
41 this->publish_state(latest_concentration);
42 }
43}
44
45} // namespace esphome::d01
void dump_config() override
Definition d01.cpp:24
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:39