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