ESPHome 2025.6.3
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
23 ESP_LOGCONFIG(TAG, "Running setup for '%s'", this->name_.c_str());
24 this->spi_setup();
25}
27 ESP_LOGCONFIG(TAG, "MAX31855:");
28 LOG_PIN(" CS Pin: ", this->cs_);
29 LOG_UPDATE_INTERVAL(this);
30 LOG_SENSOR(" ", "Thermocouple", this);
31 if (this->temperature_reference_) {
32 LOG_SENSOR(" ", "Reference", this->temperature_reference_);
33 } else {
34 ESP_LOGCONFIG(TAG, " Reference temperature disabled.");
35 }
36}
39 this->enable();
40 delay(1);
41 uint8_t data[4];
42 this->read_array(data, 4);
43 this->disable();
44
45 const uint32_t mem = encode_uint32(data[0], data[1], data[2], data[3]);
46
47 // Verify we got data
48 if (mem != 0xFFFFFFFF) {
49 this->status_clear_error();
50 } else {
51 ESP_LOGE(TAG, "No data received from MAX31855 (0x%08" PRIX32 "). Check wiring!", mem);
52 this->publish_state(NAN);
53 if (this->temperature_reference_) {
55 }
56 this->status_set_error();
57 return;
58 }
59
60 // Internal reference temperature always works
61 if (this->temperature_reference_) {
62 int16_t val = (mem & 0x0000FFF0) >> 4;
63 if (val & 0x0800) {
64 val |= 0xF000; // Pad out 2's complement
65 }
66 const float t_ref = float(val) * 0.0625f;
67 ESP_LOGD(TAG, "Got reference temperature: %.4f°C", t_ref);
69 }
70
71 // Check thermocouple faults
72 if (mem & 0x00000001) {
73 ESP_LOGW(TAG, "Thermocouple open circuit (not connected) fault from MAX31855 (0x%08" PRIX32 ")", mem);
74 this->publish_state(NAN);
75 this->status_set_warning();
76 return;
77 }
78 if (mem & 0x00000002) {
79 ESP_LOGW(TAG, "Thermocouple short circuit to ground fault from MAX31855 (0x%08" PRIX32 ")", mem);
80 this->publish_state(NAN);
81 this->status_set_warning();
82 return;
83 }
84 if (mem & 0x00000004) {
85 ESP_LOGW(TAG, "Thermocouple short circuit to VCC fault from MAX31855 (0x%08" PRIX32 ")", mem);
86 this->publish_state(NAN);
87 this->status_set_warning();
88 return;
89 }
90 if (mem & 0x00010000) {
91 ESP_LOGW(TAG, "Got faulty reading from MAX31855 (0x%08" PRIX32 ")", mem);
92 this->publish_state(NAN);
93 this->status_set_warning();
94 return;
95 }
96
97 // Decode thermocouple temperature
98 int16_t val = (mem & 0xFFFC0000) >> 18;
99 if (val & 0x2000) {
100 val |= 0xC000; // Pad out 2's complement
101 }
102 const float t_sense = float(val) * 0.25f;
103 ESP_LOGD(TAG, "Got thermocouple temperature: %.2f°C", t_sense);
104 this->publish_state(t_sense);
105 this->status_clear_warning();
106}
107
108} // namespace max31855
109} // namespace esphome
void status_set_warning(const char *message="unspecified")
void status_set_error(const char *message="unspecified")
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.cpp:75
constexpr const char * c_str() const
Definition string_ref.h:69
float get_setup_priority() const override
Definition max31855.cpp:37
sensor::Sensor * temperature_reference_
Definition max31855.h:27
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
mopeka_std_values val[4]
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:20
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:200
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29