ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
sfa30.cpp
Go to the documentation of this file.
1#include "sfa30.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace sfa30 {
6
7static const char *const TAG = "sfa30";
8
9static const uint16_t SFA30_CMD_GET_DEVICE_MARKING = 0xD060;
10static const uint16_t SFA30_CMD_START_CONTINUOUS_MEASUREMENTS = 0x0006;
11static const uint16_t SFA30_CMD_READ_MEASUREMENT = 0x0327;
12
14 // Serial Number identification
15 uint16_t raw_device_marking[16];
16 if (!this->get_register(SFA30_CMD_GET_DEVICE_MARKING, raw_device_marking, 16, 5)) {
17 ESP_LOGE(TAG, "Failed to read device marking");
18 this->error_code_ = DEVICE_MARKING_READ_FAILED;
19 this->mark_failed();
20 return;
21 }
22
23 for (size_t i = 0; i < 16; i++) {
24 this->device_marking_[i * 2] = static_cast<char>(raw_device_marking[i] >> 8);
25 this->device_marking_[i * 2 + 1] = static_cast<char>(raw_device_marking[i] & 0xFF);
26 }
27 ESP_LOGD(TAG, "Device Marking: '%s'", this->device_marking_);
28
29 if (!this->write_command(SFA30_CMD_START_CONTINUOUS_MEASUREMENTS)) {
30 ESP_LOGE(TAG, "Error starting measurements.");
31 this->error_code_ = MEASUREMENT_INIT_FAILED;
32 this->mark_failed();
33 return;
34 }
35}
36
38 ESP_LOGCONFIG(TAG, "sfa30:");
39 LOG_I2C_DEVICE(this);
40 if (this->is_failed()) {
41 switch (this->error_code_) {
42 case DEVICE_MARKING_READ_FAILED:
43 ESP_LOGW(TAG, "Unable to read device marking!");
44 break;
45 case MEASUREMENT_INIT_FAILED:
46 ESP_LOGW(TAG, "Measurement initialization failed!");
47 break;
48 default:
49 ESP_LOGW(TAG, "Unknown setup error!");
50 break;
51 }
52 }
53 LOG_UPDATE_INTERVAL(this);
54 ESP_LOGCONFIG(TAG, " Device Marking: '%s'", this->device_marking_);
55 LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_);
56 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
57 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
58}
59
61 if (!this->write_command(SFA30_CMD_READ_MEASUREMENT)) {
62 ESP_LOGW(TAG, "Error reading measurement!");
63 this->status_set_warning();
64 return;
65 }
66
67 this->set_timeout(5, [this]() {
68 uint16_t raw_data[3];
69 if (!this->read_data(raw_data, 3)) {
70 ESP_LOGW(TAG, "Error reading measurement data!");
71 this->status_set_warning();
72 return;
73 }
74
75 if (this->formaldehyde_sensor_ != nullptr) {
76 const float formaldehyde = raw_data[0] / 5.0f;
77 this->formaldehyde_sensor_->publish_state(formaldehyde);
78 }
79
80 if (this->humidity_sensor_ != nullptr) {
81 const float humidity = raw_data[1] / 100.0f;
82 this->humidity_sensor_->publish_state(humidity);
83 }
84
85 if (this->temperature_sensor_ != nullptr) {
86 const float temperature = raw_data[2] / 200.0f;
87 this->temperature_sensor_->publish_state(temperature);
88 }
89
91 });
92}
93
94} // namespace sfa30
95} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
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:45
sensor::Sensor * temperature_sensor_
Definition sfa30.h:29
void dump_config() override
Definition sfa30.cpp:37
sensor::Sensor * humidity_sensor_
Definition sfa30.h:28
sensor::Sensor * formaldehyde_sensor_
Definition sfa30.h:27
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12