ESPHome  2024.4.1
mcp9808.cpp
Go to the documentation of this file.
1 #include "mcp9808.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace mcp9808 {
6 
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;
10 
11 static const uint16_t MCP9808_MANUF_ID = 0x0054;
12 static const uint16_t MCP9808_DEV_ID = 0x0400;
13 
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;
17 
18 static const char *const TAG = "mcp9808";
19 
21  ESP_LOGCONFIG(TAG, "Setting up %s...", this->name_.c_str());
22 
23  uint16_t manu = 0;
24  if (!this->read_byte_16(MCP9808_REG_MANUF_ID, &manu) || manu != MCP9808_MANUF_ID) {
25  this->mark_failed();
26  ESP_LOGE(TAG, "%s manufacuturer id failed, device returned %X", this->name_.c_str(), manu);
27  return;
28  }
29  uint16_t dev_id = 0;
30  if (!this->read_byte_16(MCP9808_REG_DEVICE_ID, &dev_id) || dev_id != MCP9808_DEV_ID) {
31  this->mark_failed();
32  ESP_LOGE(TAG, "%s device id failed, device returned %X", this->name_.c_str(), dev_id);
33  return;
34  }
35 }
37  ESP_LOGCONFIG(TAG, "%s:", this->name_.c_str());
38  LOG_I2C_DEVICE(this);
39  if (this->is_failed()) {
40  ESP_LOGE(TAG, "Communication with %s failed!", this->name_.c_str());
41  }
42  LOG_UPDATE_INTERVAL(this);
43  LOG_SENSOR(" ", "Temperature", this);
44 }
46  uint16_t raw_temp;
47  if (!this->read_byte_16(MCP9808_REG_AMBIENT_TEMP, &raw_temp)) {
48  this->status_set_warning();
49  return;
50  }
51  if (raw_temp == 0xFFFF) {
52  this->status_set_warning();
53  return;
54  }
55 
56  float temp = NAN;
57  uint8_t msb = (uint8_t) ((raw_temp & 0xff00) >> 8);
58  uint8_t lsb = raw_temp & 0x00ff;
59 
60  msb = msb & MCP9808_AMBIENT_CLEAR_FLAGS;
61 
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;
65  } else {
66  temp = (uint16_t) (msb) *16 + lsb / 16.0f;
67  }
68 
69  if (std::isnan(temp)) {
70  this->status_set_warning();
71  return;
72  }
73 
74  ESP_LOGD(TAG, "%s: Got temperature=%.4f°C", this->name_.c_str(), temp);
75  this->publish_state(temp);
76  this->status_clear_warning();
77 }
79 
80 } // namespace mcp9808
81 } // namespace esphome
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
void dump_config() override
Definition: mcp9808.cpp:36
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
u_int8_t raw_temp
constexpr const char * c_str() const
Definition: string_ref.h:68
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
float get_setup_priority() const override
Definition: mcp9808.cpp:78