ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
sm300d2.cpp
Go to the documentation of this file.
1#include "sm300d2.h"
3#include "esphome/core/log.h"
4
5namespace esphome::sm300d2 {
6
7ESPHOME_LOG_TAG(TAG, "sm300d2");
8static constexpr uint8_t SM300D2_RESPONSE_LENGTH = 17;
9
11 // Takes the first frame already received; only one cut off at the end of the buffer is waited for
13 const uint8_t *response = nullptr;
14 this->read_available<1>([&](uint8_t c) { return (response = reader.feed(c)) == nullptr; });
15 for (uint8_t c; response == nullptr && reader.length() > 0 && this->read_byte(&c);)
16 response = reader.feed(c);
17
18 if (response == nullptr) {
19 ESP_LOGW(TAG, "Reading data from SM300D2 failed!");
20 this->status_set_warning();
21 return;
22 }
23
24 uint16_t calculated_checksum = sum_bytes(response, SM300D2_RESPONSE_LENGTH - 1);
25 // Occasionally the checksum has a +/- 0x80 offset. Negative temperatures are
26 // responsible for some of these. The rest are unknown/undocumented.
27 if ((calculated_checksum != response[SM300D2_RESPONSE_LENGTH - 1]) &&
28 (calculated_checksum - 0x80 != response[SM300D2_RESPONSE_LENGTH - 1]) &&
29 (calculated_checksum + 0x80 != response[SM300D2_RESPONSE_LENGTH - 1])) {
30 ESP_LOGW(TAG, "SM300D2 Checksum doesn't match: 0x%02X!=0x%02X", response[SM300D2_RESPONSE_LENGTH - 1],
31 calculated_checksum);
32 this->status_set_warning();
33 return;
34 }
35
37
38 ESP_LOGD(TAG, "Successfully read SM300D2 data");
39
40 const uint16_t co2 = (response[2] * 256) + response[3];
41 const uint16_t formaldehyde = (response[4] * 256) + response[5];
42 const uint16_t tvoc = (response[6] * 256) + response[7];
43 const uint16_t pm_2_5 = (response[8] * 256) + response[9];
44 const uint16_t pm_10_0 = (response[10] * 256) + response[11];
45 // A negative value is indicated by adding 0x80 (128) to the temperature value
46 const float temperature = ((response[12] + (response[13] * 0.1f)) > 128)
47 ? (((response[12] + (response[13] * 0.1f)) - 128) * -1)
48 : response[12] + (response[13] * 0.1f);
49 const float humidity = response[14] + (response[15] * 0.1f);
50
51 ESP_LOGD(TAG, "Received CO₂: %u ppm", co2);
52 PUBLISH_STATE(this->co2_sensor_, co2);
53
54 ESP_LOGD(TAG, "Received Formaldehyde: %u µg/m³", formaldehyde);
55 PUBLISH_STATE(this->formaldehyde_sensor_, formaldehyde);
56
57 ESP_LOGD(TAG, "Received TVOC: %u µg/m³", tvoc);
58 PUBLISH_STATE(this->tvoc_sensor_, tvoc);
59
60 ESP_LOGD(TAG, "Received PM2.5: %u µg/m³", pm_2_5);
61 PUBLISH_STATE(this->pm_2_5_sensor_, pm_2_5);
62
63 ESP_LOGD(TAG, "Received PM10: %u µg/m³", pm_10_0);
64 PUBLISH_STATE(this->pm_10_0_sensor_, pm_10_0);
65
66 ESP_LOGD(TAG, "Received Temperature: %.2f °C", temperature);
67 PUBLISH_STATE(this->temperature_sensor_, temperature);
68
69 ESP_LOGD(TAG, "Received Humidity: %.2f percent", humidity);
70 PUBLISH_STATE(this->humidity_sensor_, humidity);
71}
72
73void SM300D2Sensor::dump_config() {
74 ESP_LOGCONFIG(TAG, "SM300D2:");
75 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
76 LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_);
77 LOG_SENSOR(" ", "TVOC", this->tvoc_sensor_);
78 LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_);
79 LOG_SENSOR(" ", "PM10", this->pm_10_0_sensor_);
80 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
81 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
82}
83
84} // namespace esphome::sm300d2
void status_clear_warning()
Definition component.h:309
Collects fixed length binary frames of N bytes that start with the header bytes HEADER....
size_t length() const
Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.
const uint8_t * feed(uint8_t c)
Feed one byte; returns the completed frame, header included, valid until the next call,...
virtual void update()=0
bool read_byte(uint8_t *data)
Definition uart.h:39
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
ESPHOME_LOG_TAG(TAG, "sm300d2")
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...
Definition helpers.h:939
uint16_t temperature
Definition sun_gtil2.cpp:12