The ESP32 AsyncUDP library provides a powerful and efficient way to handle UDP (User Datagram Protocol) communication asynchronously on ESP32 microcontrollers. This library is designed to work seamlessly with the ESP32 Arduino Core and offers high-performance, non-blocking UDP operations for both client and server applications.
Unlike traditional blocking UDP implementations, AsyncUDP operates asynchronously, allowing your ESP32 to handle multiple UDP connections and operations simultaneously without blocking the main execution thread. This is particularly beneficial for IoT applications, real-time data streaming, sensor networks, and communication protocols that require low latency and high throughput.
The library supports both IPv4 and IPv6 addressing, multicast and broadcast communication, and provides comprehensive packet handling capabilities. It’s built on top of the ESP32’s LwIP (Lightweight IP) stack and FreeRTOS, ensuring optimal performance and reliability.
Below is a comprehensive list of methods available in the AsyncUDP Library, categorized by their purpose.
Connection Management
- connect()
Establishes a connection to a remote UDP endpoint for sending data - connected()
Checks if the UDP socket is currently connected to a remote endpoint - listen()
Binds the UDP socket to a specific port to listen for incoming packets - listenMulticast()
Joins a multicast group and listens for multicast packets on specified address and port - close()
Closes the UDP connection and releases associated resources
Data Transmission
- write()Sends raw data to the connected UDP endpoint or as a response to received packet
- writeTo()
Sends raw data to a specific IP address and port without establishing a connection - send()
Sends an AsyncUDPMessage to the connected endpoint - sendTo()Sends an AsyncUDPMessage to a specific IP address and port
- print()
Prints formatted text data using Arduino’s Print interface - printf()
Prints formatted text data using printf-style formatting
Broadcast & Multicast
- broadcast()
Sends data to all devices on the local network using broadcast addressing - broadcastTo()
Sends data to all devices on a specific port using broadcast addressing - isBroadcast()
Checks if a received packet was sent using broadcast addressing - isMulticast()
Checks if a received packet was sent using multicast addressing
Event Handling
- onPacket()
Sets a callback function to handle incoming UDP packets asynchronously
Network Information
- localIP()
Returns the local IP address of the received packet or listening socket - localPort()
Returns the local port number of the received packet or listening socket - remoteIP()
Returns the IP address of the sender for received packets - remotePort()
Returns the port number of the sender for received packets - listenIP()
Returns the IP address that the UDP socket is currently listening on - remoteMac()
Retrieves the MAC address of the sender for received packets
Packet Data Access
- data()
Returns a pointer to the raw data buffer of a received packet - length()
Returns the length in bytes of a received packet or message buffer - available()
Returns the number of bytes available for reading from a packet - read()
Reads data from a received packet using Stream interface - peek()
Peeks at the next byte in a packet without consuming it
Utility & Status
- data()
Returns a pointer to the raw data buffer of a received packet - length()
Returns the length in bytes of a received packet or message buffer - available()
Returns the number of bytes available for reading from a packet - read()
Reads data from a received packet using Stream interface - peek()
Peeks at the next byte in a packet without consuming it