ESPHome
2026.12.0-dev
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
#ifdef USE_BINARY_SENSOR
25
SUB_BINARY_SENSOR(connected)
26
#endif
27
#ifdef USE_SENSOR
28
SUB_SENSOR(disconnects)
29
#endif
30
31
public
:
32
void
set_host(
const
char
*host) { this->
link_
.
set_host
(host); }
33
void
set_port(uint16_t port) { this->
link_
.
set_port
(port); }
34
void
set_reconnect_interval(
uint32_t
ms) { this->
link_
.
set_reconnect_interval
(ms); }
35
void
set_idle_timeout(
uint32_t
ms) { this->
link_
.
set_idle_timeout
(ms); }
36
#ifdef USE_SOCKET_TCP_LISTENER
37
void
set_server(
bool
server
) { this->
server_
=
server
; }
38
#ifdef USE_SOCKET_IPV4_ALLOW
39
void
set_allow(
const
socket::Ipv4AllowEntry
*entries,
size_t
count
) { this->
listener_
.
set_allow
(entries,
count
); }
40
#endif
41
#endif
42
43
void
setup()
override
;
44
void
loop()
override
;
45
void
dump_config()
override
;
46
void
on_shutdown()
override
;
47
float
get_setup_priority()
const override
{
return
setup_priority::AFTER_WIFI
; }
48
49
protected
:
50
void
sync_link_
();
51
void
read_socket_
();
52
void
read_uart_
();
53
void
discard_uart_
();
54
55
static
constexpr
size_t
READ_CHUNK
= 128;
56
// Scratch size for dropping stale UART bytes on connect.
57
static
constexpr
size_t
DISCARD_CHUNK
= 32;
58
59
socket::TcpClientLink
link_
;
60
#ifdef USE_SOCKET_TCP_LISTENER
61
socket::TcpListener
listener_
;
62
#endif
63
// Loop start time of the last socket-to-UART write; sizes the next paced write.
64
uint32_t
last_write_ms_
{0};
65
#ifdef USE_SENSOR
66
uint32_t
disconnects_
{0};
67
#endif
68
bool
server_
{
false
};
69
// The link state loop() saw last; edges clear the buffer and publish the sensor.
70
bool
link_was_up_
{
false
};
71
// A read stopped before EAGAIN. ready() stays false until new data arrives.
72
bool
rx_pending_
{
false
};
73
};
74
75
}
// namespace esphome::uart_tcp
binary_sensor.h
esphome::Component
Definition
component.h:148
esphome::socket::TcpClientLinkBase::set_host
void set_host(const char *host)
Definition
tcp_client_link.h:27
esphome::socket::TcpClientLinkBase::set_port
void set_port(uint16_t port)
Definition
tcp_client_link.h:28
esphome::socket::TcpClientLinkBase::set_idle_timeout
void set_idle_timeout(uint32_t ms)
0 leaves the link up.
Definition
tcp_client_link.h:32
esphome::socket::TcpClientLinkBase::set_reconnect_interval
void set_reconnect_interval(uint32_t ms)
Definition
tcp_client_link.h:29
esphome::socket::TcpClientLink
A link with its own outgoing buffer.
Definition
tcp_client_link.h:112
esphome::socket::TcpListener
The server side of a bridged TCP link: owns the listen socket and the allow list.
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:15
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:57
esphome::uart_tcp::UartTcp::link_was_up_
bool link_was_up_
Definition
uart_tcp.h:70
esphome::uart_tcp::UartTcp::link_
socket::TcpClientLink link_
Definition
uart_tcp.h:59
esphome::uart_tcp::UartTcp::READ_CHUNK
static constexpr size_t READ_CHUNK
Definition
uart_tcp.h:55
esphome::uart_tcp::UartTcp::listener_
socket::TcpListener listener_
Definition
uart_tcp.h:61
esphome::uart_tcp::UartTcp::read_socket_
void read_socket_()
Definition
uart_tcp.cpp:73
esphome::uart_tcp::UartTcp::disconnects_
uint32_t disconnects_
Definition
uart_tcp.h:66
esphome::uart_tcp::UartTcp::read_uart_
void read_uart_()
Definition
uart_tcp.cpp:111
esphome::uart_tcp::UartTcp::discard_uart_
void discard_uart_()
Definition
uart_tcp.cpp:98
esphome::uart_tcp::UartTcp::rx_pending_
bool rx_pending_
Definition
uart_tcp.h:72
esphome::uart_tcp::UartTcp::last_write_ms_
uint32_t last_write_ms_
Definition
uart_tcp.h:64
esphome::uart_tcp::UartTcp::server_
bool server_
Definition
uart_tcp.h:68
esphome::uart_tcp::UartTcp::sync_link_
void sync_link_()
Definition
uart_tcp.cpp:54
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:56
esphome::uart_tcp
Definition
uart_tcp.cpp:10
uint32_t
static void uint32_t
Definition
crash_handler.cpp:179
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
server
httpd_handle_t server
Definition
web_server_idf.cpp:184
Generated by
1.12.0