yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

5. UDP Client Experiment

UDP Client Experiment


UDP Client Experiment1. UDP Client Overview2. Core Concepts2.1 UDP vs TCP2.2 UDP Send Three-Step Process2.3 Receiving Characteristics3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 Configuration and Global Variables5.2 setup() — Bind Local Port5.3 loop() — Send and Receive6. Operation Flow6.1 Build and Flash6.2 Operation Steps6.3 Verification Methods7. Common Problems


1. UDP Client Overview

UDP (User Datagram Protocol) is a connectionless, unreliable but efficient transport layer protocol. Unlike TCP, UDP does not establish connections, does not guarantee delivery, and does not guarantee order, but has low overhead and low latency, making it very suitable for scenarios with high real-time requirements that allow occasional packet loss.

In IoT applications, UDP is widely used for high-frequency sensor data reporting (e.g., temperature and humidity collected 100 times per second, where losing a few frames is acceptable), LAN device discovery (broadcast/multicast probing for online devices on the same subnet), DNS domain resolution (small query volume, retry on timeout), and real-time audio/video streaming (latency takes priority over picture quality integrity).

In the Arduino ESP32 development environment, the system provides the packaged WiFiUDP library, which is still based on the lwIP protocol stack's sendto/recvfrom interface underneath, greatly simplifying the UDP programming process. This experiment configures ESP32-S3 as a UDP client. The device reports data to a specified UDP server at a 1-second cycle while listening and parsing server response packets in real-time.

The experiment uses the unified WiFiUDP standard three-step transmission mechanism: use beginPacket() to specify the communication target address and port, use write() to fill the data payload to be transmitted, and finally call endPacket() to complete the UDP datagram transmission, fully implementing lightweight, high real-time IoT UDP data interaction.

Typical Application Scenarios:

Application TypeExample
Real-time StreamingAudio/video transmission (losing a few frames does not affect experience)
Domain QueryDNS requests
Clock SynchronizationNTP time synchronization
Sensor BroadcastingMultiple devices simultaneously receiving sensor data

2. Core Concepts

2.1 UDP vs TCP

FeatureUDPTCP
ConnectionConnectionless, send packets directlyRequires three-way handshake
ReliabilityNo delivery guarantee, no retransmissionACK confirmation + automatic retransmission
Message BoundaryPreserved (one send = one datagram)Streaming (data coalescing, needs packet splitting)
Applicable ScenariosVideo streaming, DNS, IoT sensor dataWeb pages, file transfer, MQTT
Transmission EfficiencyHigh (header only 8 bytes)Lower (header 20 bytes)

2.2 UDP Send Three-Step Process

beginPacket(ip, port) specify target → write(buf, len) fill payload → endPacket() encapsulate UDP header + IP header and send. Underneath, it calls lwIP's sendto().

2.3 Receiving Characteristics

UDP naturally preserves message boundaries — each parsePacket() + read() exactly completes reading one complete datagram, without the packet coalescing problem that TCP has.


3. Hardware Dependencies

Pure software experiment. You must first connect to the router via WIFI-STA, and the client and server need to be on the same local area network.


4. Project Architecture and Flow

​x
setup()
  ├─ connectWiFi()                   // Connect to router (same as WIFI-STA experiment)
  └─ udp.begin(LOCAL_PORT)           // Bind local port

loop()
  ├─ Every second: beginPacket → write → endPacket  // Send datagram
  ├─ parsePacket() >= 0 → read()            // Non-blocking read response
  └─ delay(10)

5. Source Code Details

Complete source code: Source code → Arduino → 3.Network Communication → UDP_Client → UDP_Client.ino

5.1 Configuration and Global Variables

xxxxxxxxxx
#include <WiFiUDP.h>
WiFiUDP udp;
const char* SERVER_IP  = "192.168.11.183";
uint16_t    SERVER_PORT = 8080;
uint16_t    LOCAL_PORT  = 8080;

5.2 setup() — Bind Local Port

xxxxxxxxxx
void setup() {
  Serial.begin(115200);
  connectWiFi();                    // Same as WIFI-STA experiment
  udp.begin(LOCAL_PORT);            // Underneath: socket() → bind(8080)
}

