H.264 Codec for ESP32-S3
Loading...
Searching...
No Matches
UDPPrint.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file UDPPrint.h -
5 * @brief header-only Arduino Print implementation that sends data over
6 */
7
8#include <Arduino.h>
9#include <WiFiUdp.h>
10#include <WiFi.h>
11
12namespace esp_h264 {
13
14/**
15 * @brief A Print implementation that buffers and sends data over UDP
16 *
17 * UDPPrint extends Arduino's Print class to send data over UDP networks.
18 * It internally buffers data and sends UDP packets when the buffer fills
19 * or when explicitly flushed.
20 *
21 * @note WiFi connection must be established before using this class
22 * @note Data is buffered up to BUF_SIZE (1400 bytes) before automatic
23 * transmission
24 *
25 * Example usage:
26 * @code
27 * #include "UDPPrint.h"
28 *
29 * UDPPrint up;
30 * up.begin("192.168.1.100", 5000);
31 * up.print("Hello World");
32 * up.flush(); // Send immediately
33 * @endcode
34 */
35class UDPPrint : public Print {
36 public:
37 UDPPrint() : udp_(new WiFiUDP()), destPort_(0), bufPos_(0) {}
38 ~UDPPrint() { delete udp_; }
39
40 bool initWiFi(const char* ssid, const char* password) {
41 ESP_LOGD(TAG, "initWiFi");
42 WiFi.mode(WIFI_STA);
43 WiFi.begin(ssid, password);
44 WiFi.setSleep(false);
45 WiFi.setTxPower(WIFI_POWER_19_5dBm);
46 unsigned long start = millis();
47 while (WiFi.status() != WL_CONNECTED) {
48 delay(200);
49 if (millis() - start > 15000) return false;
50 Serial.print(".");
51 }
52 Serial.println();
53 Serial.print("WiFi connected, IP address: ");
54 Serial.println(WiFi.localIP());
55 return true;
56 }
57
58 // Begin with numeric IP string
59 bool begin(const char* destIp, uint16_t destPort) {
60 destIp_ = String(destIp);
61 destPort_ = destPort;
62 // Don't call udp_->begin() to bind to a local port; beginPacket will handle
63 // it
64 bufPos_ = 0;
65 return true;
66 }
67
68 // Begin with IPAddress
69 bool begin(const IPAddress& addr, uint16_t destPort) {
70 destIp_ = addr.toString();
71 destPort_ = destPort;
72 bufPos_ = 0;
73 return true;
74 }
75
76 // Write a single byte
77 size_t write(uint8_t ch) override {
78 buffer_[bufPos_++] = ch;
79 if (bufPos_ >= sizeof(buffer_)) flush();
80 return 1;
81 }
82
83 // Write a buffer
84 size_t write(const uint8_t* data, size_t size) override {
85 size_t written = 0;
86 while (size > 0) {
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);
91 bufPos_ += tocopy;
92 written += tocopy;
93 size -= tocopy;
94 }
95 flush();
96 ESP_LOGI(TAG, "Buffered and sent %u bytes to %s:%u",
97 (unsigned)written, destIp_.c_str(), destPort_);
98 return written;
99 }
100
101 // Send buffered data immediately
102 void flush() {
103 if (bufPos_ == 0) return;
104 if (!udp_) return;
105
106 size_t to_send = bufPos_;
107
108 bool sent = false;
109 size_t written = 0;
110 int retry = 10;
111 while (!sent && retry-- > 0) {
112 if (!udp_->beginPacket(destIp_.c_str(), destPort_)) {
113 // unable to start packet
114 bufPos_ = 0;
115 return;
116 }
117 written = udp_->write(buffer_, bufPos_);
118 sent = udp_->endPacket();
119 delay(write_delay_ms);
120 }
121
122 if (!sent) {
123 ESP_LOGE(TAG,
124 "Failed to send UDP packet %u bytes to %s:%u after retries",
125 (unsigned)to_send, destIp_.c_str(), destPort_);
126 }
127 bufPos_ = 0;
128 }
129
130 // Convenience: send immediate string
131 size_t print(const char* s) {
132 size_t len = write((const uint8_t*)s, strlen(s));
133 flush();
134 return len;
135 }
136
137 size_t println(const char* s) {
138 char buffer[strlen(s) + 2]{};
139 strcpy(buffer, s);
140 strcat(buffer, "\n");
141 return print(buffer);
142 }
143
144 /// Set delay after sending a UDP packet (default 100ms)
145 void setWriteDelay(unsigned long ms) { write_delay_ms = ms; }
146
147 private:
148 WiFiUDP* udp_;
149 String destIp_;
150 uint16_t destPort_;
151 const char* TAG = "UDPPrint";
152 static constexpr size_t BUF_SIZE = 1200;
153 uint8_t buffer_[BUF_SIZE];
154 size_t bufPos_;
155 size_t write_delay_ms = 20;
156};
157
158} // namespace esp_h264
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