ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
mcp9600.cpp
Go to the documentation of this file.
1#include "mcp9600.h"
2#include "esphome/core/log.h"
3
4namespace esphome::mcp9600 {
5
6static const char *const TAG = "mcp9600";
7
8static const uint8_t MCP9600_REGISTER_HOT_JUNCTION = 0x00;
9// static const uint8_t MCP9600_REGISTER_JUNCTION_DELTA = 0x01; // Unused, but kept for future reference
10static const uint8_t MCP9600_REGISTER_COLD_JUNTION = 0x02;
11// static const uint8_t MCP9600_REGISTER_RAW_DATA_ADC = 0x03; // Unused, but kept for future reference
12static const uint8_t MCP9600_REGISTER_STATUS = 0x04;
13static const uint8_t MCP9600_REGISTER_SENSOR_CONFIG = 0x05;
14static const uint8_t MCP9600_REGISTER_CONFIG = 0x06;
15static const uint8_t MCP9600_REGISTER_ALERT1_CONFIG = 0x08;
16static const uint8_t MCP9600_REGISTER_ALERT2_CONFIG = 0x09;
17static const uint8_t MCP9600_REGISTER_ALERT3_CONFIG = 0x0A;
18static const uint8_t MCP9600_REGISTER_ALERT4_CONFIG = 0x0B;
19static const uint8_t MCP9600_REGISTER_ALERT1_HYSTERESIS = 0x0C;
20static const uint8_t MCP9600_REGISTER_ALERT2_HYSTERESIS = 0x0D;
21static const uint8_t MCP9600_REGISTER_ALERT3_HYSTERESIS = 0x0E;
22static const uint8_t MCP9600_REGISTER_ALERT4_HYSTERESIS = 0x0F;
23static const uint8_t MCP9600_REGISTER_ALERT1_LIMIT = 0x10;
24static const uint8_t MCP9600_REGISTER_ALERT2_LIMIT = 0x11;
25static const uint8_t MCP9600_REGISTER_ALERT3_LIMIT = 0x12;
26static const uint8_t MCP9600_REGISTER_ALERT4_LIMIT = 0x13;
27static const uint8_t MCP9600_REGISTER_DEVICE_ID = 0x20;
28
30 uint16_t dev_id = 0;
31 this->read_byte_16(MCP9600_REGISTER_DEVICE_ID, &dev_id);
32 this->device_id_ = (uint8_t) (dev_id >> 8);
33
34 // Allows both MCP9600's and MCP9601's to be connected.
35 if (this->device_id_ != (uint8_t) 0x40 && this->device_id_ != (uint8_t) 0x41) {
36 this->error_code_ = COMMUNICATION_FAILED;
37 this->mark_failed();
38 return;
39 }
40
41 bool success = this->write_byte(MCP9600_REGISTER_STATUS, 0x00);
42 success &= this->write_byte(MCP9600_REGISTER_SENSOR_CONFIG, uint8_t(0x00 | thermocouple_type_ << 4));
43 success &= this->write_byte(MCP9600_REGISTER_CONFIG, 0x00);
44 success &= this->write_byte(MCP9600_REGISTER_ALERT1_CONFIG, 0x00);
45 success &= this->write_byte(MCP9600_REGISTER_ALERT2_CONFIG, 0x00);
46 success &= this->write_byte(MCP9600_REGISTER_ALERT3_CONFIG, 0x00);
47 success &= this->write_byte(MCP9600_REGISTER_ALERT4_CONFIG, 0x00);
48 success &= this->write_byte(MCP9600_REGISTER_ALERT1_HYSTERESIS, 0x00);
49 success &= this->write_byte(MCP9600_REGISTER_ALERT2_HYSTERESIS, 0x00);
50 success &= this->write_byte(MCP9600_REGISTER_ALERT3_HYSTERESIS, 0x00);
51 success &= this->write_byte(MCP9600_REGISTER_ALERT4_HYSTERESIS, 0x00);
52 success &= this->write_byte_16(MCP9600_REGISTER_ALERT1_LIMIT, 0x0000);
53 success &= this->write_byte_16(MCP9600_REGISTER_ALERT2_LIMIT, 0x0000);
54 success &= this->write_byte_16(MCP9600_REGISTER_ALERT3_LIMIT, 0x0000);
55 success &= this->write_byte_16(MCP9600_REGISTER_ALERT4_LIMIT, 0x0000);
56
57 if (!success) {
58 this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION;
59 this->mark_failed();
60 return;
61 }
62}
63
65 ESP_LOGCONFIG(TAG,
66 "MCP9600:\n"
67 " Device ID: 0x%x",
68 this->device_id_);
69 LOG_I2C_DEVICE(this);
70 LOG_UPDATE_INTERVAL(this);
71 LOG_SENSOR(" ", "Hot Junction Temperature", this->hot_junction_sensor_);
72 LOG_SENSOR(" ", "Cold Junction Temperature", this->cold_junction_sensor_);
73
74 switch (this->error_code_) {
76 ESP_LOGE(TAG, "Connected device does not match a known MCP9600 or MCP901 sensor");
77 break;
79 ESP_LOGE(TAG, "Failed to update device configuration");
80 break;
81 case NONE:
82 default:
83 break;
84 }
85}
86
88 if (this->hot_junction_sensor_ != nullptr) {
89 uint16_t raw_hot_junction_temperature;
90 if (!this->read_byte_16(MCP9600_REGISTER_HOT_JUNCTION, &raw_hot_junction_temperature)) {
91 this->status_set_warning();
92 return;
93 }
94 float hot_junction_temperature = int16_t(raw_hot_junction_temperature) * 0.0625;
95 this->hot_junction_sensor_->publish_state(hot_junction_temperature);
96 }
97
98 if (this->cold_junction_sensor_ != nullptr) {
99 uint16_t raw_cold_junction_temperature;
100 if (!this->read_byte_16(MCP9600_REGISTER_COLD_JUNTION, &raw_cold_junction_temperature)) {
101 this->status_set_warning();
102 return;
103 }
104 float cold_junction_temperature = int16_t(raw_cold_junction_temperature) * 0.0625;
105 this->cold_junction_sensor_->publish_state(cold_junction_temperature);
106 }
107
108 this->status_clear_warning();
109}
110
111} // namespace esphome::mcp9600
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:289
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
sensor::Sensor * hot_junction_sensor_
Definition mcp9600.h:35
MCP9600ThermocoupleType thermocouple_type_
Definition mcp9600.h:38
enum esphome::mcp9600::MCP9600Component::ErrorCode NONE
sensor::Sensor * cold_junction_sensor_
Definition mcp9600.h:36
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68