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;
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;
21static uint64_t raw_to_serial(
const uint16_t *
raw,
size_t words) {
23 for (
size_t i = 0; i < words; i++) {
24 serial = (serial << 16) |
raw[i];
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) {
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);
37 out[words * 2] =
'\0';
44 this->
mark_failed(LOG_STR(
"Protocol detection failed"));
48 this->
mark_failed(LOG_STR(
"Failed to start measurements"));
52 ESP_LOGD(TAG,
"Measurement started");
61 ESP_LOGD(TAG,
"Detected production SFA40, serial number: %012" PRIX64, this->
serial_number_);
67 ESP_LOGD(TAG,
"Detected engineering-sample SFA40, marking: '%s'", this->
device_marking_);
73void SFA40Component::dump_config() {
74 ESP_LOGCONFIG(TAG,
"sfa40:");
76 LOG_UPDATE_INTERVAL(
this);
79 ESP_LOGCONFIG(TAG,
" Protocol: production\n Serial Number: %012" PRIX64, this->
serial_number_);
82 ESP_LOGCONFIG(TAG,
" Protocol: prototype (B4)\n Marking: '%s'", this->
device_marking_);
85 ESP_LOGCONFIG(TAG,
" Protocol: (detecting...)");
88 ESP_LOGCONFIG(TAG,
" Wait for ready: %s", YESNO(this->
wait_for_ready_));
89 LOG_SENSOR(
" ",
"Formaldehyde", this->formaldehyde_sensor_);
90 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
91 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
94void SFA40Component::update() {
100 : SFA40_CMD_READ_MEASURE_B4;
103 ESP_LOGW(TAG,
"Error reading measurement");
111 ESP_LOGW(TAG,
"Error reading measurement data");
117 const bool sensor_not_ready = (
status & STATUS_NOT_READY) != 0;
118 const bool sensor_out_of_spec = (
status & STATUS_OUT_OF_SPEC) != 0;
120 if (this->formaldehyde_sensor_ !=
nullptr) {
121 if (sensor_out_of_spec) {
122 ESP_LOGW(TAG,
"Skipping formaldehyde publish: sensor out of spec (status=0x%02X)", status);
124 ESP_LOGD(TAG,
"Skipping formaldehyde publish: sensor warming up");
126 this->formaldehyde_sensor_->publish_state(
static_cast<float>(
raw[0]) / 10.0f);
130 PUBLISH_STATE(this->humidity_sensor_, clamp(125.0f *
static_cast<float>(
raw[1]) / 65535.0f - 6.0f, 0.0f, 100.0f));
132 PUBLISH_STATE(this->temperature_sensor_, 175.0f * (
static_cast<float>(
raw[2]) / 65535.0f) - 45.0f);