7 static const char *
const TAG =
"sm300d2";
8 static const uint8_t SM300D2_RESPONSE_LENGTH = 17;
11 uint8_t response[SM300D2_RESPONSE_LENGTH];
17 bool read_success =
read_array(response, SM300D2_RESPONSE_LENGTH);
20 ESP_LOGW(TAG,
"Reading data from SM300D2 failed!");
25 if (response[0] != 0x3C || response[1] != 0x02) {
26 ESP_LOGW(TAG,
"Invalid preamble for SM300D2 response!");
34 if ((calculated_checksum != response[SM300D2_RESPONSE_LENGTH - 1]) &&
35 (calculated_checksum - 0x80 != response[SM300D2_RESPONSE_LENGTH - 1]) &&
36 (calculated_checksum + 0x80 != response[SM300D2_RESPONSE_LENGTH - 1])) {
37 ESP_LOGW(TAG,
"SM300D2 Checksum doesn't match: 0x%02X!=0x%02X", response[SM300D2_RESPONSE_LENGTH - 1],
45 ESP_LOGD(TAG,
"Successfully read SM300D2 data");
47 const uint16_t co2 = (response[2] * 256) + response[3];
48 const uint16_t formaldehyde = (response[4] * 256) + response[5];
49 const uint16_t tvoc = (response[6] * 256) + response[7];
50 const uint16_t pm_2_5 = (response[8] * 256) + response[9];
51 const uint16_t pm_10_0 = (response[10] * 256) + response[11];
53 const float temperature = ((response[12] + (response[13] * 0.1f)) > 128)
54 ? (((response[12] + (response[13] * 0.1f)) - 128) * -1)
55 : response[12] + (response[13] * 0.1f);
56 const float humidity = response[14] + (response[15] * 0.1f);
58 ESP_LOGD(TAG,
"Received CO₂: %u ppm", co2);
62 ESP_LOGD(TAG,
"Received Formaldehyde: %u µg/m³", formaldehyde);
66 ESP_LOGD(TAG,
"Received TVOC: %u µg/m³", tvoc);
70 ESP_LOGD(TAG,
"Received PM2.5: %u µg/m³", pm_2_5);
74 ESP_LOGD(TAG,
"Received PM10: %u µg/m³", pm_10_0);
78 ESP_LOGD(TAG,
"Received Temperature: %.2f °C", temperature);
82 ESP_LOGD(TAG,
"Received Humidity: %.2f percent", humidity);
89 for (
int i = 0; i < (SM300D2_RESPONSE_LENGTH - 1); i++) {
96 ESP_LOGCONFIG(TAG,
"SM300D2:");
sensor::Sensor * pm_10_0_sensor_
sensor::Sensor * humidity_sensor_
bool peek_byte(uint8_t *data)
sensor::Sensor * co2_sensor_
sensor::Sensor * pm_2_5_sensor_
optional< std::array< uint8_t, N > > read_array()
void status_set_warning(const char *message="unspecified")
sensor::Sensor * formaldehyde_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 status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
uint16_t sm300d2_checksum_(uint8_t *ptr)
void dump_config() override
Implementation of SPI Controller mode.
sensor::Sensor * temperature_sensor_
sensor::Sensor * tvoc_sensor_