ESPHome 2026.2.2
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 auto f = std::bind(&MAX31855Sensor::read_data_, this);
19 this->set_timeout("value", 220, f);
20}
21
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->temperature_reference_) {
29 LOG_SENSOR(" ", "Reference", this->temperature_reference_);
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 if (this->temperature_reference_) {
51 }
52 this->status_set_error();
53 return;
54 }
55
56 // Internal reference temperature always works
57 if (this->temperature_reference_) {
58 int16_t val = (mem & 0x0000FFF0) >> 4;
59 if (val & 0x0800) {
60 val |= 0xF000; // Pad out 2's complement
61 }
62 const float t_ref = float(val) * 0.0625f;
63 ESP_LOGD(TAG, "Got reference temperature: %.4f°C", t_ref);
65 }
66
67 // Check thermocouple faults
68 if (mem & 0x00000001) {
69 ESP_LOGW(TAG, "Thermocouple open circuit (not connected) fault from MAX31855 (0x%08" PRIX32 ")", mem);
70 this->publish_state(NAN);
71 this->status_set_warning();
72 return;
73 }
74 if (mem & 0x00000002) {
75 ESP_LOGW(TAG, "Thermocouple short circuit to ground fault from MAX31855 (0x%08" PRIX32 ")", mem);
76 this->publish_state(NAN);
77 this->status_set_warning();
78 return;
79 }
80 if (mem & 0x00000004) {
81 ESP_LOGW(TAG, "Thermocouple short circuit to VCC fault from MAX31855 (0x%08" PRIX32 ")", mem);
82 this->publish_state(NAN);
83 this->status_set_warning();
84 return;
85 }
86 if (mem & 0x00010000) {
87 ESP_LOGW(TAG, "Got faulty reading from MAX31855 (0x%08" PRIX32 ")", mem);
88 this->publish_state(NAN);
89 this->status_set_warning();
90 return;
91 }
92
93 // Decode thermocouple temperature
94 int16_t val = (mem & 0xFFFC0000) >> 18;
95 if (val & 0x2000) {
96 val |= 0xC000; // Pad out 2's complement
97 }
98 const float t_sense = float(val) * 0.25f;
99 ESP_LOGD(TAG, "Got thermocouple temperature: %.2f°C", t_sense);
100 this->publish_state(t_sense);
101 this->status_clear_warning();
102}
103
104} // namespace max31855
105} // namespace esphome
void status_set_warning(const char *message=nullptr)
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:429
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:373
void status_clear_warning()
sensor::Sensor * temperature_reference_
Definition max31855.h:26
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
mopeka_std_values val[4]
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:536
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:26