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