ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
max31888.cpp
Go to the documentation of this file.
1#include "max31888.h"
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "max31888");
8
9static constexpr uint32_t CONVERSION_TIMEOUT_ID = 0;
10
11static constexpr uint8_t MAX31888_COMMAND_START_CONVERSION = 0x44;
12static constexpr uint8_t MAX31888_COMMAND_READ = 0x33;
13static constexpr uint8_t MAX31888_COMMAND_SOFT_RESET = 0x82;
14static constexpr uint8_t MAX31888_REGISTER_FIFO_DATA = 0x08;
15static constexpr uint8_t MAX31888_FIFO_READ_LENGTH = 0x01; // number of bytes to read, minus one
16static constexpr uint16_t MAX31888_CONVERSION_TIME_MS = 20;
17static constexpr float MAX31888_DEGREES_PER_LSB = 0.005f;
18
19// Sends a command and checks the checksum the device appends to it (Maxim CRC-16 over the command byte,
20// sent inverted and low byte first)
22 if (!this->send_command_(command)) {
23 this->status_set_warning(LOG_STR("bus reset failed"));
24 return false;
25 }
26 const uint8_t low = this->bus_->read8();
27 const uint8_t high = this->bus_->read8();
28 if (crc16(&command, 1, 0, 0xa001, false, true) != encode_uint16(high, low)) {
29 this->status_set_warning(LOG_STR("command checksum invalid"));
30 return false;
31 }
32 return true;
33}
34
36 if (!this->check_address_or_index_())
37 return;
38 if (!this->send_checked_command_(MAX31888_COMMAND_SOFT_RESET)) {
39 this->mark_failed();
40 }
41}
42
44 ESP_LOGCONFIG(TAG, "MAX31888 Sensor:");
45 if (this->address_ == 0) {
46 ESP_LOGW(TAG, " Unable to select an address");
47 return;
48 }
49 LOG_ONE_WIRE_DEVICE(this);
50 LOG_UPDATE_INTERVAL(this);
51}
52
54 if (this->address_ == 0)
55 return;
56
57 if (!this->send_checked_command_(MAX31888_COMMAND_START_CONVERSION)) {
58 this->publish_state(NAN);
59 return;
60 }
61
62 this->set_timeout(CONVERSION_TIMEOUT_ID, MAX31888_CONVERSION_TIME_MS, [this] {
63 int16_t raw;
64 if (!this->read_temperature_(raw)) {
65 this->publish_state(NAN);
66 return;
67 }
68 const float temperature = raw * MAX31888_DEGREES_PER_LSB;
69 ESP_LOGD(TAG, "'%s': Got Temperature=%.3f°C", this->get_name().c_str(), temperature);
71 this->publish_state(temperature);
72 });
73}
74
76 // The checksum covers the command and its two arguments as well as the data
77 uint8_t frame[7] = {MAX31888_COMMAND_READ, MAX31888_REGISTER_FIFO_DATA, MAX31888_FIFO_READ_LENGTH};
78 if (!this->send_command_(frame[0])) {
79 this->status_set_warning(LOG_STR("bus reset failed"));
80 return false;
81 }
82 this->bus_->write8(frame[1]);
83 this->bus_->write8(frame[2]);
84 for (size_t i = 3; i < sizeof(frame); i++) {
85 frame[i] = this->bus_->read8();
86 }
87 if (crc16(frame, 5, 0, 0xa001, false, true) != encode_uint16(frame[6], frame[5])) {
88 this->status_set_warning(LOG_STR("checksum invalid"));
89 return false;
90 }
91 raw = static_cast<int16_t>(encode_uint16(frame[3], frame[4]));
92 return true;
93}
94
95} // namespace esphome::max31888
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:291
const StringRef & get_name() const
Definition entity_base.h:71
bool read_temperature_(int16_t &raw)
Reads the latest conversion from the FIFO; false when the bus or the checksum fails.
Definition max31888.cpp:75
bool send_checked_command_(uint8_t command)
Definition max31888.cpp:21
virtual void write8(uint8_t val)=0
Write a word to the bus. LSB first.
virtual uint8_t read8()=0
Read an 8 bit word from the bus.
OneWireBus * bus_
pointer to OneWireBus instance
Definition one_wire.h:33
bool send_command_(uint8_t cmd)
send command on the bus
Definition one_wire.cpp:7
bool check_address_or_index_()
find an address if necessary should be called from setup
Definition one_wire.cpp:14
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "max31888")
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
Definition helpers.cpp:95
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:980
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12