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