Send and Receive UDP String

This sketch waits for a UDP packet on a local port. When a valid packet is received, an acknowledge packet is sent back to the client on a specified outgoing port.

This sketch waits for a UDP packet on a local port. When a valid packet is received, an acknowledge packet is sent back to the client on a specified outgoing port.

Hardware Required

  • Arduino WiFi Shield

  • Shield-compatible Arduino board

Circuit

The WiFi shield uses pins 10, 11, 12, and 13 for the SPI connection to the HDG104 module. Digital pin 4 is used to control the chip select pin on the SD card.

You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.

WiFiShield bb

image developed using Fritzing. For more circuit examples, see the Fritzing project page

In the above image, the Arduino board would be stacked below the WiFi shield.

Code

1/*
2
3 WiFi UDP Send and Receive String
4
5 This sketch wait an UDP packet on localPort using a WiFi shield.
6
7 When a packet is received an Acknowledge packet is sent to the client on port remotePort
8
9 Circuit:
10
11 * WiFi shield attached
12
13 created 30 December 2012
14
15 by dlf (Metodo2 srl)
16
17 */
18
19#include <SPI.h>
20#include <WiFi.h>
21#include <WiFiUdp.h>
22
23int status = WL_IDLE_STATUS;
24char ssid[] = "yourNetwork"; // your network SSID (name)
25char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
26int keyIndex = 0; // your network key Index number (needed only for WEP)
27
28unsigned int localPort = 2390; // local port to listen on
29
30char packetBuffer[255]; //buffer to hold incoming packet
31char ReplyBuffer[] = "acknowledged"; // a string to send back
32
33WiFiUDP Udp;
34
35void setup() {
36
37 //Initialize serial and wait for port to open:
38
39 Serial.begin(9600);
40
41 while (!Serial) {
42
43 ; // wait for serial port to connect. Needed for native USB port only
44
45 }
46
47 // check for the presence of the shield:
48
49 if (WiFi.status() == WL_NO_SHIELD) {
50
51 Serial.println("WiFi shield not present");
52
53 // don't continue:
54
55 while (true);
56
57 }
58
59 String fv = WiFi.firmwareVersion();
60
61 if (fv != "1.1.0") {
62
63 Serial.println("Please upgrade the firmware");
64
65 }
66
67 // attempt to connect to Wifi network:
68
69 while (status != WL_CONNECTED) {
70
71 Serial.print("Attempting to connect to SSID: ");
72
73 Serial.println(ssid);
74
75 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
76
77 status = WiFi.begin(ssid);
78
79 // wait 10 seconds for connection:
80
81 delay(10000);
82
83 }
84
85 Serial.println("Connected to wifi");
86
87 printWifiStatus();
88
89 Serial.println("\nStarting connection to server...");
90
91 // if you get a connection, report back via serial:
92
93 Udp.begin(localPort);
94}
95
96void loop() {
97
98 // if there's data available, read a packet
99
100 int packetSize = Udp.parsePacket();
101
102 if (packetSize) {
103
104 Serial.print("Received packet of size ");
105
106 Serial.println(packetSize);
107
108 Serial.print("From ");
109
110 IPAddress remoteIp = Udp.remoteIP();
111
112 Serial.print(remoteIp);
113
114 Serial.print(", port ");
115
116 Serial.println(Udp.remotePort());
117
118 // read the packet into packetBufffer
119
120 int len = Udp.read(packetBuffer, 255);
121
122 if (len > 0) {
123
124 packetBuffer[len] = 0;
125
126 }
127
128 Serial.println("Contents:");
129
130 Serial.println(packetBuffer);
131
132 // send a reply, to the IP address and port that sent us the packet we received
133
134 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
135
136 Udp.write(ReplyBuffer);
137
138 Udp.endPacket();
139
140 }
141}
142
143void printWifiStatus() {
144
145 // print the SSID of the network you're attached to:
146
147 Serial.print("SSID: ");
148
149 Serial.println(WiFi.SSID());
150
151 // print your WiFi shield's IP address:
152
153 IPAddress ip = WiFi.localIP();
154
155 Serial.print("IP Address: ");
156
157 Serial.println(ip);
158
159 // print the received signal strength:
160
161 long rssi = WiFi.RSSI();
162
163 Serial.print("signal strength (RSSI):");
164
165 Serial.print(rssi);
166
167 Serial.println(" dBm");
168}

Last revision 2018/08/23 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.