diff --git a/esphome/components/logger/logger.h b/esphome/components/logger/logger.h index b8a252c7bd..b9be7f3f1d 100644 --- a/esphome/components/logger/logger.h +++ b/esphome/components/logger/logger.h @@ -31,6 +31,7 @@ class Logger : public Component { /// Manually set the baud rate for serial, set to 0 to disable. void set_baud_rate(uint32_t baud_rate); uint32_t get_baud_rate() const { return baud_rate_; } + HardwareSerial *get_hw_serial() const { return hw_serial_; } /// Get the UART used by the logger. UARTSelection get_uart() const; diff --git a/esphome/components/uart/uart.cpp b/esphome/components/uart/uart.cpp index 3284d4cb67..1f40498606 100644 --- a/esphome/components/uart/uart.cpp +++ b/esphome/components/uart/uart.cpp @@ -46,12 +46,7 @@ void UARTComponent::dump_config() { } ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_); ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_); -#ifdef USE_LOGGER - if (this->hw_serial_ == &Serial && logger::global_logger->get_baud_rate() != 0) { - ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please " - "disable logging over the serial port by setting logger->baud_rate to 0."); - } -#endif + this->check_logger_conflict_(); } void UARTComponent::write_byte(uint8_t data) { @@ -156,13 +151,7 @@ void UARTComponent::dump_config() { } else { ESP_LOGCONFIG(TAG, " Using software serial"); } - -#ifdef USE_LOGGER - if (this->hw_serial_ == &Serial && logger::global_logger->get_baud_rate() != 0) { - ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please " - "disable logging over the serial port by setting logger->baud_rate to 0."); - } -#endif + this->check_logger_conflict_(); } void UARTComponent::write_byte(uint8_t data) { @@ -378,6 +367,19 @@ int UARTComponent::peek() { return data; } +void UARTComponent::check_logger_conflict_() { +#ifdef USE_LOGGER + if (this->hw_serial_ == nullptr || logger::global_logger->get_baud_rate() == 0) { + return; + } + + if (this->hw_serial_ == logger::global_logger->get_hw_serial()) { + ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please " + "disable logging over the serial port by setting logger->baud_rate to 0."); + } +#endif +} + void UARTDevice::check_uart_settings(uint32_t baud_rate, uint8_t stop_bits) { if (this->parent_->baud_rate_ != baud_rate) { ESP_LOGE(TAG, " Invalid baud_rate: Integration requested baud_rate %u but you have %u!", baud_rate, diff --git a/esphome/components/uart/uart.h b/esphome/components/uart/uart.h index 0e92fed0dc..76e7496b80 100644 --- a/esphome/components/uart/uart.h +++ b/esphome/components/uart/uart.h @@ -76,6 +76,7 @@ class UARTComponent : public Component, public Stream { void set_stop_bits(uint8_t stop_bits) { this->stop_bits_ = stop_bits; } protected: + void check_logger_conflict_(); bool check_read_timeout_(size_t len = 1); friend class UARTDevice;