From d7a21bd2471442c192a6ef701d8f5e461cf46048 Mon Sep 17 00:00:00 2001 From: aledand Date: Tue, 15 Dec 2015 11:47:28 +0100 Subject: [PATCH 1/3] Update firmware.md spark/firmware #766 PR companion --- src/content/reference/firmware.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/content/reference/firmware.md b/src/content/reference/firmware.md index aba059b8cc..6a1046a9d5 100644 --- a/src/content/reference/firmware.md +++ b/src/content/reference/firmware.md @@ -3260,6 +3260,22 @@ UDP udp; udp.leaveMulticast(); ``` +### isValid() + +Check if the previusly bind UDP socket is valid. + +```cpp + +// SYNTAX + +if (udp.isValid()) { + // seek available data ... +} else { + // udp socket not valid +} + +``` + {{/if}} From 126cb3355163e1a5ba312427333b7b503829573c Mon Sep 17 00:00:00 2001 From: aledand Date: Tue, 15 Dec 2015 14:28:11 +0100 Subject: [PATCH 2/3] Update firmware.md --- src/content/reference/firmware.md | 48 +++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/content/reference/firmware.md b/src/content/reference/firmware.md index 6a1046a9d5..19edf9a68e 100644 --- a/src/content/reference/firmware.md +++ b/src/content/reference/firmware.md @@ -3262,16 +3262,52 @@ udp.leaveMulticast(); ### isValid() -Check if the previusly bind UDP socket is valid. +UDP reading APIs do not check for socket validity. On typical listening loop isValid() can be used to avoid waiting for ever for data no more coming. ```cpp -// SYNTAX +// 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()); +} -if (udp.isValid()) { - // seek available data ... -} else { - // udp socket not valid +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); + } } ``` From c6f0f77a8deb3c1e4a593ff91ac24abbc101215e Mon Sep 17 00:00:00 2001 From: aledand Date: Tue, 15 Dec 2015 14:30:13 +0100 Subject: [PATCH 3/3] Update firmware.md --- src/content/reference/firmware.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/reference/firmware.md b/src/content/reference/firmware.md index 19edf9a68e..9d588e1d69 100644 --- a/src/content/reference/firmware.md +++ b/src/content/reference/firmware.md @@ -3262,7 +3262,7 @@ 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 no more coming. +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