11static const char *
const TAG =
"ms8607";
14static const uint8_t MS8607_PT_CMD_RESET = 0x1E;
18static const uint8_t MS8607_PROM_START = 0xA0;
20static const uint8_t MS8607_PROM_END = 0xAE;
22static const uint8_t MS8607_PROM_COUNT = (MS8607_PROM_END - MS8607_PROM_START) >> 1;
25static const uint8_t MS8607_CMD_H_RESET = 0xFE;
27static const uint8_t MS8607_CMD_H_MEASURE_NO_HOLD = 0xF5;
29static const float MS8607_H_TEMP_COEFFICIENT = -0.18;
32static const uint8_t MS8607_CMD_ADC_READ = 0x00;
38static const uint8_t MS8607_CMD_CONV_D1_OSR_8K = 0x4A;
40static const uint8_t MS8607_CMD_CONV_D2_OSR_8K = 0x5A;
66static uint8_t crc4(uint16_t *buffer,
size_t length);
67static uint8_t hsensor_crc_check(uint16_t value);
86 bool const pt_successful = this->
write_bytes(MS8607_PT_CMD_RESET,
nullptr, 0);
89 if (!(pt_successful && h_successful)) {
90 ESP_LOGE(TAG,
"Resetting I2C devices failed");
91 if (!pt_successful && !h_successful) {
93 }
else if (!pt_successful) {
139 ESP_LOGCONFIG(TAG,
"MS8607:");
140 LOG_I2C_DEVICE(
this);
144 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
147 ESP_LOGE(TAG,
"Temperature/Pressure RESET failed");
150 ESP_LOGE(TAG,
"Humidity RESET failed");
153 ESP_LOGE(TAG,
"Temperature/Pressure && Humidity RESET failed");
156 ESP_LOGE(TAG,
"Reading PROM failed");
159 ESP_LOGE(TAG,
"PROM values failed CRC");
163 ESP_LOGE(TAG,
"Error reason unknown %u",
static_cast<uint8_t
>(this->
error_code_));
167 LOG_UPDATE_INTERVAL(
this);
174 ESP_LOGD(TAG,
"Reading calibration values from PROM");
176 uint16_t buffer[MS8607_PROM_COUNT];
177 bool successful =
true;
179 for (uint8_t idx = 0; idx < MS8607_PROM_COUNT; ++idx) {
180 uint8_t
const address_to_read = MS8607_PROM_START + (idx * 2);
181 successful &= this->
read_byte_16(address_to_read, &buffer[idx]);
185 ESP_LOGE(TAG,
"Reading calibration values from PROM failed");
190 ESP_LOGD(TAG,
"Checking CRC of calibration values from PROM");
191 uint8_t
const expected_crc = (buffer[0] & 0xF000) >> 12;
193 uint8_t
const actual_crc = crc4(buffer, MS8607_PROM_COUNT);
195 if (expected_crc != actual_crc) {
196 ESP_LOGE(TAG,
"Incorrect CRC value. Provided value 0x%01X != calculated value 0x%01X", expected_crc, actual_crc);
207 ESP_LOGD(TAG,
"Finished reading calibration values");
220static uint8_t crc4(uint16_t *buffer,
size_t length) {
221 uint16_t crc_remainder = 0;
224 auto apply_crc = [&crc_remainder](uint8_t next) {
225 crc_remainder ^= next;
226 for (uint8_t bit = 8; bit > 0; --bit) {
227 if (crc_remainder & 0x8000) {
228 crc_remainder = (crc_remainder << 1) ^ 0x3000;
230 crc_remainder = (crc_remainder << 1);
236 for (
size_t idx = 0; idx <
length; ++idx) {
245 return (crc_remainder >> 12) & 0xF;
257static uint8_t hsensor_crc_check(uint16_t value) {
263 while (msb != 0x80) {
266 result = ((result ^ polynom) & mask) | (result & ~mask);
274 return result & 0xFF;
279 if (!this->
write_bytes(MS8607_CMD_CONV_D2_OSR_8K,
nullptr, 0)) {
291 if (!this->
read_bytes(MS8607_CMD_ADC_READ, bytes, 3)) {
296 const uint32_t d2_raw_temperature =
encode_uint32(0, bytes[0], bytes[1], bytes[2]);
301 if (!this->
write_bytes(MS8607_CMD_CONV_D1_OSR_8K,
nullptr, 0)) {
313 if (!this->
read_bytes(MS8607_CMD_ADC_READ, bytes, 3)) {
317 const uint32_t d1_raw_pressure =
encode_uint32(0, bytes[0], bytes[1], bytes[2]);
323 ESP_LOGW(TAG,
"Request to measure humidity failed");
336 ESP_LOGW(TAG,
"Failed to read the measured humidity value");
344 uint8_t
const expected_crc = bytes[2];
345 uint8_t
const actual_crc = hsensor_crc_check(humidity);
346 if (expected_crc != actual_crc) {
347 ESP_LOGE(TAG,
"Incorrect Humidity CRC value. Provided value 0x%01X != calculated value 0x%01X", expected_crc,
352 if (!(humidity & 0x2)) {
354 ESP_LOGE(TAG,
"Humidity status bit was not set to 1?");
359 float const humidity_partial = double(humidity) / (1 << 16);
360 float const humidity_percentage = std::lerp(-6.0, 118.0, humidity_partial);
361 float const compensated_humidity_percentage =
362 humidity_percentage + (20 - temperature_float) * MS8607_H_TEMP_COEFFICIENT;
363 ESP_LOGD(TAG,
"Compensated for temperature, humidity=%.2f%%", compensated_humidity_percentage);
385 int64_t pressure_sensitivity =
390 const int64_t d_t_squared = int64_t(d_t) * d_t;
391 int64_t temperature_2 = 0;
392 int32_t pressure_offset_2 = 0;
393 int32_t pressure_sensitivity_2 = 0;
399 temperature_2 = (3 * d_t_squared) >> 33;
401 pressure_offset_2 = 61 * low_temperature_adjustment;
403 pressure_sensitivity_2 = 29 * low_temperature_adjustment;
410 pressure_offset_2 += 17 * very_low_temperature_adjustment;
412 pressure_sensitivity_2 += 9 * very_low_temperature_adjustment;
416 temperature_2 = (5 * d_t_squared) >> 38;
420 pressure_offset -= pressure_offset_2;
421 pressure_sensitivity -= pressure_sensitivity_2;
424 const int32_t
pressure = (((d1_raw_pressure * pressure_sensitivity) >> 21) - pressure_offset) >> 15;
426 const float temperature_float =
temperature / 100.0f;
427 const float pressure_float =
pressure / 100.0f;
428 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(const char *message=nullptr)
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()
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
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.
Providing packet encoding functions for exchanging data with a remote host.
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.
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].