5.3 loop() — Send and Receive

Send a datagram every second while non-blockingly checking for responses.

xxxxxxxxxx
void loop() {
  static unsigned long lastSend = 0;
  if (millis() - lastSend > 1000) {
    lastSend = millis();
    udp.beginPacket(SERVER_IP, SERVER_PORT);     // Specify target
    udp.write("ESP32 UDP Client Data");           // Fill payload
    udp.endPacket();                              // Send
  }

  int packetSize = udp.parsePacket();  // Non-blocking check
  if (packetSize > 0) {
    char buf[128];
    int len = udp.read(buf, sizeof(buf) - 1);
    buf[len] = '\0';
    Serial.printf("UDP Receive: %s\n", buf);
  }
  delay(10);
}

parsePacket() returns the number of bytes in the datagram, 0 means no data. Unlike TCP, each parsePacket + read only reads one complete datagram.


6. Operation Flow

6.1 Build and Flash

Arduino flash process: See Arduino → 1. Before Development → 2. Build and Flash.

  1. Modify the server IP to the actual address

  2. Build and flash

6.2 Operation Steps

Step 1: Open the network debugging assistant网络调试助手图标, configure UDP server

Open the network debugging assistant on the PC and configure it with the following parameters:

网络调试串口设置

ParameterValue
Protocol TypeUDP
Local IP AddressPC's LAN IP (e.g., 192.168.11.183)
Local Port8080

Click the "Connect" button, the network debugging assistant starts listening on UDP port 8080 and waits for client data.

Network Debugging Assistant Output

Step 2: Modify the server IP in the code, flash the program

Open Arduino IDE, change SERVER_IP in the code to the PC's actual LAN IP address, then compile and flash to the ESP32-S3 development board.

Step 3: Open the serial monitor and observe the startup process

After flashing, open the serial monitor in Arduino IDE (baud rate 115200). You should see ESP32 connect to WiFi and start the UDP client:

xxxxxxxxxx
19:06:39.068 -> Connecting to WiFi: Yahboom2 (Attempt 1)
19:06:40.125 -> ...
19:06:41.094 -> WiFi Connected successfully!
19:06:41.140 -> Device IP: 192.168.11.181
19:06:41.140 -> Target Server: 192.168.11.59:8080
19:06:41.140 -> UDP Client Startup successful!
19:06:41.140 -> UDP Send: ESP32 UDP Client Data
19:06:42.098 -> UDP Send: ESP32 UDP Client Data
...

At this point, the network debugging assistant should continuously receive ESP32 UDP Client Data sent by ESP32, indicating that UDP one-way communication is normal.

Serial Output

Step 4: Reply data from the network debugging assistant to verify two-way communication

Enter Hello YAHBOOM in the send area of the network debugging assistant and click send. The serial monitor will print the received reply in real-time:

xxxxxxxxxx
19:08:08.197 -> UDP Send: ESP32 UDP Client Data
19:08:09.216 -> UDP Send: ESP32 UDP Client Data
19:08:09.388 -> UDP Receive: Hello YAHBOOM
19:08:10.080 -> UDP Receive: Hello YAHBOOM
19:08:10.126 -> UDP Receive: Hello YAHBOOM
19:08:10.218 -> UDP Send: ESP32 UDP Client Data
19:08:11.188 -> UDP Send: ESP32 UDP Client Data
19:08:11.375 -> UDP Receive: Hello YAHBOOM
19:08:12.207 -> UDP Send: ESP32 UDP Client Data
...

Network Assistant Send Interaction

For every Hello YAHBOOM sent, the serial monitor prints one UDP Receive: Hello YAHBOOM, proving that both ESP32 sending and receiving are working normally.

6.3 Verification Methods

OperationExpected Result
Server onlineReceive reply after sending
Server offlineSending normal (UDP does not check if the other party exists), but no reply

7. Common Problems

PhenomenonCauseSolution
Sending normal but cannot receive replyServer not running or wrong portConfirm server IP/port, confirm server is started
parsePacket always returns 0Local port inconsistent with server reply targetCheck if the port bound by begin() is correct
UDP Server Experiment
TCP Client Experiment

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文