13static constexpr uint32_t RESET_TIMEOUT_ID = 0;
14static constexpr uint32_t PROM_READ_TIMEOUT_ID = 1;
15static constexpr uint32_t TEMPERATURE_TIMEOUT_ID = 2;
16static constexpr uint32_t PRESSURE_TIMEOUT_ID = 3;
17static constexpr uint32_t HUMIDITY_TIMEOUT_ID = 4;
20static constexpr uint8_t MS8607_PT_CMD_RESET = 0x1E;
24static constexpr uint8_t MS8607_PROM_START = 0xA0;
26static constexpr uint8_t MS8607_PROM_END = 0xAE;
28static constexpr uint8_t MS8607_PROM_COUNT = (MS8607_PROM_END - MS8607_PROM_START) >> 1;
31static constexpr uint8_t MS8607_CMD_H_RESET = 0xFE;
33static constexpr uint8_t MS8607_CMD_H_MEASURE_NO_HOLD = 0xF5;
35static constexpr float MS8607_H_TEMP_COEFFICIENT = -0.18f;
38static constexpr uint8_t MS8607_CMD_ADC_READ = 0x00;
44static constexpr uint8_t MS8607_CMD_CONV_D1_OSR_8K = 0x4A;
46static constexpr uint8_t MS8607_CMD_CONV_D2_OSR_8K = 0x5A;
53static constexpr float TEMPERATURE_LOWER_LIMIT = -50.0f;
55static constexpr float TEMPERATURE_UPPER_LIMIT = 100.0f;
58static constexpr float PRESSURE_LOWER_LIMIT = 0.0f;
60static constexpr float PRESSURE_UPPER_LIMIT = 2000.0f;
62void MS8607Component::setup() {
73 bool const pt_successful = this->
write_bytes(MS8607_PT_CMD_RESET,
nullptr, 0);
74 bool h_successful =
true;
79 ESP_LOGD(TAG,
"Resetting I2C address: 0x%02X", this->
address_);
82 if (!(pt_successful && h_successful)) {
83 ESP_LOGE(TAG,
"Resetting I2C devices failed");
93 if (!pt_successful && !h_successful) {
94 this->
mark_failed(LOG_STR(
"Temperature/Pressure and Humidity reset failed"));
95 }
else if (!pt_successful) {
96 this->
mark_failed(LOG_STR(
"Temperature/Pressure reset failed"));
98 this->
mark_failed(LOG_STR(
"Humidity reset failed"));
106 this->
set_timeout(PROM_READ_TIMEOUT_ID, 15, [
this]() {
112void MS8607Component::update() {
127void MS8607Component::dump_config() {
128 ESP_LOGCONFIG(TAG,
"MS8607:");
129 LOG_I2C_DEVICE(
this);
133 LOG_UPDATE_INTERVAL(
this);
134 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
135 LOG_SENSOR(
" ",
"Pressure", this->pressure_sensor_);
136 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
139 " Calibration Values:\n"
140 " Pressure Sensitivity: 0x%04X\n"
141 " Pressure Offset: 0x%04X\n"
142 " Pressure Sensitivity Temperature Coefficient: 0x%04X\n"
143 " Pressure Offset Temperature Coefficient: 0x%04X\n"
144 " Reference Temperature: 0x%04X\n"
145 " Temperature Coefficient of Temperature: 0x%04X",
147 this->calibration_values_.pressure_sensitivity_temperature_coefficient,
148 this->calibration_values_.pressure_offset_temperature_coefficient,
149 this->calibration_values_.reference_temperature,
150 this->calibration_values_.temperature_coefficient_of_temperature);
154 ESP_LOGD(TAG,
"Reading calibration values from PROM");
156 uint16_t
buffer[MS8607_PROM_COUNT];
157 bool successful =
true;
159 for (uint8_t idx = 0; idx < MS8607_PROM_COUNT; ++idx) {
160 uint8_t
const address_to_read = MS8607_PROM_START + (idx * 2);
165 this->
mark_failed(LOG_STR(
"Reading the PROM calibration values failed"));
169 ESP_LOGD(TAG,
"Checking CRC of calibration values from PROM");
170 uint8_t
const expected_crc = (
buffer[0] & 0xF000) >> 12;
172 uint8_t
const actual_crc = crc4(
buffer, MS8607_PROM_COUNT);
174 if (expected_crc != actual_crc) {
175 ESP_LOGE(TAG,
"Incorrect CRC value. Provided value 0x%01X != calculated value 0x%01X", expected_crc, actual_crc);
176 this->
mark_failed(LOG_STR(
"PROM calibration values failed the CRC check"));
186 ESP_LOGD(TAG,
"Finished reading calibration values");
200 uint16_t crc_remainder = 0;
203 auto apply_crc = [&crc_remainder](uint8_t
next) {
204 crc_remainder ^=
next;
205 for (uint8_t bit = 8; bit > 0; --bit) {
206 if (crc_remainder & 0x8000) {
207 crc_remainder = (crc_remainder << 1) ^ 0x3000;
209 crc_remainder = (crc_remainder << 1);
215 for (
size_t idx = 0; idx <
length; ++idx) {
224 return (crc_remainder >> 12) & 0xF;
229 if (!this->
read_bytes(MS8607_CMD_ADC_READ, bytes, 3))
237 if (!this->
write_bytes(MS8607_CMD_CONV_D2_OSR_8K,
nullptr, 0)) {
238 ESP_LOGW(TAG,
"Requesting ADC Read of temperature failed");
250 if (!this->
read_adc_(d2_raw_temperature)) {
251 ESP_LOGW(TAG,
"ADC Read of temperature failed");
259 temperature.temperature_float > TEMPERATURE_UPPER_LIMIT) {
260 ESP_LOGW(TAG,
"Treating this temperature read of %0.2f°C as a failed read, skipping it",
267 PUBLISH_STATE(this->temperature_sensor_,
temperature.temperature_float);
275 if (!this->
write_bytes(MS8607_CMD_CONV_D1_OSR_8K,
nullptr, 0)) {
276 ESP_LOGW(TAG,
"Requesting ADC Read of pressure failed");
283 [
this, d2_raw_temperature]() { this->
read_pressure_(d2_raw_temperature); });
292 ESP_LOGW(TAG,
"ADC Read of pressure failed");
294 }
else if (d1_raw_pressure == 0) {
296 ESP_LOGW(TAG,
"ADC Read of pressure returned all zeros, the conversion had not finished");
301 ESP_LOGW(TAG,
"Treating this pressure read of %.2fhPa as a failed read, skipping it",
pressure);
304 this->pressure_sensor_->publish_state(
pressure);
322 ESP_LOGW(TAG,
"Request to measure humidity failed");
335 ESP_LOGW(TAG,
"Failed to read the measured humidity value");
344 uint8_t
const expected_crc = bytes[2];
345 uint8_t
const actual_crc =
crc8(bytes, 2, 0, 0x31,
true);
346 if (expected_crc != actual_crc) {
347 ESP_LOGW(TAG,
"Incorrect Humidity CRC value. Provided value 0x%01X != calculated value 0x%01X", expected_crc,
353 if (!(humidity & 0x2)) {
355 ESP_LOGW(TAG,
"Humidity status bit was not set to 1?");
360 ESP_LOGW(TAG,
"Treating this humidity read as a failed read, skipping it");
380 const int64_t d_t_squared = int64_t(result.
d_t) * result.
d_t;
388 int32_t temperature_2;
391 temperature_2 = int32_t((3 * d_t_squared) >> 33);
394 temperature_2 = int32_t((5 * d_t_squared) >> 38);
407 int64_t pressure_offset =
411 int64_t pressure_sensitivity =
416 int32_t pressure_offset_2 = 0;
417 int32_t pressure_sensitivity_2 = 0;
420 const int32_t low_temperature_adjustment =
424 pressure_offset_2 = 61 * low_temperature_adjustment;
426 pressure_sensitivity_2 = 29 * low_temperature_adjustment;
430 const int32_t very_low_temperature_adjustment =
434 pressure_offset_2 += 17 * very_low_temperature_adjustment;
436 pressure_sensitivity_2 += 9 * very_low_temperature_adjustment;
440 pressure_offset -= pressure_offset_2;
441 pressure_sensitivity -= pressure_sensitivity_2;
444 const int32_t
pressure = (((d1_raw_pressure * pressure_sensitivity) >> 21) - pressure_offset) >> 15;
453 float const humidity_partial = humidity_float / (1 << 16);
454 float const humidity_percentage = std::lerp(-6.0f, 119.0f, humidity_partial);
455 return humidity_percentage + (20 - temperature_float) * MS8607_H_TEMP_COEFFICIENT;
void mark_failed()
Mark this component as failed.
void status_clear_error()
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
bool read_bytes_raw(uint8_t *data, uint8_t len) const
uint8_t get_i2c_address() const
Returns the I2C address of the object.
uint8_t address_
store the address of the device on the bus
bool read_byte_16(uint8_t a_register, uint16_t *data)
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
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 request_read_humidity_(float temperature)
start async humidity read
void request_read_temperature_()
Start async temperature read.
bool update_ok_
False once any step of the current update cycle failed.
bool read_adc_(uint32_t &value)
Read a 24-bit converted ADC value, either D1 (pressure) or D2 (temperature)
uint8_t reset_attempts_remaining_
MS8607HumidityDevice * humidity_device_
I2CDevice object to communicate with secondary I2C address for the humidity sensor.
void read_humidity_(float temperature)
process async humidity read
void read_pressure_(uint32_t d2_raw_temperature)
Process async pressure read and publish it.
bool request_read_pressure_(uint32_t d2_raw_temperature)
Start async pressure read; false if the request failed.
bool setup_complete_
Set once the PROM calibration values have been read.
CalibrationValues calibration_values_
This device's pressure & temperature calibration values, read from PROM.
void finalize_update_()
End the update cycle, setting or clearing the warning from update_ok_.
bool read_calibration_values_from_prom_()
Read and CRC-check the PROM calibration values; marks the component failed on error.
void try_reset_()
Attempt to reset both I2C devices, retrying with backoff on failure.
void read_temperature_()
Process async temperature read, publish it and hand off to pressure or humidity.
void finish_update_(float temperature)
Read humidity if it is configured, otherwise end the update cycle.
float compensated_humidity(float humidity_float, float temperature_float)
convert raw humidity value into correct range & apply temperature compensation calculation
ESPHOME_LOG_TAG(TAG, "ms8607")
float compensated_pressure(uint32_t d1_raw_pressure, const CalibrationValues &calibration_values, const CompensatedTemperature &temperature_values)
use raw pressure, calibration values, and current temperature to figure out actual pressure Precondit...
CompensatedTemperature compensated_temperature(uint32_t d2_raw_temperature, const CalibrationValues &calibration_values)
use raw temperature and calibration values to figure out actual temperature value return value includ...
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
Pressure & temperature calibration values, read from the PROM.
uint16_t pressure_offset_temperature_coefficient
Temperature coefficient of pressure offset | TCO. [C4].
uint16_t pressure_sensitivity_temperature_coefficient
Temperature coefficient of pressure sensitivity | TCS. [C3].
uint16_t pressure_sensitivity
Pressure sensitivity | SENS-T1. [C1].
uint16_t temperature_coefficient_of_temperature
Temperature coefficient of the temperature | TEMPSENS. [C6].
uint16_t reference_temperature
Reference temperature | T-REF. [C5].
uint16_t pressure_offset
Pressure offset | OFF-T1. [C2].
int32_t first_order_temperature
temperature as hundredths of degree celsius in range [-4000, 8500], after first order temperature cal...
int32_t d_t
difference between actual and reference temperature
float temperature_float
actual temperature, after 2nd order temperature calculation, in degrees celsius as a float