7 static const uint8_t MCP9808_REG_AMBIENT_TEMP = 0x05;
8 static const uint8_t MCP9808_REG_MANUF_ID = 0x06;
9 static const uint8_t MCP9808_REG_DEVICE_ID = 0x07;
11 static const uint16_t MCP9808_MANUF_ID = 0x0054;
12 static const uint16_t MCP9808_DEV_ID = 0x0400;
14 static const uint8_t MCP9808_AMBIENT_CLEAR_FLAGS = 0x1F;
15 static const uint8_t MCP9808_AMBIENT_CLEAR_SIGN = 0x0F;
16 static const uint8_t MCP9808_AMBIENT_TEMP_NEGATIVE = 0x10;
18 static const char *
const TAG =
"mcp9808";
21 ESP_LOGCONFIG(TAG,
"Setting up %s...", this->
name_.
c_str());
24 if (!this->
read_byte_16(MCP9808_REG_MANUF_ID, &manu) || manu != MCP9808_MANUF_ID) {
26 ESP_LOGE(TAG,
"%s manufacuturer id failed, device returned %X", this->
name_.
c_str(), manu);
30 if (!this->
read_byte_16(MCP9808_REG_DEVICE_ID, &dev_id) || dev_id != MCP9808_DEV_ID) {
32 ESP_LOGE(TAG,
"%s device id failed, device returned %X", this->
name_.
c_str(), dev_id);
37 ESP_LOGCONFIG(TAG,
"%s:", this->
name_.
c_str());
40 ESP_LOGE(TAG,
"Communication with %s failed!", this->
name_.
c_str());
42 LOG_UPDATE_INTERVAL(
this);
43 LOG_SENSOR(
" ",
"Temperature",
this);
47 if (!this->
read_byte_16(MCP9808_REG_AMBIENT_TEMP, &raw_temp)) {
51 if (raw_temp == 0xFFFF) {
57 uint8_t msb = (uint8_t) ((raw_temp & 0xff00) >> 8);
58 uint8_t lsb = raw_temp & 0x00ff;
60 msb = msb & MCP9808_AMBIENT_CLEAR_FLAGS;
62 if ((msb & MCP9808_AMBIENT_TEMP_NEGATIVE) == MCP9808_AMBIENT_TEMP_NEGATIVE) {
63 msb = msb & MCP9808_AMBIENT_CLEAR_SIGN;
64 temp = (256 - ((uint16_t) (msb) *16 + lsb / 16.0f)) * -1;
66 temp = (uint16_t) (msb) *16 + lsb / 16.0f;
69 if (std::isnan(temp)) {
74 ESP_LOGD(TAG,
"%s: Got temperature=%.4f°C", this->
name_.
c_str(), temp);
bool read_byte_16(uint8_t a_register, uint16_t *data)
const float DATA
For components that import data from directly connected sensors like DHT.
void status_set_warning(const char *message="unspecified")
void dump_config() override
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
constexpr const char * c_str() const
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
float get_setup_priority() const override