ESPHome 2026.5.0b1
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
8static const char *const TAG = "max31855";
9
11 this->enable();
12 delay(1);
13 // conversion initiated by rising edge
14 this->disable();
15
16 // Conversion time typ: 170ms, max: 220ms
17 this->set_timeout("value", 220, [this]() { this->read_data_(); });
18}
19
22 ESP_LOGCONFIG(TAG, "MAX31855:");
23 LOG_PIN(" CS Pin: ", this->cs_);
24 LOG_UPDATE_INTERVAL(this);
25 LOG_SENSOR(" ", "Thermocouple", this);
26 if (this->temperature_reference_) {
27 LOG_SENSOR(" ", "Reference", this->temperature_reference_);
28 } else {
29 ESP_LOGCONFIG(TAG, " Reference temperature disabled.");
30 }
31}
33 this->enable();
34 delay(1);
35 uint8_t data[4];
36 this->read_array(data, 4);
37 this->disable();
38
39 const uint32_t mem = encode_uint32(data[0], data[1], data[2], data[3]);
40
41 // Verify we got data
42 if (mem != 0xFFFFFFFF) {
43 this->status_clear_error();
44 } else {
45 ESP_LOGE(TAG, "No data received from MAX31855 (0x%08" PRIX32 "). Check wiring!", mem);
46 this->publish_state(NAN);
47 if (this->temperature_reference_) {
49 }
50 this->status_set_error();
51 return;
52 }
53
54 // Internal reference temperature always works
55 if (this->temperature_reference_) {
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);
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
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:510
void status_clear_error()
Definition component.h:312
void status_clear_warning()
Definition component.h:306
sensor::Sensor * temperature_reference_
Definition max31855.h:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
mopeka_std_values val[3]
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:867
void HOT delay(uint32_t ms)
Definition hal.cpp:82
static void uint32_t