ESPHome  2024.11.1
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 
5 namespace esphome {
6 namespace ms5611 {
7 
8 static const char *const TAG = "ms5611";
9 
10 static const uint8_t MS5611_ADDRESS = 0x77;
11 static const uint8_t MS5611_CMD_ADC_READ = 0x00;
12 static const uint8_t MS5611_CMD_RESET = 0x1E;
13 static const uint8_t MS5611_CMD_CONV_D1 = 0x40;
14 static const uint8_t MS5611_CMD_CONV_D2 = 0x50;
15 static const uint8_t MS5611_CMD_READ_PROM = 0xA2;
16 
18  ESP_LOGCONFIG(TAG, "Setting up MS5611...");
19  if (!this->write_bytes(MS5611_CMD_RESET, nullptr, 0)) {
20  this->mark_failed();
21  return;
22  }
23  delay(100); // NOLINT
24  for (uint8_t offset = 0; offset < 6; offset++) {
25  if (!this->read_byte_16(MS5611_CMD_READ_PROM + (offset * 2), &this->prom_[offset])) {
26  this->mark_failed();
27  return;
28  }
29  }
30 }
32  ESP_LOGCONFIG(TAG, "MS5611:");
33  LOG_I2C_DEVICE(this);
34  if (this->is_failed()) {
35  ESP_LOGE(TAG, "Communication with MS5611 failed!");
36  }
37  LOG_UPDATE_INTERVAL(this);
38  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
39  LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
40 }
43  // request temperature reading
44  if (!this->write_bytes(MS5611_CMD_CONV_D2 + 0x08, nullptr, 0)) {
45  this->status_set_warning();
46  return;
47  }
48 
49  auto f = std::bind(&MS5611Component::read_temperature_, this);
50  this->set_timeout("temperature", 10, f);
51 }
53  uint8_t bytes[3];
54  if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
55  this->status_set_warning();
56  return;
57  }
58  const uint32_t raw_temperature = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
59 
60  // request pressure reading
61  if (!this->write_bytes(MS5611_CMD_CONV_D1 + 0x08, nullptr, 0)) {
62  this->status_set_warning();
63  return;
64  }
65 
66  auto f = std::bind(&MS5611Component::read_pressure_, this, raw_temperature);
67  this->set_timeout("pressure", 10, f);
68 }
69 void MS5611Component::read_pressure_(uint32_t raw_temperature) {
70  uint8_t bytes[3];
71  if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
72  this->status_set_warning();
73  return;
74  }
75  const uint32_t raw_pressure = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
76  this->calculate_values_(raw_temperature, raw_pressure);
77 }
78 
79 // Calculations are taken from the datasheet which can be found here:
80 // 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
81 // Sections PRESSURE AND TEMPERATURE CALCULATION and SECOND ORDER TEMPERATURE COMPENSATION
82 // Variable names below match variable names from the datasheet but lowercased
83 void MS5611Component::calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure) {
84  const uint32_t c1 = uint32_t(this->prom_[0]);
85  const uint32_t c2 = uint32_t(this->prom_[1]);
86  const uint16_t c3 = uint16_t(this->prom_[2]);
87  const uint16_t c4 = uint16_t(this->prom_[3]);
88  const int32_t c5 = int32_t(this->prom_[4]);
89  const uint16_t c6 = uint16_t(this->prom_[5]);
90  const uint32_t d1 = raw_pressure;
91  const int32_t d2 = raw_temperature;
92 
93  // Promote dt to 64 bit here to make the math below cleaner
94  const int64_t dt = d2 - (c5 << 8);
95  int32_t temp = (2000 + ((dt * c6) >> 23));
96 
97  int64_t off = (c2 << 16) + ((dt * c4) >> 7);
98  int64_t sens = (c1 << 15) + ((dt * c3) >> 8);
99 
100  if (temp < 2000) {
101  const int32_t t2 = (dt * dt) >> 31;
102  int32_t off2 = ((5 * (temp - 2000) * (temp - 2000)) >> 1);
103  int32_t sens2 = ((5 * (temp - 2000) * (temp - 2000)) >> 2);
104  if (temp < -1500) {
105  off2 = (off2 + 7 * (temp + 1500) * (temp + 1500));
106  sens2 = sens2 + ((11 * (temp + 1500) * (temp + 1500)) >> 1);
107  }
108  temp = temp - t2;
109  off = off - off2;
110  sens = sens - sens2;
111  }
112 
113  // Here we multiply unsigned 32-bit by signed 64-bit using signed 64-bit math.
114  // Possible ranges of D1 and SENS from the datasheet guarantee
115  // that this multiplication does not overflow
116  const int32_t p = ((((d1 * sens) >> 21) - off) >> 15);
117 
118  const float temperature = temp / 100.0f;
119  const float pressure = p / 100.0f;
120  ESP_LOGD(TAG, "Got temperature=%0.02f°C pressure=%0.01fhPa", temperature, pressure);
121 
122  if (this->temperature_sensor_ != nullptr)
123  this->temperature_sensor_->publish_state(temperature);
124  if (this->pressure_sensor_ != nullptr)
125  this->pressure_sensor_->publish_state(pressure); // hPa
126  this->status_clear_warning();
127 }
128 
129 } // namespace ms5611
130 } // namespace esphome
sensor::Sensor * pressure_sensor_
Definition: ms5611.h:26
sensor::Sensor * temperature_sensor_
Definition: ms5611.h:25
void read_pressure_(uint32_t raw_temperature)
Definition: ms5611.cpp:69
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
void dump_config() override
Definition: ms5611.cpp:31
uint8_t pressure
Definition: tt21100.cpp:19
bool is_failed() const
Definition: component.cpp:143
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
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:212
void calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure)
Definition: ms5611.cpp:83
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
float get_setup_priority() const override
Definition: ms5611.cpp:41
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< uint8_t > bytes
Definition: sml_parser.h:12
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:248