Skip to content

Document UDP.isValid() / UDP.status() #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/content/reference/firmware.md
Original file line number Diff line number Diff line change
Expand Up @@ -3260,6 +3260,58 @@ UDP udp;
udp.leaveMulticast();
```

### isValid()

UDP reading APIs do not check for socket validity. On typical listening loop isValid() can be used to avoid waiting for ever for data will never come.

```cpp

// EXAMPLE USAGE

// UDP Port used for two way communication
unsigned int localPort = 8888;

// An UDP instance to let us send and receive packets over UDP
UDP Udp;

void setup() {
// start the UDP
Udp.begin(localPort);

// Print your device IP Address via serial
Serial.begin(9600);
Serial.println(WiFi.localIP());
}

void loop() {
if (Udp.isValid()) {
// Check if data has been received
if (Udp.parsePacket() > 0) {

// Read first char of data received
char c = Udp.read();

// Ignore other chars
Udp.flush();

// Store sender ip and port
IPAddress ipAddress = Udp.remoteIP();
int port = Udp.remotePort();

// Echo back data to sender
Udp.beginPacket(ipAddress, port);
Udp.write(c);
Udp.endPacket();
}
}
else { // UDP Sockect not valid, restart it
Udp.Stop();
Udp.begin(localPort);
}
}

```

{{/if}}


Expand Down