ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
sdp3x.cpp
Go to the documentation of this file.
1#include "sdp3x.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::sdp3x {
7
8static const char *const TAG = "sdp3x.sensor";
9static const uint16_t SDP3X_SOFT_RESET = 0x0006;
10static const uint16_t SDP3X_READ_ID1 = 0x367C;
11static const uint16_t SDP3X_READ_ID2 = 0xE102;
12static const uint16_t SDP3X_START_DP_AVG = 0x3615;
13static const uint16_t SDP3X_START_MASS_FLOW_AVG = 0x3603;
14static const uint16_t SDP3X_STOP_MEAS = 0x3FF9;
15
17
19 if (!this->write_command(SDP3X_STOP_MEAS)) {
20 ESP_LOGW(TAG, "Stop failed"); // This sometimes fails for no good reason
21 }
22
23 if (!this->write_command(SDP3X_SOFT_RESET)) {
24 ESP_LOGW(TAG, "Soft Reset failed"); // This sometimes fails for no good reason
25 }
26
27 this->set_timeout(20, [this] {
28 if (!this->write_command(SDP3X_READ_ID1)) {
29 ESP_LOGE(TAG, "Read ID1 failed");
30 this->mark_failed();
31 return;
32 }
33 if (!this->write_command(SDP3X_READ_ID2)) {
34 ESP_LOGE(TAG, "Read ID2 failed");
35 this->mark_failed();
36 return;
37 }
38
39 uint16_t data[6];
40 if (!this->read_data(data, 6)) {
41 ESP_LOGE(TAG, "Read ID failed");
42 this->mark_failed();
43 return;
44 }
45
46 // SDP8xx
47 // ref:
48 // https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/8_Differential_Pressure/Datasheets/Sensirion_Differential_Pressure_Datasheet_SDP8xx_Digital.pdf
49 if (data[1] >> 8 == 0x02) {
50 switch (data[1] & 0xFF) {
51 case 0x01: // SDP800-500Pa
52 ESP_LOGCONFIG(TAG, "Sensor is SDP800-500Pa");
53 break;
54 case 0x0A: // SDP810-500Pa
55 ESP_LOGCONFIG(TAG, "Sensor is SDP810-500Pa");
56 break;
57 case 0x04: // SDP801-500Pa
58 ESP_LOGCONFIG(TAG, "Sensor is SDP801-500Pa");
59 break;
60 case 0x0D: // SDP811-500Pa
61 ESP_LOGCONFIG(TAG, "Sensor is SDP811-500Pa");
62 break;
63 case 0x02: // SDP800-125Pa
64 ESP_LOGCONFIG(TAG, "Sensor is SDP800-125Pa");
65 break;
66 case 0x0B: // SDP810-125Pa
67 ESP_LOGCONFIG(TAG, "Sensor is SDP810-125Pa");
68 break;
69 }
70 } else if (data[1] >> 8 == 0x01) {
71 if ((data[1] & 0xFF) == 0x01) {
72 ESP_LOGCONFIG(TAG, "Sensor is SDP31-500Pa");
73 } else if ((data[1] & 0xFF) == 0x02) {
74 ESP_LOGCONFIG(TAG, "Sensor is SDP32-125Pa");
75 }
76 }
77
78 if (!this->write_command(measurement_mode_ == DP_AVG ? SDP3X_START_DP_AVG : SDP3X_START_MASS_FLOW_AVG)) {
79 ESP_LOGE(TAG, "Start Measurements failed");
80 this->mark_failed();
81 return;
82 }
83 ESP_LOGCONFIG(TAG, "started");
84 });
85}
87 LOG_SENSOR(" ", "SDP3X", this);
88 LOG_I2C_DEVICE(this);
89 if (this->is_failed()) {
90 ESP_LOGE(TAG, " Connection with failed");
91 }
92 LOG_UPDATE_INTERVAL(this);
93}
94
96 uint16_t data[3];
97 if (!this->read_data(data, 3)) {
98 ESP_LOGW(TAG, "Couldn't read data");
99 this->status_set_warning();
100 return;
101 }
102
103 int16_t pressure_raw = data[0];
104 int16_t temperature_raw = data[1];
105 int16_t scale_factor_raw = data[2];
106 // scale factor is in Pa - convert to hPa
107 float pressure = pressure_raw / (scale_factor_raw * 100.0f);
108 ESP_LOGV(TAG, "Got raw pressure=%d, raw scale factor =%d, raw temperature=%d ", pressure_raw, scale_factor_raw,
109 temperature_raw);
110 ESP_LOGD(TAG, "Got Pressure=%.3f hPa", pressure);
111
112 this->publish_state(pressure);
113 this->status_clear_warning();
114}
115
116} // namespace esphome::sdp3x
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
void status_clear_warning()
Definition component.h:306
void read_pressure_()
Internal method to read the pressure from the component after it has been scheduled.
Definition sdp3x.cpp:95
void setup() override
Setup the sensor and test for a connection.
Definition sdp3x.cpp:18
void update() override
Schedule temperature+pressure readings.
Definition sdp3x.cpp:16
void dump_config() override
Definition sdp3x.cpp:86
MeasurementMode measurement_mode_
Definition sdp3x.h:24
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
uint8_t pressure
Definition tt21100.cpp:7