7static const uint8_t MCP9808_REG_AMBIENT_TEMP = 0x05;
8static const uint8_t MCP9808_REG_MANUF_ID = 0x06;
9static const uint8_t MCP9808_REG_DEVICE_ID = 0x07;
11static const uint16_t MCP9808_MANUF_ID = 0x0054;
12static const uint16_t MCP9808_DEV_ID = 0x0400;
14static const uint8_t MCP9808_AMBIENT_CLEAR_FLAGS = 0x1F;
15static const uint8_t MCP9808_AMBIENT_CLEAR_SIGN = 0x0F;
16static const uint8_t MCP9808_AMBIENT_TEMP_NEGATIVE = 0x10;
18static const char *
const TAG =
"mcp9808";
22 if (!this->
read_byte_16(MCP9808_REG_MANUF_ID, &manu) || manu != MCP9808_MANUF_ID) {
24 ESP_LOGE(TAG,
"Incorrect manufacturer ID (%X) for '%s'", manu, this->
name_.
c_str());
28 if (!this->
read_byte_16(MCP9808_REG_DEVICE_ID, &dev_id) || dev_id != MCP9808_DEV_ID) {
30 ESP_LOGE(TAG,
"Incorrect device ID (%X) for '%s'", dev_id, this->
name_.
c_str());
35 ESP_LOGCONFIG(TAG,
"%s:", this->
name_.
c_str());
38 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL_FOR, this->
name_.
c_str());
40 LOG_UPDATE_INTERVAL(
this);
41 LOG_SENSOR(
" ",
"Temperature",
this);
49 if (raw_temp == 0xFFFF) {
55 uint8_t msb = (uint8_t) ((
raw_temp & 0xff00) >> 8);
58 msb = msb & MCP9808_AMBIENT_CLEAR_FLAGS;
60 if ((msb & MCP9808_AMBIENT_TEMP_NEGATIVE) == MCP9808_AMBIENT_TEMP_NEGATIVE) {
61 msb = msb & MCP9808_AMBIENT_CLEAR_SIGN;
62 temp = (256 - ((uint16_t) (msb) *16 + lsb / 16.0f)) * -1;
64 temp = (uint16_t) (msb) *16 + lsb / 16.0f;
67 if (std::isnan(temp)) {
72 ESP_LOGD(TAG,
"%s: Got temperature=%.4f°C", this->
name_.
c_str(), temp);
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
constexpr const char * c_str() const
bool read_byte_16(uint8_t a_register, uint16_t *data)
void dump_config() override
float get_setup_priority() const override
void publish_state(float state)
Publish a new state to the front-end.
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.