4
5
6
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
40 bool initWiFi(
const char* ssid,
const char* password) {
41 ESP_LOGD(TAG,
"initWiFi");
43 WiFi.begin(ssid, password);
45 WiFi.setTxPower(WIFI_POWER_19_5dBm);
46 unsigned long start = millis();
47 while (WiFi.status() != WL_CONNECTED) {
49 if (millis() - start > 15000)
return false;
53 Serial.print(
"WiFi connected, IP address: ");
54 Serial.println(WiFi.localIP());
59 bool begin(
const char* destIp, uint16_t destPort) {
60 destIp_ = String(destIp);
69 bool begin(
const IPAddress& addr, uint16_t destPort) {
70 destIp_ = addr.toString();
78 buffer_[bufPos_++] = ch;
79 if (bufPos_ >=
sizeof(buffer_))
flush();
87 size_t space =
sizeof(buffer_) - bufPos_;
88 if (space == 0) flush();
89 size_t tocopy = (size < space) ? size : space;
90 memcpy(buffer_ + bufPos_, data + written, tocopy);
96 ESP_LOGI(TAG,
"Buffered and sent %u bytes to %s:%u",
97 (
unsigned)written, destIp_.c_str(), destPort_);
103 if (bufPos_ == 0)
return;
106 size_t to_send = bufPos_;
111 while (!sent && retry-- > 0) {
112 if (!udp_->beginPacket(destIp_.c_str(), destPort_)) {
117 written = udp_->write(buffer_, bufPos_);
118 sent = udp_->endPacket();
119 delay(write_delay_ms);
124 "Failed to send UDP packet %u bytes to %s:%u after retries",
125 (
unsigned)to_send, destIp_.c_str(), destPort_);
132 size_t len = write((
const uint8_t*)s, strlen(s));
138 char buffer[strlen(s) + 2]{};
140 strcat(buffer,
"\n");
141 return print(buffer);
151 const char* TAG =
"UDPPrint";
152 static constexpr size_t BUF_SIZE = 1200;
155 size_t write_delay_ms = 20;
A Print implementation that buffers and sends data over UDP.
Definition: UDPPrint.h:35
size_t write(const uint8_t *data, size_t size) override
Definition: UDPPrint.h:84
~UDPPrint()
Definition: UDPPrint.h:38
void setWriteDelay(unsigned long ms)
Set delay after sending a UDP packet (default 100ms)
Definition: UDPPrint.h:145
size_t print(const char *s)
Definition: UDPPrint.h:131
UDPPrint()
Definition: UDPPrint.h:37
bool begin(const char *destIp, uint16_t destPort)
Definition: UDPPrint.h:59
bool initWiFi(const char *ssid, const char *password)
Definition: UDPPrint.h:40
size_t write(uint8_t ch) override
Definition: UDPPrint.h:77
bool begin(const IPAddress &addr, uint16_t destPort)
Definition: UDPPrint.h:69
void flush()
Definition: UDPPrint.h:102
size_t println(const char *s)
Definition: UDPPrint.h:137
Definition: H264Decoder.h:31