ESPHome 2026.2.4
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 {
13namespace debug {
14
15static const char *const TAG = "debug";
16
18 ESP_LOGCONFIG(TAG, "Debug component:");
19#ifdef USE_TEXT_SENSOR
20 LOG_TEXT_SENSOR(" ", "Device info", this->device_info_);
21#endif // USE_TEXT_SENSOR
22#ifdef USE_SENSOR
23 LOG_SENSOR(" ", "Free space on heap", this->free_sensor_);
24 LOG_SENSOR(" ", "Largest free heap block", this->block_sensor_);
25 LOG_SENSOR(" ", "CPU frequency", this->cpu_frequency_sensor_);
26#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 2)
27 LOG_SENSOR(" ", "Heap fragmentation", this->fragmentation_sensor_);
28#endif // defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 2)
29#endif // USE_SENSOR
30
31 char device_info_buffer[DEVICE_INFO_BUFFER_SIZE];
32 ESP_LOGD(TAG, "ESPHome version %s", ESPHOME_VERSION);
33 size_t pos = buf_append_printf(device_info_buffer, DEVICE_INFO_BUFFER_SIZE, 0, "%s", ESPHOME_VERSION);
34
35 this->free_heap_ = get_free_heap_();
36 ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
37
38 pos = get_device_info_(std::span<char, DEVICE_INFO_BUFFER_SIZE>(device_info_buffer), pos);
39
40#ifdef USE_TEXT_SENSOR
41 if (this->device_info_ != nullptr) {
42 this->device_info_->publish_state(device_info_buffer, pos);
43 }
44 if (this->reset_reason_ != nullptr) {
45 char reset_reason_buffer[RESET_REASON_BUFFER_SIZE];
47 get_reset_reason_(std::span<char, RESET_REASON_BUFFER_SIZE>(reset_reason_buffer)));
48 }
49#endif // USE_TEXT_SENSOR
50
51#if defined(USE_ESP32) || defined(USE_ZEPHYR)
52 this->log_partition_info_(); // Log partition information
53#endif
54}
55
57 // log when free heap space has halved
58 uint32_t new_free_heap = get_free_heap_();
59 if (new_free_heap < this->free_heap_ / 2) {
60 this->free_heap_ = new_free_heap;
61 ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
62 this->status_momentary_warning("heap", 1000);
63 }
64
65#ifdef USE_SENSOR
66 // calculate loop time - from last call to this one
67 if (this->loop_time_sensor_ != nullptr) {
68 uint32_t now = App.get_loop_component_start_time();
69 uint32_t loop_time = now - this->last_loop_timetag_;
70 this->max_loop_time_ = std::max(this->max_loop_time_, loop_time);
71 this->last_loop_timetag_ = now;
72 }
73#endif // USE_SENSOR
74}
75
77#ifdef USE_SENSOR
78 if (this->free_sensor_ != nullptr) {
80 }
81
82 if (this->loop_time_sensor_ != nullptr) {
84 this->max_loop_time_ = 0;
85 }
86 if (this->cpu_frequency_sensor_ != nullptr) {
88 }
89
90#endif // USE_SENSOR
92}
93
95
96} // namespace debug
97} // namespace esphome
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(const char *name, uint32_t length=5000)
Set warning status flag and automatically clear it after a timeout.
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)
sensor::Sensor * fragmentation_sensor_
const char * get_reset_reason_(std::span< char, RESET_REASON_BUFFER_SIZE > buffer)
text_sensor::TextSensor * reset_reason_
sensor::Sensor * cpu_frequency_sensor_
text_sensor::TextSensor * device_info_
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
void publish_state(const std::string &state)
const float LATE
For components that should be initialized at the very end of the setup process.
Definition component.cpp:93
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:51
size_t size_t pos
Definition helpers.h:729
Application App
Global storage of Application pointer - only one Application can exist.