8000 update example with UDP showing remote v4 or v6 address · esp8266/Arduino@402c35e · GitHub
[go: up one dir, main page]

Skip to content

Commit 402c35e

Browse files
committed
update example with UDP showing remote v4 or v6 address
1 parent 389bf07 commit 402c35e

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

libraries/esp8266/examples/IPv6/IPv6.ino

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,61 @@
2121
*/
2222

2323
#include <ESP8266WiFi.h>
24+
#include <WiFiUdp.h>
2425

2526
#define SSID "ssid"
2627
#define PSK "psk"
2728

28-
#define FQDN "www.google.com" // with both IPv4 & IPv6 addresses
29-
#define FQDN6 "par21s04-in-x04.1e100.net" // does not resolve in IPv4 (wanted: better example)
29+
#define FQDN F("www.google.com") // with both IPv4 & IPv6 addresses
30+
#define FQDN6 F("par21s04-in-x04.1e100.net") // does not resolve in IPv4 (wanted: better example)
3031
#define STATUSDELAY_MS 30000
32+
#define TCP_PORT 23
33+
#define UDP_PORT 1111
3134

32-
WiFiServer statusServer(23);
35+< 8000 div class="diff-text-inner">WiFiServer statusServer(TCP_PORT);
36+
WiFiUDP udp;
3337

3438
void fqdn (Print& out, const String& fqdn) {
35-
out.print("resolving ");
39+
out.print(F("resolving "));
3640
out.print(fqdn);
37-
out.print(": ");
41+
out.print(F(": "));
3842
IPAddress result;
3943
if (WiFi.hostByName(fqdn.c_str(), result)) {
4044
result.printTo(out);
4145
out.println();
4246
} else
43-
out.println("timeout or not found");
47+
out.println(F("timeout or not found"));
4448
}
4549

4650
void status (Print& out) {
4751
out.println();
4852

49-
out.print("address= ");
53+
out.print(F("address= "));
5054
WiFi.localIP().printTo(out);
5155
out.println();
52-
out.print("gateway= ");
56+
out.print(F("gateway= "));
5357
WiFi.gatewayIP().printTo(out);
5458
out.println();
55-
out.print("netmask= ");
59+
out.print(F("netmask= "));
5660
WiFi.subnetMask().printTo(out);
5761
out.println();
5862

5963
for (int i = 0; i < 3; i++) {
60-
out.print("dns");
64+
out.print(F("dns"));
6165
out.print(i);
62-
out.print("= ");
66+
out.print(F("= "));
6367
WiFi.dnsIP(i).printTo(out);
6468
out.println();
6569
}
6670

6771
#if LWIP_IPV6
68-
out.println("Try me at these addresses (with 'telnet <addr>'):");
69-
out.print("IPv6 link-scope(intranet)= ");
72+
out.println(F("Try me at these addresses:"));
73+
out.println(F("(with 'telnet <addr>')"));
74+
out.println(F("(with 'nc -u <addr> 1111')"));
75+
out.print(F("IPv6 link-scope(intranet)= "));
7076
WiFi.localIP6Link().printTo(out);
7177
out.println();
72-
out.print("IPV6 global-scope(internet)= ");
78+
out.print(F("IPV6 global-scope(internet)= "));
7379
WiFi.localIP6Global().printTo(out);
7480
out.println();
7581
#endif
@@ -87,22 +93,29 @@ void setup() {
8793
Serial.println();
8894
Serial.println(ESP.getFullVersion());
8995

96+
WiFi.setSleepMode(WIFI_NONE_SLEEP);
9097
WiFi.mode(WIFI_STA);
9198
WiFi.begin(SSID, PSK);
9299
while (WiFi.status() != WL_CONNECTED)
93100
{
94101
Serial.print('.');
95102
delay(500);
96103
}
97-
Serial.println("connected");
104+
Serial.println(F("connected: "));
98105

99106
statusServer.begin();
107+
udp.begin(UDP_PORT);
108+
109+
Serial.print(F("TCP server on port "));
110+
Serial.print(TCP_PORT);
111+
Serial.print(F(" - UDP server on port "));
112+
Serial.println(UDP_PORT);
100113
}
101114

102115
unsigned long statusTimeMs = 0;
103116

104117
void loop() {
105-
118+
106119
if (statusServer.hasClient()) {
107120
WiFiClient cli = statusServer.available();
108121
cli.setNoDelay(0); // should be default, was not default
@@ -111,6 +124,34 @@ void loop() {
111124
cli.stop();
112125
}
113126

127+
// if there's data available, read a packet
128+
int packetSize = udp.parsePacket();
129+
if (packetSize) {
130+
Serial.print(F("udp received "));
131+
Serial.print(packetSize);
132+
Serial.print(F(" bytes from "));
133+
udp.remoteIP().printTo(Serial);
134+
Serial.print(F(" :"));
135+
Serial.println(udp.remotePort());
136+
137+
char* buffer = (char*)malloc(packetSize + 1);
138+
if (buffer) {
139+
udp.read(buffer, packetSize);
140+
buffer[packetSize] = 0;
141+
Serial.println(F("Contents:"));
142+
Serial.println(buffer);
143+
144+
// send a reply, to the IP address and port that sent us the packet we received
145+
udp.beginPacket(udp.remoteIP(), udp.remotePort());
146+
udp.write(buffer, packetSize);
147+
udp.endPacket();
148+
free(buffer);
149+
} else {
150+
Serial.println(F("udp: malloc failed"));
151+
while (udp.read() >= 0);
152+
}
153+
}
154+
114155
unsigned long now = millis();
115156
if (now > statusTimeMs) {
116157
statusTimeMs = now + STATUSDELAY_MS;

0 commit comments

Comments
 (0)
0