ESPHome 2026.9.0-dev
Loading...
Searching...
No Matches
sfa40.cpp
Go to the documentation of this file.
1#include "sfa40.h"
2#include "esphome/core/log.h"
3#include <cinttypes>
4
5namespace esphome::sfa40 {
6
7static const char *const TAG = "sfa40";
8
9// SFA40 Datasheet: https://sensirion.com/media/documents/5B06EDD9/69F84BD8/Sensirion_Datasheet_SFA40.pdf
10
11static const uint16_t SFA40_CMD_START_MEASUREMENT = 0x00AC;
12static const uint16_t SFA40_CMD_STOP_MEASUREMENT = 0x50D2;
13static const uint16_t SFA40_CMD_READ_MEASURE_PROD = 0xC0EB;
14// B4 (engineering-sample) command codes. Commands from here: https://github.com/DFRobot/DFRobot_SFA40
15static const uint16_t SFA40_CMD_READ_MEASURE_B4 = 0xE06D;
16static const uint16_t SFA40_CMD_READ_ID_PROD = 0x02CE;
17static const uint16_t SFA40_CMD_READ_ID_B4 = 0x0559;
18static const uint8_t STATUS_NOT_READY = 0x01;
19static const uint8_t STATUS_OUT_OF_SPEC = 0x02;
20
21static uint64_t raw_to_serial(const uint16_t *raw, size_t words) {
22 uint64_t serial = 0;
23 for (size_t i = 0; i < words; i++) {
24 serial = (serial << 16) | raw[i];
25 }
26 return serial;
27}
28
29static void raw_to_marking(const uint16_t *raw, size_t words, char *out, size_t out_len) {
30 if (out_len < words * 2 + 1) {
31 return;
32 }
33 for (size_t i = 0; i < words; i++) {
34 out[i * 2] = static_cast<char>(raw[i] >> 8);
35 out[i * 2 + 1] = static_cast<char>(raw[i] & 0xFF);
36 }
37 out[words * 2] = '\0';
38}
39
41 this->write_command(SFA40_CMD_STOP_MEASUREMENT);
42 this->set_timeout(25, [this]() {
43 if (!this->detect_protocol_()) {
44 ESP_LOGE(TAG, "Failed to detect SFA40 protocol");
46 this->mark_failed();
47 return;
48 }
49 if (!this->write_command(SFA40_CMD_START_MEASUREMENT)) {
50 ESP_LOGE(TAG, "Failed to start measurements");
52 this->mark_failed();
53 return;
54 }
55 this->initialized_ = true;
56 ESP_LOGD(TAG, "Measurement started");
57 });
58}
59
61 uint16_t raw[5] = {};
62 if (this->get_register(SFA40_CMD_READ_ID_PROD, raw, 3, 5)) {
64 this->serial_number_ = raw_to_serial(raw, 3);
65 ESP_LOGD(TAG, "Detected production SFA40, serial number: %012" PRIX64, this->serial_number_);
66 return true;
67 }
68 if (this->get_register(SFA40_CMD_READ_ID_B4, raw, 5, 5)) {
70 raw_to_marking(raw, 5, this->device_marking_, sizeof(this->device_marking_));
71 ESP_LOGD(TAG, "Detected engineering-sample SFA40, marking: '%s'", this->device_marking_);
72 return true;
73 }
74 return false;
75}
76
78 ESP_LOGCONFIG(TAG, "sfa40:");
79 LOG_I2C_DEVICE(this);
80 if (this->is_failed()) {
81 switch (this->error_code_) {
83 ESP_LOGW(TAG, "Protocol detection failed!");
84 break;
86 ESP_LOGW(TAG, "Measurement initialization failed!");
87 break;
88 default:
89 ESP_LOGW(TAG, "Unknown setup error!");
90 break;
91 }
92 }
93 LOG_UPDATE_INTERVAL(this);
94 switch (this->protocol_version_) {
96 ESP_LOGCONFIG(TAG, " Protocol: production\n Serial Number: %012" PRIX64, this->serial_number_);
97 break;
99 ESP_LOGCONFIG(TAG, " Protocol: prototype (B4)\n Marking: '%s'", this->device_marking_);
100 break;
101 default:
102 ESP_LOGCONFIG(TAG, " Protocol: (detecting...)");
103 break;
104 }
105 ESP_LOGCONFIG(TAG, " Wait for ready: %s", YESNO(this->wait_for_ready_));
106 LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_);
107 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
108 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
109}
110
113 return;
114 }
115
116 const uint16_t read_cmd = (this->protocol_version_ == ProtocolVersion::PRODUCTION) ? SFA40_CMD_READ_MEASURE_PROD
117 : SFA40_CMD_READ_MEASURE_B4;
118
119 if (!this->write_command(read_cmd)) {
120 ESP_LOGW(TAG, "Error reading measurement");
121 this->status_set_warning();
122 return;
123 }
124
125 this->set_timeout(5, [this]() {
126 uint16_t raw[4];
127 if (!this->read_data(raw, 4)) {
128 ESP_LOGW(TAG, "Error reading measurement data");
129 this->status_set_warning();
130 return;
131 }
132
133 const uint8_t status = raw[3] >> 8;
134 const bool sensor_not_ready = (status & STATUS_NOT_READY) != 0;
135 const bool sensor_out_of_spec = (status & STATUS_OUT_OF_SPEC) != 0;
136
137 if (this->formaldehyde_sensor_ != nullptr) {
138 if (sensor_out_of_spec) {
139 ESP_LOGW(TAG, "Skipping formaldehyde publish: sensor out of spec (status=0x%02X)", status);
140 } else if (this->wait_for_ready_ && sensor_not_ready) {
141 ESP_LOGD(TAG, "Skipping formaldehyde publish: sensor warming up");
142 } else {
143 this->formaldehyde_sensor_->publish_state(static_cast<float>(raw[0]) / 10.0f);
144 }
145 }
146
147 if (this->humidity_sensor_ != nullptr) {
148 this->humidity_sensor_->publish_state(clamp(125.0f * static_cast<float>(raw[1]) / 65535.0f - 6.0f, 0.0f, 100.0f));
149 }
150
151 if (this->temperature_sensor_ != nullptr) {
152 this->temperature_sensor_->publish_state(175.0f * (static_cast<float>(raw[2]) / 65535.0f) - 45.0f);
153 }
154
155 this->status_clear_warning();
156 });
157}
158
159} // namespace esphome::sfa40
uint8_t raw[35]
Definition bl0939.h:0
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:289
bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from I2C register.
bool write_command(T i2c_register)
Write a command to the I2C device.
bool read_data(uint16_t *data, uint8_t len)
Read data words from I2C device.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
sensor::Sensor * temperature_sensor_
Definition sfa40.h:42
void dump_config() override
Definition sfa40.cpp:77
sensor::Sensor * humidity_sensor_
Definition sfa40.h:43
ProtocolVersion protocol_version_
Definition sfa40.h:34
sensor::Sensor * formaldehyde_sensor_
Definition sfa40.h:41