WiFiUDP Class

The WiFiUDP class is used send and receive UDP messages over Wi-Fi.

WiFiUDP

Description

Creates a named instance of the WiFi UDP class that can send and receive UDP messages. On AVR based boards, outgoing UDP packets are limited to 72 bytes in size currently. For non-AVR boards the limit is 1446 bytes.

Syntax

WiFiUDP

Parameters

none

WiFiUDP.begin()

Description

Initializes the WiFi UDP library and network settings. Starts WiFiUDP socket, listening at local port PORT.

Syntax

WiFiUDP.begin(port);

Parameters

port: the local port to listen on (int)

Returns

1: if successful 0: if there are no sockets available to use

WiFiUDP.available()

Description

Get the number of bytes (characters) available for reading from the buffer. This is data that's already arrived.

This function can only be successfully called after WiFiUDP.parsePacket().

available() inherits from the Stream utility class.

Syntax

WiFiUDP.available()

Parameters

None

Returns

the number of bytes available in the current packet 0: if parsePacket hasn't been called yet

WiFiUDP.beginPacket()

Description

Starts a connection to write UDP data to the remote connection

Syntax

WiFiUDP.beginPacket(hostName, port);
WiFiUDP.beginPacket(hostIp, port);

Parameters

hostName: the address of the remote host. It accepts a character string or an IPAddress

hostIp: the IP address of the remote connection (4 bytes)

port: the port of the remote connection (int)

Returns

1: if successful 0: if there was a problem with the supplied IP address or port

WiFiUDP.endPacket()

Description

Called after writing UDP data to the remote connection. It finishes off the packet and send it.

Syntax

WiFiUDP.endPacket();

Parameters

None

Returns

1: if the packet was sent successfully 0: if there was an error

WiFiUDP.write()

Description

Writes UDP data to the remote connection. Must be wrapped between beginPacket() and endPacket(). beginPacket() initializes the packet of data, it is not sent until endPacket() is called.

Syntax

WiFiUDP.write(byte);
WiFiUDP.write(buffer, size);

Parameters

byte: the outgoing byte buffer: the outgoing message size: the size of the buffer

Returns

single byte into the packet bytes size from buffer into the packet

WiFiUDP.parsePacket()

Description

It starts processing the next available incoming packet, checks for the presence of a UDP packet, and reports the size. parsePacket() must be called before reading the buffer with UDP.read().

Syntax

UDP.parsePacket();

Parameters

None

Returns

the size of the packet in bytes 0: if no packets are available

WiFiUDP.peek()

Read a byte from the file without advancing to the next one. That is, successive calls to peek() will return the same value, as will the next call to read().

This function inherited from the Stream class. See the Stream class main page for more information.

Syntax

WiFiUDP.peek()

Parameters

none

Returns

b: the next byte or character -1: if none is available

WiFiUDP.read()

Description

Reads UDP data from the specified buffer. If no arguments are given, it will return the next character in the buffer.

This function can only be successfully called after WiFiUDP.parsePacket().

Syntax

WiFiUDP.read();
WiFiUDP.read(buffer, len);

Parameters

buffer: buffer to hold incoming packets (char*)

len: maximum size of the buffer (int)

Returns

b: the characters in the buffer (char) size: the size of the buffer -1: if no buffer is available

WiFiUDP.flush()

Discard any bytes that have been written to the client but not yet read.

flush() inherits from the Stream utility class.

Syntax

WiFiUDP.flush()

Parameters

none

Returns

none

WiFiUDP.stop()

Description

Disconnect from the server. Release any resource being used during the UDP session.

Syntax

WiFiUDP.stop()

Parameters

none

Returns

none

WiFiUDP.remoteIP()

Description

Gets the IP address of the remote connection.

This function must be called after WiFiUDP.parsePacket().

Syntax

WiFiUDP.remoteIP();

Parameters

None

Returns

4 bytes : the IP address of the host who sent the current incoming packet

WiFiUDP.remotePort()

Description

Gets the port of the remote UDP connection.

This function must be called after UDP.parsePacket().

Syntax

UDP.remotePort();

Parameters

None

Returns

The port of the host who sent the current incoming packet