Support for MCP9600 Thermocouple Amplifier (#3700)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
MrEditor97 2022-08-25 04:49:31 +01:00 committed by GitHub
parent 63290a265c
commit 8fa18ca7c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 256 additions and 0 deletions

View file

@ -122,6 +122,7 @@ esphome/components/mcp2515/* @danielschramm @mvturnho
esphome/components/mcp3204/* @rsumner
esphome/components/mcp4728/* @berfenger
esphome/components/mcp47a1/* @jesserockz
esphome/components/mcp9600/* @MrEditor97
esphome/components/mcp9808/* @k7hpn
esphome/components/md5/* @esphome/core
esphome/components/mdns/* @esphome/core

View file

View file

@ -0,0 +1,115 @@
#include "mcp9600.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mcp9600 {
static const char *const TAG = "mcp9600";
static const uint8_t MCP9600_REGISTER_HOT_JUNCTION = 0x00;
// static const uint8_t MCP9600_REGISTER_JUNCTION_DELTA = 0x01; // Unused, but kept for future reference
static const uint8_t MCP9600_REGISTER_COLD_JUNTION = 0x02;
// static const uint8_t MCP9600_REGISTER_RAW_DATA_ADC = 0x03; // Unused, but kept for future reference
static const uint8_t MCP9600_REGISTER_STATUS = 0x04;
static const uint8_t MCP9600_REGISTER_SENSOR_CONFIG = 0x05;
static const uint8_t MCP9600_REGISTER_CONFIG = 0x06;
static const uint8_t MCP9600_REGISTER_ALERT1_CONFIG = 0x08;
static const uint8_t MCP9600_REGISTER_ALERT2_CONFIG = 0x09;
static const uint8_t MCP9600_REGISTER_ALERT3_CONFIG = 0x0A;
static const uint8_t MCP9600_REGISTER_ALERT4_CONFIG = 0x0B;
static const uint8_t MCP9600_REGISTER_ALERT1_HYSTERESIS = 0x0C;
static const uint8_t MCP9600_REGISTER_ALERT2_HYSTERESIS = 0x0D;
static const uint8_t MCP9600_REGISTER_ALERT3_HYSTERESIS = 0x0E;
static const uint8_t MCP9600_REGISTER_ALERT4_HYSTERESIS = 0x0F;
static const uint8_t MCP9600_REGISTER_ALERT1_LIMIT = 0x10;
static const uint8_t MCP9600_REGISTER_ALERT2_LIMIT = 0x11;
static const uint8_t MCP9600_REGISTER_ALERT3_LIMIT = 0x12;
static const uint8_t MCP9600_REGISTER_ALERT4_LIMIT = 0x13;
static const uint8_t MCP9600_REGISTER_DEVICE_ID = 0x20;
void MCP9600Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up MCP9600...");
uint16_t dev_id = 0;
this->read_byte_16(MCP9600_REGISTER_DEVICE_ID, &dev_id);
this->device_id_ = (uint8_t)(dev_id >> 8);
// Allows both MCP9600's and MCP9601's to be connected.
if (this->device_id_ != (uint8_t) 0x40 && this->device_id_ != (uint8_t) 0x41) {
this->error_code_ = COMMUNICATION_FAILED;
this->mark_failed();
return;
}
bool success = this->write_byte(MCP9600_REGISTER_STATUS, 0x00);
success |= this->write_byte(MCP9600_REGISTER_SENSOR_CONFIG, uint8_t(0x00 | thermocouple_type_ << 4));
success |= this->write_byte(MCP9600_REGISTER_CONFIG, 0x00);
success |= this->write_byte(MCP9600_REGISTER_ALERT1_CONFIG, 0x00);
success |= this->write_byte(MCP9600_REGISTER_ALERT2_CONFIG, 0x00);
success |= this->write_byte(MCP9600_REGISTER_ALERT3_CONFIG, 0x00);
success |= this->write_byte(MCP9600_REGISTER_ALERT4_CONFIG, 0x00);
success |= this->write_byte(MCP9600_REGISTER_ALERT1_HYSTERESIS, 0x00);
success |= this->write_byte(MCP9600_REGISTER_ALERT2_HYSTERESIS, 0x00);
success |= this->write_byte(MCP9600_REGISTER_ALERT3_HYSTERESIS, 0x00);
success |= this->write_byte(MCP9600_REGISTER_ALERT4_HYSTERESIS, 0x00);
success |= this->write_byte_16(MCP9600_REGISTER_ALERT1_LIMIT, 0x0000);
success |= this->write_byte_16(MCP9600_REGISTER_ALERT2_LIMIT, 0x0000);
success |= this->write_byte_16(MCP9600_REGISTER_ALERT3_LIMIT, 0x0000);
success |= this->write_byte_16(MCP9600_REGISTER_ALERT4_LIMIT, 0x0000);
if (!success) {
this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION;
this->mark_failed();
return;
}
}
void MCP9600Component::dump_config() {
ESP_LOGCONFIG(TAG, "MCP9600:");
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
ESP_LOGCONFIG(TAG, " Device ID: 0x%x", this->device_id_);
LOG_SENSOR(" ", "Hot Junction Temperature", this->hot_junction_sensor_);
LOG_SENSOR(" ", "Cold Junction Temperature", this->cold_junction_sensor_);
switch (this->error_code_) {
case COMMUNICATION_FAILED:
ESP_LOGE(TAG, "Connected device does not match a known MCP9600 or MCP901 sensor");
break;
case FAILED_TO_UPDATE_CONFIGURATION:
ESP_LOGE(TAG, "Failed to update device configuration");
break;
case NONE:
default:
break;
}
}
void MCP9600Component::update() {
if (this->hot_junction_sensor_ != nullptr) {
uint16_t raw_hot_junction_temperature;
if (!this->read_byte_16(MCP9600_REGISTER_HOT_JUNCTION, &raw_hot_junction_temperature)) {
this->status_set_warning();
return;
}
float hot_junction_temperature = int16_t(raw_hot_junction_temperature) * 0.0625;
this->hot_junction_sensor_->publish_state(hot_junction_temperature);
}
if (this->cold_junction_sensor_ != nullptr) {
uint16_t raw_cold_junction_temperature;
if (!this->read_byte_16(MCP9600_REGISTER_COLD_JUNTION, &raw_cold_junction_temperature)) {
this->status_set_warning();
return;
}
float cold_junction_temperature = int16_t(raw_cold_junction_temperature) * 0.0625;
this->cold_junction_sensor_->publish_state(cold_junction_temperature);
}
this->status_clear_warning();
}
} // namespace mcp9600
} // namespace esphome

View file

@ -0,0 +1,51 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace mcp9600 {
enum MCP9600ThermocoupleType : uint8_t {
MCP9600_THERMOCOUPLE_TYPE_K = 0b000,
MCP9600_THERMOCOUPLE_TYPE_J = 0b001,
MCP9600_THERMOCOUPLE_TYPE_T = 0b010,
MCP9600_THERMOCOUPLE_TYPE_N = 0b011,
MCP9600_THERMOCOUPLE_TYPE_S = 0b100,
MCP9600_THERMOCOUPLE_TYPE_E = 0b101,
MCP9600_THERMOCOUPLE_TYPE_B = 0b110,
MCP9600_THERMOCOUPLE_TYPE_R = 0b111,
};
class MCP9600Component : public PollingComponent, public i2c::I2CDevice {
public:
void setup() override;
void dump_config() override;
void update() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_hot_junction(sensor::Sensor *hot_junction) { this->hot_junction_sensor_ = hot_junction; }
void set_cold_junction(sensor::Sensor *cold_junction) { this->cold_junction_sensor_ = cold_junction; }
void set_thermocouple_type(MCP9600ThermocoupleType thermocouple_type) {
this->thermocouple_type_ = thermocouple_type;
};
protected:
uint8_t device_id_{0};
sensor::Sensor *hot_junction_sensor_{nullptr};
sensor::Sensor *cold_junction_sensor_{nullptr};
MCP9600ThermocoupleType thermocouple_type_{MCP9600_THERMOCOUPLE_TYPE_K};
enum ErrorCode {
NONE,
COMMUNICATION_FAILED,
FAILED_TO_UPDATE_CONFIGURATION,
} error_code_{NONE};
};
} // namespace mcp9600
} // namespace esphome

View file

@ -0,0 +1,81 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
CONF_ID,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
)
CONF_THERMOCOUPLE_TYPE = "thermocouple_type"
CONF_HOT_JUNCTION = "hot_junction"
CONF_COLD_JUNCTION = "cold_junction"
DEPENDENCIES = ["i2c"]
CODEOWNERS = ["@MrEditor97"]
mcp9600_ns = cg.esphome_ns.namespace("mcp9600")
MCP9600Component = mcp9600_ns.class_(
"MCP9600Component", cg.PollingComponent, i2c.I2CDevice
)
MCP9600ThermocoupleType = mcp9600_ns.enum("MCP9600ThermocoupleType")
THERMOCOUPLE_TYPE = {
"K": MCP9600ThermocoupleType.MCP9600_THERMOCOUPLE_TYPE_K,
"J": MCP9600ThermocoupleType.MCP9600_THERMOCOUPLE_TYPE_J,
"T": MCP9600ThermocoupleType.MCP9600_THERMOCOUPLE_TYPE_T,
"N": MCP9600ThermocoupleType.MCP9600_THERMOCOUPLE_TYPE_N,
"S": MCP9600ThermocoupleType.MCP9600_THERMOCOUPLE_TYPE_S,
"E": MCP9600ThermocoupleType.MCP9600_THERMOCOUPLE_TYPE_E,
"B": MCP9600ThermocoupleType.MCP9600_THERMOCOUPLE_TYPE_B,
"R": MCP9600ThermocoupleType.MCP9600_THERMOCOUPLE_TYPE_R,
}
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(MCP9600Component),
cv.Optional(CONF_THERMOCOUPLE_TYPE, default="K"): cv.enum(
THERMOCOUPLE_TYPE, upper=True
),
cv.Optional(CONF_HOT_JUNCTION): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_COLD_JUNCTION): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x67))
)
FINAL_VALIDATE_SCHEMA = i2c.final_validate_device_schema(
"mcp9600", min_frequency="100khz"
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
cg.add(var.set_thermocouple_type(config[CONF_THERMOCOUPLE_TYPE]))
if CONF_HOT_JUNCTION in config:
conf = config[CONF_HOT_JUNCTION]
sens = await sensor.new_sensor(conf)
cg.add(var.set_hot_junction(sens))
if CONF_COLD_JUNCTION in config:
conf = config[CONF_COLD_JUNCTION]
sens = await sensor.new_sensor(conf)
cg.add(var.set_cold_junction(sens))

View file

@ -38,6 +38,7 @@ uart:
baud_rate: 19200
i2c:
frequency: 100khz
modbus:
uart_id: uart1
@ -328,6 +329,13 @@ sensor:
store_baseline: true
address: 0x69
- platform: mcp9600
thermocouple_type: K
hot_junction:
name: "Thermocouple Temperature"
cold_junction:
name: "Ambient Temperature"
script:
- id: automation_test
then: