ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
debug_component.cpp
Go to the documentation of this file.
1#include "debug_component.h"
2
3#include <algorithm>
5#include "esphome/core/hal.h"
7#include "esphome/core/log.h"
9#include <cinttypes>
10#include <climits>
11
12namespace esphome::debug {
13
14ESPHOME_LOG_TAG(TAG, "debug");
15
17 ESP_LOGCONFIG(TAG, "Debug component:");
18#ifdef USE_TEXT_SENSOR
19 LOG_TEXT_SENSOR(" ", "Device info", this->device_info_text_sensor_);
20#endif // USE_TEXT_SENSOR
21#ifdef USE_SENSOR
22 LOG_SENSOR(" ", "Free space on heap", this->free_sensor_);
23 LOG_SENSOR(" ", "Largest free heap block", this->block_sensor_);
24 LOG_SENSOR(" ", "CPU frequency", this->cpu_frequency_sensor_);
25#ifdef USE_ESP8266
26 LOG_SENSOR(" ", "Heap fragmentation", this->fragmentation_sensor_);
27#endif // USE_ESP8266
28#endif // USE_SENSOR
29
30 char device_info_buffer[DEVICE_INFO_BUFFER_SIZE];
31 ESP_LOGD(TAG, "ESPHome version %s", ESPHOME_VERSION);
32 size_t pos = buf_append_str(device_info_buffer, DEVICE_INFO_BUFFER_SIZE, 0, ESPHOME_VERSION);
33
34 this->free_heap_ = get_free_heap_();
35 ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
36
37 uint32_t uptime = static_cast<uint32_t>(millis_64() / 1000);
38 uint32_t days = uptime / 86400;
39 uptime -= days * 86400;
40 uint32_t hours = uptime / 3600;
41 uptime -= hours * 3600;
42 uint32_t minutes = uptime / 60;
43 uptime -= minutes * 60;
44 ESP_LOGD(TAG, "Uptime: %" PRIu32 "d %02" PRIu32 "h %02" PRIu32 "m %02" PRIu32 "s", days, hours, minutes, uptime);
45
46 pos = get_device_info_(std::span<char, DEVICE_INFO_BUFFER_SIZE>(device_info_buffer), pos);
47
48#ifdef USE_TEXT_SENSOR
49 PUBLISH_STATE(this->device_info_text_sensor_, device_info_buffer, pos);
50 if (this->reset_reason_text_sensor_ != nullptr) {
51 char reset_reason_buffer[RESET_REASON_BUFFER_SIZE];
52 this->reset_reason_text_sensor_->publish_state(
53 get_reset_reason_(std::span<char, RESET_REASON_BUFFER_SIZE>(reset_reason_buffer)));
54 }
55#endif // USE_TEXT_SENSOR
56
57#if defined(USE_ESP32) || defined(USE_ZEPHYR)
58 this->log_partition_info_(); // Log partition information
59#endif
60}
61
63 // log when free heap space has halved
64 uint32_t new_free_heap = get_free_heap_();
65 if (new_free_heap < this->free_heap_ / 2) {
66 this->free_heap_ = new_free_heap;
67 ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
68 this->status_momentary_warning(1000);
69 }
70
71#ifdef USE_SENSOR
72 // calculate loop time - from last call to this one
73 if (this->loop_time_sensor_ != nullptr) {
75 uint32_t loop_time = now - this->last_loop_timetag_;
76 this->max_loop_time_ = std::max(this->max_loop_time_, loop_time);
77 this->last_loop_timetag_ = now;
78 }
79#endif // USE_SENSOR
80}
81
83#ifdef USE_SENSOR
84 PUBLISH_STATE(this->free_sensor_, get_free_heap_());
85
86 if (this->loop_time_sensor_ != nullptr) {
87 this->loop_time_sensor_->publish_state(this->max_loop_time_);
88 this->max_loop_time_ = 0;
89 }
90 PUBLISH_STATE(this->cpu_frequency_sensor_, arch_get_cpu_freq_hz());
91
92#endif // USE_SENSOR
94}
95
97
98} // namespace esphome::debug
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void status_momentary_warning(uint32_t length=5000)
Set warning status flag and clear it after length ms.
float get_setup_priority() const override
void log_partition_info_()
Logs information about the device's partition table.
size_t get_device_info_(std::span< char, DEVICE_INFO_BUFFER_SIZE > buffer, size_t pos)
const char * get_reset_reason_(std::span< char, RESET_REASON_BUFFER_SIZE > buffer)
ESPHOME_LOG_TAG(TAG, "debug")
constexpr float LATE
For components that should be initialized at the very end of the setup process.
Definition component.h:59
size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str)
Safely append a string to buffer, returning new position (capped at size).
Definition helpers.h:1382
uint32_t arch_get_cpu_freq_hz()
Definition hal.cpp:55
uint64_t millis_64()
Definition hal.cpp:29
size_t size_t pos
Definition helpers.h:1333
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t