7#include "TinyRobotics/utils/BaseStream.h"
9namespace tinyrobotics {
12
13
14
15
16
17
18
19
30 UDPStream(
const char *ssid,
const char *password) {
32 setPassword(password);
42 void setUDP(UDP &udp) { p_udp = &udp; };
49
50
51
53 int size = p_udp->available();
56 size = p_udp->parsePacket();
62 bool begin(IPAddress a, uint16_t port) {
64 remote_address_ext = a;
65 remote_port_ext = port;
66 return p_udp->begin(port);
70 bool begin(uint16_t port, uint16_t port_ext = 0) {
72 remote_address_ext = IPAddress((uint32_t)0);
73 remote_port_ext = port_ext != 0 ? port_ext : port;
75 return p_udp->begin(port);
81 return p_udp->beginMulticast(address,port);
86 uint16_t result = p_udp->remotePort();
87 return result != 0 ? result : remote_port_ext;
93 if ((uint32_t)remote_address_ext == 0) {
94 remote_address_ext = p_udp->remoteIP();
98 return remote_address_ext;
103 p_udp->beginPacket(remoteIP(), remotePort());
104 size_t result = p_udp->write(data, len);
111 size_t avail = available();
112 size_t bytes_read = 0;
115 bytes_read = p_udp->readBytes((uint8_t *)data, len);
120 void setSSID(
const char *ssid) {
this->ssid = ssid; }
122 void setPassword(
const char *pwd) {
this->password = pwd; }
127 uint16_t remote_port_ext = 0;
128 IPAddress remote_address_ext;
129 const char *ssid =
nullptr;
130 const char *password =
nullptr;
133 Serial.print(WiFi.localIP());
135 Serial.println(remote_port_ext);
140 if (WiFi.status() != WL_CONNECTED && ssid !=
nullptr &&
141 password !=
nullptr) {
142 WiFi.begin(ssid, password);
143 while (WiFi.status() != WL_CONNECTED) {
148 if (WiFi.status() == WL_CONNECTED) {
149 esp_wifi_set_ps(WIFI_PS_NONE);
Base class for all Streams. It relies on write(const uint8_t *buffer, size_t size) and readBytes(uint...
Definition: BaseStream.h:20
A UDP class which makes sure that we can use UDP as AudioSource and AudioSink. By default the WiFiUDP...
Definition: UDPStream.h:21
IPAddress remoteIP()
We use the same remote ip as defined in begin for write.
Definition: UDPStream.h:91
void connect()
connect to WIFI if necessary
Definition: UDPStream.h:139
uint16_t remotePort()
We use the same remote port as defined in begin for write.
Definition: UDPStream.h:85
size_t readBytes(uint8_t *data, size_t len) override
Reads bytes using WiFi::readBytes.
Definition: UDPStream.h:110
int available() override
Definition: UDPStream.h:52
size_t write(const uint8_t *data, size_t len) override
Replys will be sent to the initial remote caller.
Definition: UDPStream.h:102
int availableForWrite()
Definition: UDPStream.h:46
UDPStream()=default
Default Constructor.
void setUDP(UDP &udp)
Defines an alternative UDP object. By default we use WiFiUDP.
Definition: UDPStream.h:42
bool beginMulticast(IPAddress address, uint16_t port)
Starts to receive data in multicast from/with the indicated address / port.
Definition: UDPStream.h:79
UDPStream(UDP &udp)
Constructor which defines an alternative UDP object. By default we use WiFiUDP.
Definition: UDPStream.h:38
bool begin(uint16_t port, uint16_t port_ext=0)
Starts to receive data from/with the indicated port.
Definition: UDPStream.h:70
bool begin(IPAddress a, uint16_t port)
Starts to send data to the indicated address / port.
Definition: UDPStream.h:62
UDPStream(const char *ssid, const char *password)
Convinience constructor which defines the optional ssid and password.
Definition: UDPStream.h:30