ESPHome 2025.5.0
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/log.h"
6#include "esphome/core/hal.h"
9#include <cinttypes>
10#include <climits>
11
12namespace esphome {
13namespace debug {
14
15static const char *const TAG = "debug";
16
18#ifndef ESPHOME_LOG_HAS_DEBUG
19 return; // Can't log below if debug logging is disabled
20#endif
21
22 ESP_LOGCONFIG(TAG, "Debug component:");
23#ifdef USE_TEXT_SENSOR
24 LOG_TEXT_SENSOR(" ", "Device info", this->device_info_);
25#endif // USE_TEXT_SENSOR
26#ifdef USE_SENSOR
27 LOG_SENSOR(" ", "Free space on heap", this->free_sensor_);
28 LOG_SENSOR(" ", "Largest free heap block", this->block_sensor_);
29 LOG_SENSOR(" ", "CPU frequency", this->cpu_frequency_sensor_);
30#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 2)
31 LOG_SENSOR(" ", "Heap fragmentation", this->fragmentation_sensor_);
32#endif // defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 2)
33#endif // USE_SENSOR
34
35 std::string device_info;
36 device_info.reserve(256);
37 ESP_LOGD(TAG, "ESPHome version %s", ESPHOME_VERSION);
38 device_info += ESPHOME_VERSION;
39
40 this->free_heap_ = get_free_heap_();
41 ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
42
43 get_device_info_(device_info);
44
45#ifdef USE_TEXT_SENSOR
46 if (this->device_info_ != nullptr) {
47 if (device_info.length() > 255)
48 device_info.resize(255);
49 this->device_info_->publish_state(device_info);
50 }
51 if (this->reset_reason_ != nullptr) {
53 }
54#endif // USE_TEXT_SENSOR
55
56#ifdef USE_ESP32
57 this->log_partition_info_(); // Log partition information for ESP32
58#endif // USE_ESP32
59}
60
62 // log when free heap space has halved
63 uint32_t new_free_heap = get_free_heap_();
64 if (new_free_heap < this->free_heap_ / 2) {
65 this->free_heap_ = new_free_heap;
66 ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
67 this->status_momentary_warning("heap", 1000);
68 }
69
70#ifdef USE_SENSOR
71 // calculate loop time - from last call to this one
72 if (this->loop_time_sensor_ != nullptr) {
73 uint32_t now = App.get_loop_component_start_time();
74 uint32_t loop_time = now - this->last_loop_timetag_;
75 this->max_loop_time_ = std::max(this->max_loop_time_, loop_time);
76 this->last_loop_timetag_ = now;
77 }
78#endif // USE_SENSOR
79}
80
82#ifdef USE_SENSOR
83 if (this->free_sensor_ != nullptr) {
85 }
86
87 if (this->loop_time_sensor_ != nullptr) {
89 this->max_loop_time_ = 0;
90 }
91 if (this->cpu_frequency_sensor_ != nullptr) {
93 }
94
95#endif // USE_SENSOR
97}
98
100
101} // namespace debug
102} // 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 std::string &name, uint32_t length=5000)
float get_setup_priority() const override
void log_partition_info_()
Logs information about the device's partition table.
sensor::Sensor * fragmentation_sensor_
text_sensor::TextSensor * reset_reason_
sensor::Sensor * cpu_frequency_sensor_
text_sensor::TextSensor * device_info_
void get_device_info_(std::string &device_info)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
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:28
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:63
Application App
Global storage of Application pointer - only one Application can exist.