9static const uint8_t SDS011_MSG_REQUEST_LENGTH = 19;
10static const uint8_t SDS011_MSG_RESPONSE_LENGTH = 10;
11static const uint8_t SDS011_DATA_REQUEST_LENGTH = 15;
12static const uint8_t SDS011_DATA_RESPONSE_LENGTH = 6;
13static const uint8_t SDS011_MSG_HEAD = 0xaa;
14static const uint8_t SDS011_MSG_TAIL = 0xab;
15static const uint8_t SDS011_COMMAND_ID_REQUEST = 0xb4;
16static const uint8_t SDS011_COMMAND_ID_RESPONSE = 0xc5;
17static const uint8_t SDS011_COMMAND_ID_DATA = 0xc0;
18static const uint8_t SDS011_COMMAND_REPORT_MODE = 0x02;
19static const uint8_t SDS011_COMMAND_QUERY_DATA = 0x04;
20static const uint8_t SDS011_COMMAND_SET_DEVICE_ID = 0x05;
21static const uint8_t SDS011_COMMAND_SLEEP = 0x06;
22static const uint8_t SDS011_COMMAND_FIRMWARE = 0x07;
23static const uint8_t SDS011_COMMAND_PERIOD = 0x08;
24static const uint8_t SDS011_GET_MODE = 0x00;
25static const uint8_t SDS011_SET_MODE = 0x01;
26static const uint8_t SDS011_MODE_REPORT_ACTIVE = 0x00;
27static const uint8_t SDS011_MODE_REPORT_QUERY = 0x01;
28static const uint8_t SDS011_MODE_SLEEP = 0x00;
29static const uint8_t SDS011_MODE_WORK = 0x01;
37 uint8_t command_data[SDS011_DATA_REQUEST_LENGTH] = {0};
38 command_data[0] = SDS011_COMMAND_REPORT_MODE;
39 command_data[1] = SDS011_SET_MODE;
40 command_data[2] = SDS011_MODE_REPORT_ACTIVE;
41 command_data[13] = 0xff;
42 command_data[14] = 0xff;
45 command_data[0] = SDS011_COMMAND_PERIOD;
46 command_data[1] = SDS011_SET_MODE;
48 command_data[13] = 0xff;
49 command_data[14] = 0xff;
59 uint8_t command_data[SDS011_DATA_REQUEST_LENGTH] = {0};
60 command_data[0] = SDS011_COMMAND_SLEEP;
61 command_data[1] = SDS011_SET_MODE;
62 command_data[2] = working_state ? SDS011_MODE_WORK : SDS011_MODE_SLEEP;
63 command_data[13] = 0xff;
64 command_data[14] = 0xff;
71 " Update Interval: %u min\n"
82 ESP_LOGV(TAG,
"Last transmission too long ago. Reset RX index.");
93 [
this](uint8_t
byte) {
96 if (!check.has_value()) {
100 }
else if (!*check) {
102 ESP_LOGV(TAG,
"Byte %i of received data frame is invalid.", this->
data_index_);
115 this->
write_array(command_data, SDS011_DATA_REQUEST_LENGTH);
126 uint8_t
byte = this->
data_[index];
129 return byte == SDS011_MSG_HEAD;
133 return byte == SDS011_COMMAND_ID_DATA;
136 if ((index >= 2) && (index <= 7)) {
144 ESP_LOGW(TAG,
"SDS011 Checksum doesn't match: 0x%02X!=0x%02X",
byte,
checksum);
151 if (
byte != SDS011_MSG_TAIL) {
164 ESP_LOGD(TAG,
"Got PM2.5 Concentration: %.1f µg/m³, PM10.0 Concentration: %.1f µg/m³", pm_2_5_concentration,
165 pm_10_0_concentration);
166 if (pm_2_5_concentration <= 0 && pm_10_0_concentration <= 0) {
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_
void set_update_interval_min(uint8_t update_interval_min)
uint16_t get_16_bit_uint_(uint8_t start_index) const
sensor::Sensor * pm_2_5_sensor_
void sds011_write_command_(const uint8_t *command)
void set_working_state(bool working_state)
optional< bool > check_byte_() const
uint32_t last_transmission_
uint8_t update_interval_min_
void dump_config() override
uint8_t sds011_checksum_(const uint8_t *command_data, uint8_t length) const
void publish_state(float state)
Publish a new state to the front-end.
void write_byte(uint8_t data)
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
void write_array(const uint8_t *data, size_t len)
ESPHOME_LOG_TAG(TAG, "sds011")
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Application App
Global storage of Application pointer - only one Application can exist.