7 static const char *
const TAG =
"sds011";
9 static const uint8_t SDS011_MSG_REQUEST_LENGTH = 19;
10 static const uint8_t SDS011_MSG_RESPONSE_LENGTH = 10;
11 static const uint8_t SDS011_DATA_REQUEST_LENGTH = 15;
12 static const uint8_t SDS011_DATA_RESPONSE_LENGTH = 6;
13 static const uint8_t SDS011_MSG_HEAD = 0xaa;
14 static const uint8_t SDS011_MSG_TAIL = 0xab;
15 static const uint8_t SDS011_COMMAND_ID_REQUEST = 0xb4;
16 static const uint8_t SDS011_COMMAND_ID_RESPONSE = 0xc5;
17 static const uint8_t SDS011_COMMAND_ID_DATA = 0xc0;
18 static const uint8_t SDS011_COMMAND_REPORT_MODE = 0x02;
19 static const uint8_t SDS011_COMMAND_QUERY_DATA = 0x04;
20 static const uint8_t SDS011_COMMAND_SET_DEVICE_ID = 0x05;
21 static const uint8_t SDS011_COMMAND_SLEEP = 0x06;
22 static const uint8_t SDS011_COMMAND_FIRMWARE = 0x07;
23 static const uint8_t SDS011_COMMAND_PERIOD = 0x08;
24 static const uint8_t SDS011_GET_MODE = 0x00;
25 static const uint8_t SDS011_SET_MODE = 0x01;
26 static const uint8_t SDS011_MODE_REPORT_ACTIVE = 0x00;
27 static const uint8_t SDS011_MODE_REPORT_QUERY = 0x01;
28 static const uint8_t SDS011_MODE_SLEEP = 0x00;
29 static 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;
69 ESP_LOGCONFIG(TAG,
"SDS011:");
71 ESP_LOGCONFIG(TAG,
" RX-only mode: %s", ONOFF(this->
rx_mode_only_));
78 const uint32_t now =
millis();
81 ESP_LOGV(TAG,
"Last transmission too long ago. Reset RX index.");
93 if (!check.has_value()) {
96 this->data_index_ = 0;
99 ESP_LOGV(TAG,
"Byte %i of received data frame is invalid.", this->data_index_);
100 this->data_index_ = 0;
115 this->
write_array(command_data, SDS011_DATA_REQUEST_LENGTH);
122 for (uint8_t i = 0; i <
length; i++) {
123 sum += command_data[i];
130 uint8_t byte = this->
data_[index];
133 return byte == SDS011_MSG_HEAD;
137 return byte == SDS011_COMMAND_ID_DATA;
140 if ((index >= 2) && (index <= 7)) {
147 if (checksum != byte) {
148 ESP_LOGW(TAG,
"SDS011 Checksum doesn't match: 0x%02X!=0x%02X", byte, checksum);
155 if (byte != SDS011_MSG_TAIL) {
168 ESP_LOGD(TAG,
"Got PM2.5 Concentration: %.1f µg/m³, PM10.0 Concentration: %.1f µg/m³", pm_2_5_concentration,
169 pm_10_0_concentration);
170 if (pm_2_5_concentration <= 0 && pm_10_0_concentration <= 0) {
183 return (uint16_t(this->
data_[start_index + 1]) << 8) | uint16_t(this->
data_[start_index]);
uint8_t sds011_checksum_(const uint8_t *command_data, uint8_t length) const
void set_working_state(bool working_state)
const float DATA
For components that import data from directly connected sensors like DHT.
sensor::Sensor * pm_10_0_sensor_
void write_array(const uint8_t *data, size_t len)
void write_byte(uint8_t data)
uint32_t last_transmission_
float get_setup_priority() const override
uint32_t IRAM_ATTR HOT millis()
uint8_t update_interval_min_
sensor::Sensor * pm_2_5_sensor_
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...
void set_update_interval_min(uint8_t update_interval_min)
uint16_t get_16_bit_uint_(uint8_t start_index) const
bool read_byte(uint8_t *data)
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
void dump_config() override
Implementation of SPI Controller mode.
optional< bool > check_byte_() const
void set_rx_mode_only(bool rx_mode_only)
Manually set the rx-only mode. Defaults to false.
void sds011_write_command_(const uint8_t *command)