Skip to content

Commit b699bdc

Browse files
committed
Added example WifiUdpSendReceiveString
1 parent 15727e9 commit b699bdc

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
2+
/*
3+
WiFi UDP Send and Receive String
4+
5+
This sketch wait an UDP packet on localPort using a WiFi shield.
6+
When a packet is received an Acknowledge packet is sent to the client on port remotePort
7+
8+
Circuit:
9+
* WiFi shield attached
10+
11+
created 30 December 2012
12+
by dlf (Metodo2 srl)
13+
14+
*/
15+
16+
17+
#include <SPI.h>
18+
#include <WiFi.h>
19+
#include <WiFiUdp.h>
20+
21+
int status = WL_IDLE_STATUS;
22+
char ssid[] = "yourNetwork"; // your network SSID (name)
23+
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
24+
int keyIndex = 0; // your network key Index number (needed only for WEP)
25+
26+
unsigned int localPort = 2390; // local port to listen on
27+
28+
char packetBuffer[255]; //buffer to hold incoming packet
29+
char ReplyBuffer[] = "acknowledged"; // a string to send back
30+
31+
WiFiUDP Udp;
32+
33+
void setup() {
34+
//Initialize serial and wait for port to open:
35+
Serial.begin(9600);
36+
while (!Serial) {
37+
; // wait for serial port to connect. Needed for Leonardo only
38+
}
39+
40+
// check for the presence of the shield:
41+
if (WiFi.status() == WL_NO_SHIELD) {
42+
Serial.println("WiFi shield not present");
43+
// don't continue:
44+
while(true);
45+
}
46+
47+
// attempt to connect to Wifi network:
48+
while ( status != WL_CONNECTED) {
49+
Serial.print("Attempting to connect to SSID: ");
50+
Serial.println(ssid);
51+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
52+
status = WiFi.begin(ssid);
53+
54+
// wait 10 seconds for connection:
55+
delay(10000);
56+
}
57+
Serial.println("Connected to wifi");
58+
printWifiStatus();
59+
60+
Serial.println("\nStarting connection to server...");
61+
// if you get a connection, report back via serial:
62+
Udp.begin(localPort);
63+
}
64+
65+
void loop() {
66+
67+
// if there's data available, read a packet
68+
int packetSize = Udp.parsePacket();
69+
if(packetSize)
70+
{
71+
Serial.print("Received packet of size ");
72+
Serial.println(packetSize);
73+
Serial.print("From ");
74+
IPAddress remoteIp = Udp.remoteIP();
75+
Serial.print(remoteIp);
76+
Serial.print(", port ");
77+
Serial.println(Udp.remotePort());
78+
79+
// read the packet into packetBufffer
80+
int len = Udp.read(packetBuffer,255);
81+
if (len >0) packetBuffer[len]=0;
82+
Serial.println("Contents:");
83+
Serial.println(packetBuffer);
84+
85+
// send a reply, to the IP address and port that sent us the packet we received
86+
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
87+
Udp.write(ReplyBuffer);
88+
Udp.endPacket();
89+
}
90+
}
91+
92+
93+
void printWifiStatus() {
94+
// print the SSID of the network you're attached to:
95+
Serial.print("SSID: ");
96+
Serial.println(WiFi.SSID());
97+
98+
// print your WiFi shield's IP address:
99+
IPAddress ip = WiFi.localIP();
100+
Serial.print("IP Address: ");
101+
Serial.println(ip);
102+
103+
// print the received signal strength:
104+
long rssi = WiFi.RSSI();
105+
Serial.print("signal strength (RSSI):");
106+
Serial.print(rssi);
107+
Serial.println(" dBm");
108+
}
109+
110+
111+
112+

0 commit comments

Comments
 (0)