mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-28 00:38:28 +02:00
f94e9b6b1e
* Add MAX31865 sensor support, fix MAX31855 sensor.
# MAX31865
Added support for the MAX31865 RTD-to-Digital Converter to measure PT100 and
similar RTDs. Verified with an Adafruit unit (product ID: 3328) and a PT100
probe.
# MAX31855
This was setup for incorrect SPI clock polarity and phase, and would return bad
data due to a race condition measuring on the wrong edge (verified with Saleae
Logic scope). Selecting the correct configuration fixes that problem.
Re-wrote the decode off the datasheet to handle error states better (sends NaN
as an update on failure to read temperature, which shows the value as Unknown
in Home Assistant).
Added the *optional* ability to monitor the internal high-precision temperature
sensor, which can be nice in some applications.
* Tests for MAX31855/MAX38165.
* Update style to match project rules.
Also fix CONF_REFERENCE_RESISTANCE and CONF_REFERENCE_TEMPERATURE being defined
multiple places. Missed this when I added them to const.py.
* Update style to match project rules.
Pylint line limit 101/100 ("missed it by that much").
Also apparently I can't read and patched the wrong line in max31855.cpp.
* Minor string/style cleanup.
There was a copy-paste leftover in max31855.cpp and max31865/sensor.py had
unnecessary whitespace.
* Improve MAX31865 fault detection and logging.
Log levels are more in-line with the documented descriptions.
Fault detection code is improved. A transient fault between reads is still
reported, but now only faults *during* a read cause the sensor to fail and
return NAN ("unknown" in Home Assistant).
* Update style to match project rules.
I just now realized the .clang-format and pylintrc files are included. D'oh!
* MAX31855 & MAX31865 code style alignment.
@OttoWinter caught some style mismatches, updated to match project better.
* Fix a lost '\' in max31865/sensor.py.
57 lines
1.8 KiB
C++
57 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include "esphome/core/component.h"
|
|
#include "esphome/components/sensor/sensor.h"
|
|
#include "esphome/components/spi/spi.h"
|
|
|
|
namespace esphome {
|
|
namespace max31865 {
|
|
|
|
enum MAX31865RegisterMasks { SPI_WRITE_M = 0x80 };
|
|
enum MAX31865Registers {
|
|
CONFIGURATION_REG = 0x00,
|
|
RTD_RESISTANCE_MSB_REG = 0x01,
|
|
RTD_RESISTANCE_LSB_REG = 0x02,
|
|
FAULT_THRESHOLD_H_MSB_REG = 0x03,
|
|
FAULT_THRESHOLD_H_LSB_REG = 0x04,
|
|
FAULT_THRESHOLD_L_MSB_REG = 0x05,
|
|
FAULT_THRESHOLD_L_LSB_REG = 0x06,
|
|
FAULT_STATUS_REG = 0x07,
|
|
};
|
|
enum MAX31865ConfigFilter {
|
|
FILTER_60HZ = 0,
|
|
FILTER_50HZ = 1,
|
|
};
|
|
|
|
class MAX31865Sensor : public sensor::Sensor,
|
|
public PollingComponent,
|
|
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
|
|
spi::CLOCK_PHASE_TRAILING, spi::DATA_RATE_4MHZ> {
|
|
public:
|
|
void set_reference_resistance(float reference_resistance) { reference_resistance_ = reference_resistance; }
|
|
void set_nominal_resistance(float nominal_resistance) { rtd_nominal_resistance_ = nominal_resistance; }
|
|
void set_filter(MAX31865ConfigFilter filter) { filter_ = filter; }
|
|
void set_num_rtd_wires(uint8_t rtd_wires) { rtd_wires_ = rtd_wires; }
|
|
void setup() override;
|
|
void dump_config() override;
|
|
float get_setup_priority() const override;
|
|
|
|
void update() override;
|
|
|
|
protected:
|
|
float reference_resistance_;
|
|
float rtd_nominal_resistance_;
|
|
MAX31865ConfigFilter filter_;
|
|
uint8_t rtd_wires_;
|
|
bool has_fault_ = false;
|
|
bool has_warn_ = false;
|
|
void read_data_();
|
|
void write_register_(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position = 0);
|
|
const uint8_t read_register_(uint8_t reg);
|
|
const uint16_t read_register_16_(uint8_t reg);
|
|
float calc_temperature_(const float& rtd_ratio);
|
|
};
|
|
|
|
} // namespace max31865
|
|
} // namespace esphome
|