ESPHome 2026.11.0-dev
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
8ESPHOME_LOG_TAG(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 this->mark_failed(LOG_STR("Read ID1 failed"));
30 return;
31 }
32 if (!this->write_command(SDP3X_READ_ID2)) {
33 this->mark_failed(LOG_STR("Read ID2 failed"));
34 return;
35 }
36
37 uint16_t data[6];
38 if (!this->read_data(data, 6)) {
39 this->mark_failed(LOG_STR("Read ID failed"));
40 return;
41 }
42
43 // SDP8xx
44 // ref:
45 // https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/8_Differential_Pressure/Datasheets/Sensirion_Differential_Pressure_Datasheet_SDP8xx_Digital.pdf
46 if (data[1] >> 8 == 0x02) {
47 switch (data[1] & 0xFF) {
48 case 0x01: // SDP800-500Pa
49 ESP_LOGCONFIG(TAG, "Sensor is SDP800-500Pa");
50 break;
51 case 0x0A: // SDP810-500Pa
52 ESP_LOGCONFIG(TAG, "Sensor is SDP810-500Pa");
53 break;
54 case 0x04: // SDP801-500Pa
55 ESP_LOGCONFIG(TAG, "Sensor is SDP801-500Pa");
56 break;
57 case 0x0D: // SDP811-500Pa
58 ESP_LOGCONFIG(TAG, "Sensor is SDP811-500Pa");
59 break;
60 case 0x02: // SDP800-125Pa
61 ESP_LOGCONFIG(TAG, "Sensor is SDP800-125Pa");
62 break;
63 case 0x0B: // SDP810-125Pa
64 ESP_LOGCONFIG(TAG, "Sensor is SDP810-125Pa");
65 break;
66 }
67 } else if (data[1] >> 8 == 0x01) {
68 if ((data[1] & 0xFF) == 0x01) {
69 ESP_LOGCONFIG(TAG, "Sensor is SDP31-500Pa");
70 } else if ((data[1] & 0xFF) == 0x02) {
71 ESP_LOGCONFIG(TAG, "Sensor is SDP32-125Pa");
72 }
73 }
74
75 if (!this->write_command(measurement_mode_ == DP_AVG ? SDP3X_START_DP_AVG : SDP3X_START_MASS_FLOW_AVG)) {
76 this->mark_failed(LOG_STR("Start Measurements failed"));
77 return;
78 }
79 ESP_LOGCONFIG(TAG, "started");
80 });
81}
83 LOG_SENSOR(" ", "SDP3X", this);
84 LOG_I2C_DEVICE(this);
85 LOG_UPDATE_INTERVAL(this);
86}
87
89 uint16_t data[3];
90 if (!this->read_data(data, 3)) {
91 ESP_LOGW(TAG, "Couldn't read data");
92 this->status_set_warning();
93 return;
94 }
95
96 int16_t pressure_raw = data[0];
97 int16_t temperature_raw = data[1];
98 int16_t scale_factor_raw = data[2];
99 // scale factor is in Pa - convert to hPa
100 float pressure = pressure_raw / (scale_factor_raw * 100.0f);
101 ESP_LOGV(TAG, "Got raw pressure=%d, raw scale factor =%d, raw temperature=%d ", pressure_raw, scale_factor_raw,
102 temperature_raw);
103 ESP_LOGD(TAG, "Got Pressure=%.3f hPa", pressure);
104
105 this->publish_state(pressure);
106 this->status_clear_warning();
107}
108
109} // namespace esphome::sdp3x
void mark_failed()
Mark this component as failed.
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:296
void read_pressure_()
Internal method to read the pressure from the component after it has been scheduled.
Definition sdp3x.cpp:88
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:82
MeasurementMode measurement_mode_
Definition sdp3x.h:26
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")
uint8_t pressure
Definition tt21100.cpp:7