10static const char *
const TAG =
"ms8607";
13static const uint8_t MS8607_PT_CMD_RESET = 0x1E;
17static const uint8_t MS8607_PROM_START = 0xA0;
19static const uint8_t MS8607_PROM_END = 0xAE;
21static const uint8_t MS8607_PROM_COUNT = (MS8607_PROM_END - MS8607_PROM_START) >> 1;
24static const uint8_t MS8607_CMD_H_RESET = 0xFE;
26static const uint8_t MS8607_CMD_H_MEASURE_NO_HOLD = 0xF5;
28static const float MS8607_H_TEMP_COEFFICIENT = -0.18;
31static const uint8_t MS8607_CMD_ADC_READ = 0x00;
37static const uint8_t MS8607_CMD_CONV_D1_OSR_8K = 0x4A;
39static const uint8_t MS8607_CMD_CONV_D2_OSR_8K = 0x5A;
65static uint8_t crc4(uint16_t *buffer,
size_t length);
84 bool const pt_successful = this->
write_bytes(MS8607_PT_CMD_RESET,
nullptr, 0);
87 if (!(pt_successful && h_successful)) {
88 ESP_LOGE(TAG,
"Resetting I2C devices failed");
89 if (!pt_successful && !h_successful) {
91 }
else if (!pt_successful) {
137 ESP_LOGCONFIG(TAG,
"MS8607:");
138 LOG_I2C_DEVICE(
this);
142 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
145 ESP_LOGE(TAG,
"Temperature/Pressure RESET failed");
148 ESP_LOGE(TAG,
"Humidity RESET failed");
151 ESP_LOGE(TAG,
"Temperature/Pressure && Humidity RESET failed");
154 ESP_LOGE(TAG,
"Reading PROM failed");
157 ESP_LOGE(TAG,
"PROM values failed CRC");
161 ESP_LOGE(TAG,
"Error reason unknown %u",
static_cast<uint8_t
>(this->
error_code_));
165 LOG_UPDATE_INTERVAL(
this);
172 ESP_LOGD(TAG,
"Reading calibration values from PROM");
174 uint16_t buffer[MS8607_PROM_COUNT];
175 bool successful =
true;
177 for (uint8_t idx = 0; idx < MS8607_PROM_COUNT; ++idx) {
178 uint8_t
const address_to_read = MS8607_PROM_START + (idx * 2);
179 successful &= this->
read_byte_16(address_to_read, &buffer[idx]);
183 ESP_LOGE(TAG,
"Reading calibration values from PROM failed");
188 ESP_LOGD(TAG,
"Checking CRC of calibration values from PROM");
189 uint8_t
const expected_crc = (buffer[0] & 0xF000) >> 12;
191 uint8_t
const actual_crc = crc4(buffer, MS8607_PROM_COUNT);
193 if (expected_crc != actual_crc) {
194 ESP_LOGE(TAG,
"Incorrect CRC value. Provided value 0x%01X != calculated value 0x%01X", expected_crc, actual_crc);
205 ESP_LOGD(TAG,
"Finished reading calibration values");
218static uint8_t crc4(uint16_t *buffer,
size_t length) {
219 uint16_t crc_remainder = 0;
222 auto apply_crc = [&crc_remainder](uint8_t next) {
223 crc_remainder ^= next;
224 for (uint8_t bit = 8; bit > 0; --bit) {
225 if (crc_remainder & 0x8000) {
226 crc_remainder = (crc_remainder << 1) ^ 0x3000;
228 crc_remainder = (crc_remainder << 1);
234 for (
size_t idx = 0; idx <
length; ++idx) {
243 return (crc_remainder >> 12) & 0xF;
248 if (!this->
write_bytes(MS8607_CMD_CONV_D2_OSR_8K,
nullptr, 0)) {
259 if (!this->
read_bytes(MS8607_CMD_ADC_READ, bytes, 3)) {
269 if (!this->
write_bytes(MS8607_CMD_CONV_D1_OSR_8K,
nullptr, 0)) {
280 if (!this->
read_bytes(MS8607_CMD_ADC_READ, bytes, 3)) {
290 ESP_LOGW(TAG,
"Request to measure humidity failed");
302 ESP_LOGW(TAG,
"Failed to read the measured humidity value");
310 uint8_t
const expected_crc = bytes[2];
311 uint8_t
const actual_crc =
crc8(bytes, 2, 0, 0x31,
true);
312 if (expected_crc != actual_crc) {
313 ESP_LOGE(TAG,
"Incorrect Humidity CRC value. Provided value 0x%01X != calculated value 0x%01X", expected_crc,
318 if (!(humidity & 0x2)) {
320 ESP_LOGE(TAG,
"Humidity status bit was not set to 1?");
325 float const humidity_partial = double(humidity) / (1 << 16);
326 float const humidity_percentage = std::lerp(-6.0, 118.0, humidity_partial);
327 float const compensated_humidity_percentage =
328 humidity_percentage + (20 - temperature_float) * MS8607_H_TEMP_COEFFICIENT;
329 ESP_LOGD(TAG,
"Compensated for temperature, humidity=%.2f%%", compensated_humidity_percentage);
351 int64_t pressure_sensitivity =
356 const int64_t d_t_squared = int64_t(d_t) * d_t;
357 int64_t temperature_2 = 0;
358 int32_t pressure_offset_2 = 0;
359 int32_t pressure_sensitivity_2 = 0;
365 temperature_2 = (3 * d_t_squared) >> 33;
367 pressure_offset_2 = 61 * low_temperature_adjustment;
369 pressure_sensitivity_2 = 29 * low_temperature_adjustment;
376 pressure_offset_2 += 17 * very_low_temperature_adjustment;
378 pressure_sensitivity_2 += 9 * very_low_temperature_adjustment;
382 temperature_2 = (5 * d_t_squared) >> 38;
386 pressure_offset -= pressure_offset_2;
387 pressure_sensitivity -= pressure_sensitivity_2;
390 const int32_t
pressure = (((d1_raw_pressure * pressure_sensitivity) >> 21) - pressure_offset) >> 15;
392 const float temperature_float =
temperature / 100.0f;
393 const float pressure_float =
pressure / 100.0f;
394 ESP_LOGD(TAG,
"Temperature=%0.2f°C, Pressure=%0.2fhPa", temperature_float, pressure_float);
void mark_failed()
Mark this component as failed.
void status_set_warning()
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void status_clear_error()
void status_clear_warning()
bool read_bytes_raw(uint8_t *data, uint8_t len) const
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 read_humidity_(float temperature_float)
process async humidity read
void request_read_temperature_()
Start async temperature read.
@ PROM_CRC_FAILED
The PROM calibration values failed the CRC check.
@ H_RESET_FAILED
Asking the Humidity sensor to reset failed.
@ PT_RESET_FAILED
Asking the Pressure/Temperature sensor to reset failed.
@ PROM_READ_FAILED
Reading the PROM calibration values failed.
@ PTH_RESET_FAILED
Both the Pressure/Temperature address and the Humidity address failed to reset.
@ NONE
Component hasn't failed (yet?)
uint8_t reset_attempts_remaining_
struct esphome::ms8607::MS8607Component::CalibrationValues calibration_values_
MS8607HumidityDevice * humidity_device_
I2CDevice object to communicate with secondary I2C address for the humidity sensor.
sensor::Sensor * temperature_sensor_
sensor::Sensor * humidity_sensor_
SetupStatus setup_status_
Current step in the multi-step & possibly delayed setup() process.
void calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure)
use raw temperature & pressure to calculate & publish values
void read_pressure_(uint32_t raw_temperature)
process async pressure read
ErrorCode error_code_
Keep track of the reason why this component failed, to augment the dumped config.
bool read_calibration_values_from_prom_()
Read and store the Pressure & Temperature calibration settings from the PROM.
void request_read_pressure_(uint32_t raw_temperature)
start async pressure read
sensor::Sensor * pressure_sensor_
@ NEEDS_PROM_READ
Reset commands succeeded, need to wait >= 15ms to read PROM.
@ NEEDS_RESET
This component has not successfully reset the PT & H devices.
@ SUCCESSFUL
Successfully read PROM and ready to update sensors.
void try_reset_()
Attempt to reset both I2C devices, retrying with backoff on failure.
void read_temperature_()
Process async temperature read.
void dump_config() override
void request_read_humidity_(float temperature_float)
start async humidity read
void publish_state(float state)
Publish a new state to the front-end.
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four 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).
uint16_t pressure_offset_temperature_coefficient
Temperature coefficient of pressure offset | TCO. [C4].
uint16_t temperature_coefficient_of_temperature
Temperature coefficient of the temperature | TEMPSENS. [C6].
uint16_t pressure_sensitivity_temperature_coefficient
Temperature coefficient of pressure sensitivity | TCS. [C3].
uint16_t pressure_sensitivity
Pressure sensitivity | SENS-T1. [C1].
uint16_t pressure_offset
Pressure offset | OFF-T1. [C2].
uint16_t reference_temperature
Reference temperature | T-REF. [C5].