8 static const char *
const TAG =
"ms5611";
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;
18 ESP_LOGCONFIG(TAG,
"Setting up MS5611...");
19 if (!this->
write_bytes(MS5611_CMD_RESET,
nullptr, 0)) {
24 for (uint8_t offset = 0; offset < 6; offset++) {
25 if (!this->
read_byte_16(MS5611_CMD_READ_PROM + (offset * 2), &this->
prom_[offset])) {
32 ESP_LOGCONFIG(TAG,
"MS5611:");
35 ESP_LOGE(TAG,
"Communication with MS5611 failed!");
37 LOG_UPDATE_INTERVAL(
this);
44 if (!this->
write_bytes(MS5611_CMD_CONV_D2 + 0x08,
nullptr, 0)) {
54 if (!this->
read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
58 const uint32_t raw_temperature = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
61 if (!this->
write_bytes(MS5611_CMD_CONV_D1 + 0x08,
nullptr, 0)) {
71 if (!this->
read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
75 const uint32_t raw_pressure = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
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;
94 const int64_t dt = d2 - (c5 << 8);
95 int32_t temp = (2000 + ((dt * c6) >> 23));
97 int64_t off = (c2 << 16) + ((dt * c4) >> 7);
98 int64_t sens = (c1 << 15) + ((dt * c3) >> 8);
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);
105 off2 = (off2 + 7 * (temp + 1500) * (temp + 1500));
106 sens2 = sens2 + ((11 * (temp + 1500) * (temp + 1500)) >> 1);
116 const int32_t p = ((((d1 * sens) >> 21) - off) >> 15);
120 ESP_LOGD(TAG,
"Got temperature=%0.02f°C pressure=%0.01fhPa", temperature, pressure);
sensor::Sensor * pressure_sensor_
sensor::Sensor * temperature_sensor_
void read_pressure_(uint32_t raw_temperature)
bool read_byte_16(uint8_t a_register, uint16_t *data)
const float DATA
For components that import data from directly connected sensors like DHT.
void status_set_warning(const char *message="unspecified")
void dump_config() override
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
void calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure)
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
float get_setup_priority() const override
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
std::vector< uint8_t > bytes
void IRAM_ATTR HOT delay(uint32_t ms)
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)