ESPHome 2026.4.0
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
6namespace esphome {
7namespace max31855 {
8
9static const char *const TAG = "max31855";
10
12 this->enable();
13 delay(1);
14 // conversion initiated by rising edge
15 this->disable();
16
17 // Conversion time typ: 170ms, max: 220ms
18 this->set_timeout("value", 220, [this]() { this->read_data_(); });
19}
20
23 ESP_LOGCONFIG(TAG, "MAX31855:");
24 LOG_PIN(" CS Pin: ", this->cs_);
25 LOG_UPDATE_INTERVAL(this);
26 LOG_SENSOR(" ", "Thermocouple", this);
27 if (this->temperature_reference_) {
28 LOG_SENSOR(" ", "Reference", this->temperature_reference_);
29 } else {
30 ESP_LOGCONFIG(TAG, " Reference temperature disabled.");
31 }
32}
34 this->enable();
35 delay(1);
36 uint8_t data[4];
37 this->read_array(data, 4);
38 this->disable();
39
40 const uint32_t mem = encode_uint32(data[0], data[1], data[2], data[3]);
41
42 // Verify we got data
43 if (mem != 0xFFFFFFFF) {
44 this->status_clear_error();
45 } else {
46 ESP_LOGE(TAG, "No data received from MAX31855 (0x%08" PRIX32 "). Check wiring!", mem);
47 this->publish_state(NAN);
48 if (this->temperature_reference_) {
50 }
51 this->status_set_error();
52 return;
53 }
54
55 // Internal reference temperature always works
56 if (this->temperature_reference_) {
57 int16_t val = (mem & 0x0000FFF0) >> 4;
58 if (val & 0x0800) {
59 val |= 0xF000; // Pad out 2's complement
60 }
61 const float t_ref = float(val) * 0.0625f;
62 ESP_LOGD(TAG, "Got reference temperature: %.4f°C", t_ref);
64 }
65
66 // Check thermocouple faults
67 if (mem & 0x00000001) {
68 ESP_LOGW(TAG, "Thermocouple open circuit (not connected) fault from MAX31855 (0x%08" PRIX32 ")", mem);
69 this->publish_state(NAN);
70 this->status_set_warning();
71 return;
72 }
73 if (mem & 0x00000002) {
74 ESP_LOGW(TAG, "Thermocouple short circuit to ground fault from MAX31855 (0x%08" PRIX32 ")", mem);
75 this->publish_state(NAN);
76 this->status_set_warning();
77 return;
78 }
79 if (mem & 0x00000004) {
80 ESP_LOGW(TAG, "Thermocouple short circuit to VCC fault from MAX31855 (0x%08" PRIX32 ")", mem);
81 this->publish_state(NAN);
82 this->status_set_warning();
83 return;
84 }
85 if (mem & 0x00010000) {
86 ESP_LOGW(TAG, "Got faulty reading from MAX31855 (0x%08" PRIX32 ")", mem);
87 this->publish_state(NAN);
88 this->status_set_warning();
89 return;
90 }
91
92 // Decode thermocouple temperature
93 int16_t val = (mem & 0xFFFC0000) >> 18;
94 if (val & 0x2000) {
95 val |= 0xC000; // Pad out 2's complement
96 }
97 const float t_sense = float(val) * 0.25f;
98 ESP_LOGD(TAG, "Got thermocouple temperature: %.2f°C", t_sense);
99 this->publish_state(t_sense);
100 this->status_clear_warning();
101}
102
103} // namespace max31855
104} // namespace esphome
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:497
void status_clear_error()
Definition component.h:299
void status_clear_warning()
Definition component.h:293
sensor::Sensor * temperature_reference_
Definition max31855.h:26
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
mopeka_std_values val[3]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:889
void HOT delay(uint32_t ms)
Definition core.cpp:28
static void uint32_t