ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
ms5611.cpp
Go to the documentation of this file.
1#include "ms5611.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome {
6namespace ms5611 {
7
8static const char *const TAG = "ms5611";
9
10static const uint8_t MS5611_ADDRESS = 0x77;
11static const uint8_t MS5611_CMD_ADC_READ = 0x00;
12static const uint8_t MS5611_CMD_RESET = 0x1E;
13static const uint8_t MS5611_CMD_CONV_D1 = 0x40;
14static const uint8_t MS5611_CMD_CONV_D2 = 0x50;
15static const uint8_t MS5611_CMD_READ_PROM = 0xA2;
16
18 if (!this->write_bytes(MS5611_CMD_RESET, nullptr, 0)) {
19 this->mark_failed();
20 return;
21 }
22 delay(100); // NOLINT
23 for (uint8_t offset = 0; offset < 6; offset++) {
24 if (!this->read_byte_16(MS5611_CMD_READ_PROM + (offset * 2), &this->prom_[offset])) {
25 this->mark_failed();
26 return;
27 }
28 }
29}
31 ESP_LOGCONFIG(TAG, "MS5611:");
32 LOG_I2C_DEVICE(this);
33 if (this->is_failed()) {
34 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
35 }
36 LOG_UPDATE_INTERVAL(this);
37 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
38 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
39}
42 // request temperature reading
43 if (!this->write_bytes(MS5611_CMD_CONV_D2 + 0x08, nullptr, 0)) {
44 this->status_set_warning();
45 return;
46 }
47
48 auto f = std::bind(&MS5611Component::read_temperature_, this);
49 this->set_timeout("temperature", 10, f);
50}
52 uint8_t bytes[3];
53 if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
54 this->status_set_warning();
55 return;
56 }
57 const uint32_t raw_temperature = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
58
59 // request pressure reading
60 if (!this->write_bytes(MS5611_CMD_CONV_D1 + 0x08, nullptr, 0)) {
61 this->status_set_warning();
62 return;
63 }
64
65 auto f = std::bind(&MS5611Component::read_pressure_, this, raw_temperature);
66 this->set_timeout("pressure", 10, f);
67}
68void MS5611Component::read_pressure_(uint32_t raw_temperature) {
69 uint8_t bytes[3];
70 if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
71 this->status_set_warning();
72 return;
73 }
74 const uint32_t raw_pressure = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
75 this->calculate_values_(raw_temperature, raw_pressure);
76}
77
78// Calculations are taken from the datasheet which can be found here:
79// https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Data+Sheet%7FMS5611-01BA03%7FB3%7Fpdf%7FEnglish%7FENG_DS_MS5611-01BA03_B3.pdf%7FCAT-BLPS0036
80// Sections PRESSURE AND TEMPERATURE CALCULATION and SECOND ORDER TEMPERATURE COMPENSATION
81// Variable names below match variable names from the datasheet but lowercased
82void MS5611Component::calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure) {
83 const uint32_t c1 = uint32_t(this->prom_[0]);
84 const uint32_t c2 = uint32_t(this->prom_[1]);
85 const uint16_t c3 = uint16_t(this->prom_[2]);
86 const uint16_t c4 = uint16_t(this->prom_[3]);
87 const int32_t c5 = int32_t(this->prom_[4]);
88 const uint16_t c6 = uint16_t(this->prom_[5]);
89 const uint32_t d1 = raw_pressure;
90 const int32_t d2 = raw_temperature;
91
92 // Promote dt to 64 bit here to make the math below cleaner
93 const int64_t dt = d2 - (c5 << 8);
94 int32_t temp = (2000 + ((dt * c6) >> 23));
95
96 int64_t off = (c2 << 16) + ((dt * c4) >> 7);
97 int64_t sens = (c1 << 15) + ((dt * c3) >> 8);
98
99 if (temp < 2000) {
100 const int32_t t2 = (dt * dt) >> 31;
101 int32_t off2 = ((5 * (temp - 2000) * (temp - 2000)) >> 1);
102 int32_t sens2 = ((5 * (temp - 2000) * (temp - 2000)) >> 2);
103 if (temp < -1500) {
104 off2 = (off2 + 7 * (temp + 1500) * (temp + 1500));
105 sens2 = sens2 + ((11 * (temp + 1500) * (temp + 1500)) >> 1);
106 }
107 temp = temp - t2;
108 off = off - off2;
109 sens = sens - sens2;
110 }
111
112 // Here we multiply unsigned 32-bit by signed 64-bit using signed 64-bit math.
113 // Possible ranges of D1 and SENS from the datasheet guarantee
114 // that this multiplication does not overflow
115 const int32_t p = ((((d1 * sens) >> 21) - off) >> 15);
116
117 const float temperature = temp / 100.0f;
118 const float pressure = p / 100.0f;
119 ESP_LOGD(TAG, "Got temperature=%0.02f°C pressure=%0.01fhPa", temperature, pressure);
120
121 if (this->temperature_sensor_ != nullptr)
122 this->temperature_sensor_->publish_state(temperature);
123 if (this->pressure_sensor_ != nullptr)
124 this->pressure_sensor_->publish_state(pressure); // hPa
125 this->status_clear_warning();
126}
127
128} // namespace ms5611
129} // 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.
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition i2c.h:252
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:250
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:216
float get_setup_priority() const override
Definition ms5611.cpp:40
void read_pressure_(uint32_t raw_temperature)
Definition ms5611.cpp:68
sensor::Sensor * temperature_sensor_
Definition ms5611.h:25
sensor::Sensor * pressure_sensor_
Definition ms5611.h:26
void calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure)
Definition ms5611.cpp:82
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
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7