ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
sm300d2.cpp
Go to the documentation of this file.
1#include "sm300d2.h"
2#include "esphome/core/log.h"
3
4namespace esphome::sm300d2 {
5
6static const char *const TAG = "sm300d2";
7static const uint8_t SM300D2_RESPONSE_LENGTH = 17;
8
10 uint8_t response[SM300D2_RESPONSE_LENGTH];
11 uint8_t peeked;
12
13 while (this->available() > 0 && this->peek_byte(&peeked) && peeked != 0x3C)
14 this->read();
15
16 bool read_success = read_array(response, SM300D2_RESPONSE_LENGTH);
17
18 if (!read_success) {
19 ESP_LOGW(TAG, "Reading data from SM300D2 failed!");
21 return;
22 }
23
24 if (response[0] != 0x3C || response[1] != 0x02) {
25 ESP_LOGW(TAG, "Invalid preamble for SM300D2 response!");
26 this->status_set_warning();
27 return;
28 }
29
30 uint16_t calculated_checksum = this->sm300d2_checksum_(response);
31 // Occasionally the checksum has a +/- 0x80 offset. Negative temperatures are
32 // responsible for some of these. The rest are unknown/undocumented.
33 if ((calculated_checksum != response[SM300D2_RESPONSE_LENGTH - 1]) &&
34 (calculated_checksum - 0x80 != response[SM300D2_RESPONSE_LENGTH - 1]) &&
35 (calculated_checksum + 0x80 != response[SM300D2_RESPONSE_LENGTH - 1])) {
36 ESP_LOGW(TAG, "SM300D2 Checksum doesn't match: 0x%02X!=0x%02X", response[SM300D2_RESPONSE_LENGTH - 1],
37 calculated_checksum);
38 this->status_set_warning();
39 return;
40 }
41
43
44 ESP_LOGD(TAG, "Successfully read SM300D2 data");
45
46 const uint16_t co2 = (response[2] * 256) + response[3];
47 const uint16_t formaldehyde = (response[4] * 256) + response[5];
48 const uint16_t tvoc = (response[6] * 256) + response[7];
49 const uint16_t pm_2_5 = (response[8] * 256) + response[9];
50 const uint16_t pm_10_0 = (response[10] * 256) + response[11];
51 // A negative value is indicated by adding 0x80 (128) to the temperature value
52 const float temperature = ((response[12] + (response[13] * 0.1f)) > 128)
53 ? (((response[12] + (response[13] * 0.1f)) - 128) * -1)
54 : response[12] + (response[13] * 0.1f);
55 const float humidity = response[14] + (response[15] * 0.1f);
56
57 ESP_LOGD(TAG, "Received CO₂: %u ppm", co2);
58 if (this->co2_sensor_ != nullptr)
59 this->co2_sensor_->publish_state(co2);
60
61 ESP_LOGD(TAG, "Received Formaldehyde: %u µg/m³", formaldehyde);
62 if (this->formaldehyde_sensor_ != nullptr)
63 this->formaldehyde_sensor_->publish_state(formaldehyde);
64
65 ESP_LOGD(TAG, "Received TVOC: %u µg/m³", tvoc);
66 if (this->tvoc_sensor_ != nullptr)
67 this->tvoc_sensor_->publish_state(tvoc);
68
69 ESP_LOGD(TAG, "Received PM2.5: %u µg/m³", pm_2_5);
70 if (this->pm_2_5_sensor_ != nullptr)
71 this->pm_2_5_sensor_->publish_state(pm_2_5);
72
73 ESP_LOGD(TAG, "Received PM10: %u µg/m³", pm_10_0);
74 if (this->pm_10_0_sensor_ != nullptr)
75 this->pm_10_0_sensor_->publish_state(pm_10_0);
76
77 ESP_LOGD(TAG, "Received Temperature: %.2f °C", temperature);
78 if (this->temperature_sensor_ != nullptr)
79 this->temperature_sensor_->publish_state(temperature);
80
81 ESP_LOGD(TAG, "Received Humidity: %.2f percent", humidity);
82 if (this->humidity_sensor_ != nullptr)
83 this->humidity_sensor_->publish_state(humidity);
84}
85
86uint16_t SM300D2Sensor::sm300d2_checksum_(uint8_t *ptr) {
87 uint8_t sum = 0;
88 for (int i = 0; i < (SM300D2_RESPONSE_LENGTH - 1); i++) {
89 sum += *ptr++;
90 }
91 return sum;
92}
93
95 ESP_LOGCONFIG(TAG, "SM300D2:");
96 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
97 LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_);
98 LOG_SENSOR(" ", "TVOC", this->tvoc_sensor_);
99 LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_);
100 LOG_SENSOR(" ", "PM10", this->pm_10_0_sensor_);
101 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
102 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
103 this->check_uart_settings(9600);
104}
105
106} // namespace esphome::sm300d2
void status_clear_warning()
Definition component.h:306
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
sensor::Sensor * humidity_sensor_
Definition sm300d2.h:31
sensor::Sensor * pm_2_5_sensor_
Definition sm300d2.h:28
sensor::Sensor * pm_10_0_sensor_
Definition sm300d2.h:29
sensor::Sensor * co2_sensor_
Definition sm300d2.h:25
uint16_t sm300d2_checksum_(uint8_t *ptr)
Definition sm300d2.cpp:86
sensor::Sensor * formaldehyde_sensor_
Definition sm300d2.h:26
sensor::Sensor * temperature_sensor_
Definition sm300d2.h:30
sensor::Sensor * tvoc_sensor_
Definition sm300d2.h:27
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
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 peek_byte(uint8_t *data)
Definition uart.h:35
uint16_t temperature
Definition sun_gtil2.cpp:12