ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
max31855.cpp
Go to the documentation of this file.
1#include "max31855.h"
2
4#include "esphome/core/log.h"
5
7
8ESPHOME_LOG_TAG(TAG, "max31855");
9
10static constexpr uint32_t READ_TIMEOUT_ID = 0;
11
12void MAX31855Sensor::update() {
13 this->enable();
14 delay(1);
15 // conversion initiated by rising edge
16 this->disable();
17
18 // Conversion time typ: 170ms, max: 220ms
19 this->set_timeout(READ_TIMEOUT_ID, 220, [this]() { this->read_data_(); });
20}
21
22void MAX31855Sensor::setup() { this->spi_setup(); }
23void MAX31855Sensor::dump_config() {
24 ESP_LOGCONFIG(TAG, "MAX31855:");
25 LOG_PIN(" CS Pin: ", this->cs_);
26 LOG_UPDATE_INTERVAL(this);
27 LOG_SENSOR(" ", "Thermocouple", this);
28 if (this->reference_sensor_) {
29 LOG_SENSOR(" ", "Reference", this->reference_sensor_);
30 } else {
31 ESP_LOGCONFIG(TAG, " Reference temperature disabled.");
32 }
33}
35 this->enable();
36 delay(1);
37 uint8_t data[4];
38 this->read_array(data, 4);
39 this->disable();
40
41 const uint32_t mem = encode_uint32(data[0], data[1], data[2], data[3]);
42
43 // Verify we got data
44 if (mem != 0xFFFFFFFF) {
45 this->status_clear_error();
46 } else {
47 ESP_LOGE(TAG, "No data received from MAX31855 (0x%08" PRIX32 "). Check wiring!", mem);
48 this->publish_state(NAN);
49 PUBLISH_STATE(this->reference_sensor_, NAN);
50 this->status_set_error();
51 return;
52 }
53
54 // Internal reference temperature always works
55 if (this->reference_sensor_) {
56 int16_t val = (mem & 0x0000FFF0) >> 4;
57 if (val & 0x0800) {
58 val |= 0xF000; // Pad out 2's complement
59 }
60 const float t_ref = float(val) * 0.0625f;
61 ESP_LOGD(TAG, "Got reference temperature: %.4f°C", t_ref);
62 this->reference_sensor_->publish_state(t_ref);
63 }
64
65 // Check thermocouple faults
66 if (mem & 0x00000001) {
67 ESP_LOGW(TAG, "Thermocouple open circuit (not connected) fault from MAX31855 (0x%08" PRIX32 ")", mem);
68 this->publish_state(NAN);
69 this->status_set_warning();
70 return;
71 }
72 if (mem & 0x00000002) {
73 ESP_LOGW(TAG, "Thermocouple short circuit to ground fault from MAX31855 (0x%08" PRIX32 ")", mem);
74 this->publish_state(NAN);
75 this->status_set_warning();
76 return;
77 }
78 if (mem & 0x00000004) {
79 ESP_LOGW(TAG, "Thermocouple short circuit to VCC fault from MAX31855 (0x%08" PRIX32 ")", mem);
80 this->publish_state(NAN);
81 this->status_set_warning();
82 return;
83 }
84 if (mem & 0x00010000) {
85 ESP_LOGW(TAG, "Got faulty reading from MAX31855 (0x%08" PRIX32 ")", mem);
86 this->publish_state(NAN);
87 this->status_set_warning();
88 return;
89 }
90
91 // Decode thermocouple temperature
92 int16_t val = (mem & 0xFFFC0000) >> 18;
93 if (val & 0x2000) {
94 val |= 0xC000; // Pad out 2's complement
95 }
96 const float t_sense = float(val) * 0.25f;
97 ESP_LOGD(TAG, "Got thermocouple temperature: %.2f°C", t_sense);
98 this->publish_state(t_sense);
100}
101
102} // namespace esphome::max31855
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void status_clear_error()
Definition component.h:315
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:309
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
mopeka_std_values val[3]
ESPHOME_LOG_TAG(TAG, "max31855")
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:1051
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t