ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
sdp3x.cpp
Go to the documentation of this file.
1#include "sdp3x.h"
2#include "esphome/core/log.h"
3
4namespace esphome::sdp3x {
5
6ESPHOME_LOG_TAG(TAG, "sdp3x.sensor");
7static constexpr uint16_t SDP3X_SOFT_RESET = 0x0006;
8static constexpr uint16_t SDP3X_READ_ID1 = 0x367C;
9static constexpr uint16_t SDP3X_READ_ID2 = 0xE102;
10static constexpr uint16_t SDP3X_STOP_MEAS = 0x3FF9;
11// The first reading is ready about 8 ms after the start command
12static constexpr uint32_t FIRST_READING_DELAY_MS = 10;
13
14// Product numbers from the SDP3x and SDP8xx datasheets
15static const LogString *model_name(uint16_t product_id) {
16 switch (product_id) {
17 case 0x0101:
18 return LOG_STR("SDP31-500Pa");
19 case 0x0102:
20 return LOG_STR("SDP32-125Pa");
21 case 0x0201:
22 return LOG_STR("SDP800-500Pa");
23 case 0x0202:
24 return LOG_STR("SDP800-125Pa");
25 case 0x0204:
26 return LOG_STR("SDP801-500Pa");
27 case 0x020A:
28 return LOG_STR("SDP810-500Pa");
29 case 0x020B:
30 return LOG_STR("SDP810-125Pa");
31 case 0x020D:
32 return LOG_STR("SDP811-500Pa");
33 default:
34 return LOG_STR("unknown");
35 }
36}
37
39 // Either may be refused when the sensor is idle, so the results are ignored
40 this->write_command(SDP3X_STOP_MEAS);
41 this->write_command(SDP3X_SOFT_RESET);
42
43 this->set_timeout(20, [this] {
44 uint16_t data[6];
45 if (!this->write_command(SDP3X_READ_ID1) || !this->get_register(SDP3X_READ_ID2, data, 6) ||
46 !this->write_command(this->start_command_)) {
47 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
48 return;
49 }
50 this->product_id_ = data[1];
51 this->set_timeout(FIRST_READING_DELAY_MS, [this] { this->update(); });
52 });
53}
54
56 LOG_SENSOR(" ", "SDP3X", this);
57 if (this->product_id_ != 0) {
58 ESP_LOGCONFIG(TAG, " Model: %s (0x%04X)", LOG_STR_ARG(model_name(this->product_id_)), this->product_id_);
59 }
60 LOG_I2C_DEVICE(this);
61 LOG_UPDATE_INTERVAL(this);
62}
63
65 uint16_t data[3];
66 if (!this->status_comm_result(this->read_data(data, 3)))
67 return;
68 const auto pressure_raw = static_cast<int16_t>(data[0]);
69 const auto scale_factor = static_cast<int16_t>(data[2]);
70 // The scale factor is per Pa; publish hPa
71 const float pressure = pressure_raw / (scale_factor * 100.0f);
72 ESP_LOGV(TAG, "Pressure %.3f hPa (raw %d, scale %d)", pressure, pressure_raw, scale_factor);
73 this->publish_state(pressure);
74}
75
76} // namespace esphome::sdp3x
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
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 dump_config() override
Definition sdp3x.cpp:55
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:65
ESPHOME_LOG_TAG(TAG, "sdp3x.sensor")
static void uint32_t
uint8_t pressure
Definition tt21100.cpp:7