12static constexpr uint32_t RESET_TIMEOUT_ID = 0;
13static constexpr uint32_t PROM_READ_TIMEOUT_ID = 1;
14static constexpr uint32_t TEMPERATURE_TIMEOUT_ID = 2;
15static constexpr uint32_t PRESSURE_TIMEOUT_ID = 3;
16static constexpr uint32_t HUMIDITY_TIMEOUT_ID = 4;
19static const uint8_t MS8607_PT_CMD_RESET = 0x1E;
23static const uint8_t MS8607_PROM_START = 0xA0;
25static const uint8_t MS8607_PROM_END = 0xAE;
27static const uint8_t MS8607_PROM_COUNT = (MS8607_PROM_END - MS8607_PROM_START) >> 1;
30static const uint8_t MS8607_CMD_H_RESET = 0xFE;
32static const uint8_t MS8607_CMD_H_MEASURE_NO_HOLD = 0xF5;
34static const float MS8607_H_TEMP_COEFFICIENT = -0.18;
37static const uint8_t MS8607_CMD_ADC_READ = 0x00;
43static const uint8_t MS8607_CMD_CONV_D1_OSR_8K = 0x4A;
45static const uint8_t MS8607_CMD_CONV_D2_OSR_8K = 0x5A;
90 bool const pt_successful = this->
write_bytes(MS8607_PT_CMD_RESET,
nullptr, 0);
93 if (!(pt_successful && h_successful)) {
94 ESP_LOGE(TAG,
"Resetting I2C devices failed");
95 if (!pt_successful && !h_successful) {
97 }
else if (!pt_successful) {
119 this->
set_timeout(PROM_READ_TIMEOUT_ID, 15, [
this]() {
143 ESP_LOGCONFIG(TAG,
"MS8607:");
144 LOG_I2C_DEVICE(
this);
148 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
151 ESP_LOGE(TAG,
"Temperature/Pressure RESET failed");
154 ESP_LOGE(TAG,
"Humidity RESET failed");
157 ESP_LOGE(TAG,
"Temperature/Pressure && Humidity RESET failed");
160 ESP_LOGE(TAG,
"Reading PROM failed");
163 ESP_LOGE(TAG,
"PROM values failed CRC");
167 ESP_LOGE(TAG,
"Error reason unknown %u",
static_cast<uint8_t
>(this->
error_code_));
171 LOG_UPDATE_INTERVAL(
this);
178 ESP_LOGD(TAG,
"Reading calibration values from PROM");
180 uint16_t
buffer[MS8607_PROM_COUNT];
181 bool successful =
true;
183 for (uint8_t idx = 0; idx < MS8607_PROM_COUNT; ++idx) {
184 uint8_t
const address_to_read = MS8607_PROM_START + (idx * 2);
189 ESP_LOGE(TAG,
"Reading calibration values from PROM failed");
194 ESP_LOGD(TAG,
"Checking CRC of calibration values from PROM");
195 uint8_t
const expected_crc = (
buffer[0] & 0xF000) >> 12;
197 uint8_t
const actual_crc = crc4(
buffer, MS8607_PROM_COUNT);
199 if (expected_crc != actual_crc) {
200 ESP_LOGE(TAG,
"Incorrect CRC value. Provided value 0x%01X != calculated value 0x%01X", expected_crc, actual_crc);
211 ESP_LOGD(TAG,
"Finished reading calibration values");
225 uint16_t crc_remainder = 0;
228 auto apply_crc = [&crc_remainder](uint8_t
next) {
229 crc_remainder ^=
next;
230 for (uint8_t bit = 8; bit > 0; --bit) {
231 if (crc_remainder & 0x8000) {
232 crc_remainder = (crc_remainder << 1) ^ 0x3000;
234 crc_remainder = (crc_remainder << 1);
240 for (
size_t idx = 0; idx <
length; ++idx) {
249 return (crc_remainder >> 12) & 0xF;
254 if (!this->
write_bytes(MS8607_CMD_CONV_D2_OSR_8K,
nullptr, 0)) {
265 if (!this->
read_bytes(MS8607_CMD_ADC_READ, bytes, 3)) {
275 if (!this->
write_bytes(MS8607_CMD_CONV_D1_OSR_8K,
nullptr, 0)) {
282 [
this, d2_raw_temperature]() { this->
read_pressure_(d2_raw_temperature); });
287 if (!this->
read_bytes(MS8607_CMD_ADC_READ, bytes, 3)) {
297 ESP_LOGW(TAG,
"Request to measure humidity failed");
309 ESP_LOGW(TAG,
"Failed to read the measured humidity value");
317 uint8_t
const expected_crc = bytes[2];
318 uint8_t
const actual_crc =
crc8(bytes, 2, 0, 0x31,
true);
319 if (expected_crc != actual_crc) {
320 ESP_LOGE(TAG,
"Incorrect Humidity CRC value. Provided value 0x%01X != calculated value 0x%01X", expected_crc,
325 if (!(humidity & 0x2)) {
327 ESP_LOGE(TAG,
"Humidity status bit was not set to 1?");
332 float const humidity_partial = float(humidity) / (1 << 16);
333 float const humidity_percentage = std::lerp(-6.0f, 118.0f, humidity_partial);
334 float const compensated_humidity_percentage =
335 humidity_percentage + (20 - temperature_float) * MS8607_H_TEMP_COEFFICIENT;
336 ESP_LOGD(TAG,
"Compensated for temperature, humidity=%.2f%%", compensated_humidity_percentage);
358 int64_t pressure_sensitivity =
363 const int64_t d_t_squared = int64_t(d_t) * d_t;
364 int64_t temperature_2 = 0;
365 int32_t pressure_offset_2 = 0;
366 int32_t pressure_sensitivity_2 = 0;
372 temperature_2 = (3 * d_t_squared) >> 33;
374 pressure_offset_2 = 61 * low_temperature_adjustment;
376 pressure_sensitivity_2 = 29 * low_temperature_adjustment;
383 pressure_offset_2 += 17 * very_low_temperature_adjustment;
385 pressure_sensitivity_2 += 9 * very_low_temperature_adjustment;
389 temperature_2 = (5 * d_t_squared) >> 38;
393 pressure_offset -= pressure_offset_2;
394 pressure_sensitivity -= pressure_sensitivity_2;
397 const int32_t
pressure = (((d1_raw_pressure * pressure_sensitivity) >> 21) - pressure_offset) >> 15;
399 const float temperature_float =
temperature / 100.0f;
400 const float pressure_float =
pressure / 100.0f;
401 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()
void status_clear_error()
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
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.
ESPHOME_LOG_TAG(TAG, "ms8607")
TAG used for logging calls.
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].