ESPHome
2026.10.0
Loading...
Searching...
No Matches
esphome
components
uart_tcp
uart_tcp.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
esphome/core/defines.h
"
4
#ifdef USE_BINARY_SENSOR
5
#include "
esphome/components/binary_sensor/binary_sensor.h
"
6
#endif
7
#ifdef USE_SENSOR
8
#include "
esphome/components/sensor/sensor.h
"
9
#endif
10
#include "
esphome/components/socket/tcp_client_link.h
"
11
#ifdef USE_SOCKET_TCP_LISTENER
12
#include "
esphome/components/socket/tcp_listener.h
"
13
#endif
14
#include "
esphome/components/uart/uart.h
"
15
#include "
esphome/core/component.h
"
16
17
#include <cstdint>
18
#include <memory>
19
20
namespace
esphome::uart_tcp
{
21
23
class
UartTcp
final :
public
Component
,
public
uart::UARTDevice
{
24
public
:
25
void
set_host
(
const
char
*host) { this->
link_
.
set_host
(host); }
26
void
set_port
(uint16_t port) { this->
link_
.
set_port
(port); }
27
void
set_reconnect_interval
(
uint32_t
ms) { this->
link_
.
set_reconnect_interval
(ms); }
28
#ifdef USE_BINARY_SENSOR
29
void
set_connected_sensor
(
binary_sensor::BinarySensor
*sensor) { this->
connected_sensor_
= sensor; }
30
#endif
31
#ifdef USE_SENSOR
32
void
set_disconnects_sensor
(
sensor::Sensor
*sensor) { this->
disconnects_sensor_
= sensor; }
33
#endif
34
#ifdef USE_SOCKET_TCP_LISTENER
35
void
set_server
(
bool
server) { this->
server_
= server; }
36
#ifdef USE_SOCKET_IPV4_ALLOW
37
void
set_allow
(
const
socket::Ipv4AllowEntry
*entries,
size_t
count
) { this->
listener_
.
set_allow
(entries,
count
); }
38
#endif
39
#endif
40
41
void
setup
()
override
;
42
void
loop
()
override
;
43
void
dump_config
()
override
;
44
void
on_shutdown
()
override
;
45
float
get_setup_priority
()
const override
{
return
setup_priority::AFTER_WIFI
; }
46
47
protected
:
48
void
sync_link_
();
49
void
read_socket_
();
50
void
read_uart_
();
51
void
discard_uart_
();
52
53
static
constexpr
size_t
READ_CHUNK
= 128;
54
// Scratch size for dropping stale UART bytes on connect.
55
static
constexpr
size_t
DISCARD_CHUNK
= 32;
56
57
socket::TcpClientLink
link_
;
58
#ifdef USE_SOCKET_TCP_LISTENER
59
socket::TcpListener
listener_
;
60
#endif
61
#ifdef USE_BINARY_SENSOR
62
binary_sensor::BinarySensor
*
connected_sensor_
{
nullptr
};
63
#endif
64
// Loop start time of the last socket-to-UART write; sizes the next paced write.
65
uint32_t
last_write_ms_
{0};
66
#ifdef USE_SENSOR
67
sensor::Sensor
*
disconnects_sensor_
{
nullptr
};
68
uint32_t
disconnects_
{0};
69
#endif
70
bool
server_
{
false
};
71
// The link state loop() saw last; edges clear the buffer and publish the sensor.
72
bool
link_was_up_
{
false
};
73
// A read stopped before EAGAIN. ready() stays false until new data arrives.
74
bool
rx_pending_
{
false
};
75
};
76
77
}
// namespace esphome::uart_tcp
binary_sensor.h
esphome::Component
Definition
component.h:145
esphome::binary_sensor::BinarySensor
Base class for all binary_sensor-type classes.
Definition
binary_sensor.h:33
esphome::sensor::Sensor
Base-class for all sensors.
Definition
sensor.h:47
esphome::socket::TcpClientLink
A reconnecting TCP stream driven from loop().
Definition
tcp_client_link.h:22
esphome::socket::TcpClientLink::set_reconnect_interval
void set_reconnect_interval(uint32_t ms)
Definition
tcp_client_link.h:26
esphome::socket::TcpClientLink::set_port
void set_port(uint16_t port)
Definition
tcp_client_link.h:25
esphome::socket::TcpClientLink::set_host
void set_host(const char *host)
Definition
tcp_client_link.h:24
esphome::socket::TcpListener
The server side of a bridged TCP link: owns the listen socket and the allow list, accepts one peer at...
Definition
tcp_listener.h:21
esphome::socket::TcpListener::set_allow
void set_allow(const Ipv4AllowEntry *entries, size_t count)
Definition
tcp_listener.h:24
esphome::uart::UARTDevice
Definition
uart.h:12
esphome::uart_tcp::UartTcp
Copies raw bytes between one hardware UART and one TCP socket.
Definition
uart_tcp.h:23
esphome::uart_tcp::UartTcp::DISCARD_CHUNK
static constexpr size_t DISCARD_CHUNK
Definition
uart_tcp.h:55
esphome::uart_tcp::UartTcp::link_was_up_
bool link_was_up_
Definition
uart_tcp.h:72
esphome::uart_tcp::UartTcp::link_
socket::TcpClientLink link_
Definition
uart_tcp.h:57
esphome::uart_tcp::UartTcp::loop
void loop() override
Definition
uart_tcp.cpp:133
esphome::uart_tcp::UartTcp::set_reconnect_interval
void set_reconnect_interval(uint32_t ms)
Definition
uart_tcp.h:27
esphome::uart_tcp::UartTcp::set_disconnects_sensor
void set_disconnects_sensor(sensor::Sensor *sensor)
Definition
uart_tcp.h:32
esphome::uart_tcp::UartTcp::disconnects_sensor_
sensor::Sensor * disconnects_sensor_
Definition
uart_tcp.h:67
esphome::uart_tcp::UartTcp::on_shutdown
void on_shutdown() override
Definition
uart_tcp.cpp:53
esphome::uart_tcp::UartTcp::READ_CHUNK
static constexpr size_t READ_CHUNK
Definition
uart_tcp.h:53
esphome::uart_tcp::UartTcp::listener_
socket::TcpListener listener_
Definition
uart_tcp.h:59
esphome::uart_tcp::UartTcp::read_socket_
void read_socket_()
Definition
uart_tcp.cpp:81
esphome::uart_tcp::UartTcp::set_host
void set_host(const char *host)
Definition
uart_tcp.h:25
esphome::uart_tcp::UartTcp::connected_sensor_
binary_sensor::BinarySensor * connected_sensor_
Definition
uart_tcp.h:62
esphome::uart_tcp::UartTcp::set_server
void set_server(bool server)
Definition
uart_tcp.h:35
esphome::uart_tcp::UartTcp::disconnects_
uint32_t disconnects_
Definition
uart_tcp.h:68
esphome::uart_tcp::UartTcp::set_allow
void set_allow(const socket::Ipv4AllowEntry *entries, size_t count)
Definition
uart_tcp.h:37
esphome::uart_tcp::UartTcp::set_connected_sensor
void set_connected_sensor(binary_sensor::BinarySensor *sensor)
Definition
uart_tcp.h:29
esphome::uart_tcp::UartTcp::read_uart_
void read_uart_()
Definition
uart_tcp.cpp:126
esphome::uart_tcp::UartTcp::dump_config
void dump_config() override
Definition
uart_tcp.cpp:34
esphome::uart_tcp::UartTcp::set_port
void set_port(uint16_t port)
Definition
uart_tcp.h:26
esphome::uart_tcp::UartTcp::discard_uart_
void discard_uart_()
Definition
uart_tcp.cpp:113
esphome::uart_tcp::UartTcp::rx_pending_
bool rx_pending_
Definition
uart_tcp.h:74
esphome::uart_tcp::UartTcp::last_write_ms_
uint32_t last_write_ms_
Definition
uart_tcp.h:65
esphome::uart_tcp::UartTcp::server_
bool server_
Definition
uart_tcp.h:70
esphome::uart_tcp::UartTcp::sync_link_
void sync_link_()
Definition
uart_tcp.cpp:60
esphome::uart_tcp::UartTcp::get_setup_priority
float get_setup_priority() const override
Definition
uart_tcp.h:45
esphome::uart_tcp::UartTcp::setup
void setup() override
Definition
uart_tcp.cpp:17
component.h
defines.h
count
unsigned count
Definition
gif.cpp:51
esphome::setup_priority::AFTER_WIFI
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition
component.h:55
esphome::uart_tcp
Definition
uart_tcp.cpp:10
uint32_t
static void uint32_t
Definition
crash_handler.cpp:153
sensor.h
esphome::socket::Ipv4AllowEntry
One allowed IPv4 network, network byte order, host bits cleared.
Definition
ipv4_allow.h:14
tcp_client_link.h
tcp_listener.h
uart.h
Generated by
1.12.0