WiFi UDP Send Receive
#include <>
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 client on a specified
outgoing port.
LAST REVISION: 10/05/2022, 08:00 AM
This sketch waits for a UDP packet on a local port. When a valid packet is
received, an acknowledge packet is sent back to 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 SPI connection to
HDG104 module. Digital pin 4 is used to control chip select pin on SD
card.
You should have access to a 802.11b/g wireless network that connects to
internet for this example. You will need to change network settings in
sketch to correspond to your particular networks SSID.
WiFiShield bb
image developed using Fritzing. For more circuit examples, see Fritzing
project page
In above image, Arduino board would be stacked below WiFi shield.
Code
/*H*******************************************************
WiFi UDP Send and Receive String
This sketch wait an UDP packet on localPort using a WiFi shield.
When a packet is received an Acknowledge packet is sent to client on port remotePort
Circuit:
* WiFi shield attached
created 30 December 2012
by dlf (Metodo2 srl)
********************************************************/
#include
#include
#include
//************************* DEFINES ************************************
//************************* PROTOTYPES ************************************
void printWifiStatus();
//************************* VARIABLES ************************************
int status = WL_IDLE_STATUS;
char ssid[] = "yourNetwork"; // NETWORK SSID (NAME)
char pass[] = "secretPassword";// NETWORK PASSWORD (FOR WPA, OR AS KEY FOR WEP)
int keyIndex = 0; // NETWORK KEY INDEX NUMBER (NEEDED ONLY FOR WEP)
unsigned int localPort = 2390; // LOCAL PORT TO LISTEN ON
char packetBuffer[255]; // INCOMING PACKET BUFFER
char ReplyBuffer[] = "acknowledged"; // RESPONSE STRING
WiFiUDP Udp;
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( BAUD ); // INIT SERIAL AND WAIT FOR PORT TO OPEN
while( !Serial )
{
;// WAIT FOR SERIAL PORT TO CONNECT. NEEDED FOR NATIVE USB PORT ONLY
}
if( WiFi.status() == WL_NO_SHIELD ) // CHECK FOR PRESENCE OF SHIELD
{
Serial.println( "WiFi shield not present" );
while( true ); // DON'T CONTINUE
}
String fv = WiFi.firmwareVersion();
if( fv != "1.1.0" )
Serial.println("Please upgrade firmware");
while( status != WL_CONNECTED ) // ATTEMPT TO CONNECT TO Wifi NETWORK
{
Serial.print( "Attempting to connect to SSID: ");
Serial.println( ssid );
// CONNECT TO WPA/WPA2 NETWORK. cHANGE THIS LINE IF USING OPEN OR WEP NETWORK
status = WiFi.begin( ssid );
delay( 10000 ); // WAIT 10 SECONDS FOR CONNECTION
}
Serial.println( "Connected to wifi" );
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
int packetSize = Udp.parsePacket();
if( packetSize ) // IF THERE'S DATA AVAILABLE, READ A PACKE
{
Serial.print( "Received packet of size ");
Serial.println( packetSize);
Serial.print( "From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print( remoteIp);
Serial.print( ", port ");
Serial.println( Udp.remotePort());
int len = Udp.read( packetBuffer, 255 ); // READ PACKET INTO BUFFFER
if( len > 0 )
packetBuffer[len] = 0;
Serial.println("Contents:");
Serial.println(packetBuffer);
// SEND A REPLY, TO IP ADDRESS AND PORT THAT SENT US PACKET RECEIVED
Udp.beginPacket( Udp.remoteIP(), Udp.remotePort());
Udp.write( ReplyBuffer );
Udp.endPacket();
}
}
/*F********************************************************************
*
**********************************************************************/
void
printWifiStatus()
{
Serial.print("SSID: "); // PRINT SSID OF NETWORK ATTACHED TO
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print( "IP Address: ");
Serial.println( ip ); // PRINT WiFi SHIELD'S IP
long rssi = WiFi.RSSI();
Serial.print( "signal strength (RSSI):");
Serial.print( rssi ); // PRINT RECEIVED SIGNAL STRENGTH
Serial.println( " dBm" );
}