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

4. UDP Client Experiment

UDP Client Experiment


UDP Client Experiment1. UDP Client Overview2. Core Concepts2.1 UDP Communication Model2.2 Command Table2.3 Core API3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Explained5.1 Creating a UDP Socket and Handshake5.2 Non-Blocking Reception5.3 Command Dispatch5.4 Main Entry6. Operating Procedures6.1 Environment Preparation6.2 Flashing and Running6.3 Serial Output Example6.4 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 ordering, but it has low overhead and low latency, making it very suitable for scenarios with high real-time requirements where occasional packet loss is acceptable.

In IoT applications, UDP is widely used for high-frequency sensor data reporting (such as temperature and humidity sampled 100 times per second where losing a few frames doesn't matter), LAN device discovery (broadcast/multicast probing for online devices on the same subnet), DNS resolution (small query volume, timeout-retry is enough), and real-time audio/video streaming (latency takes priority over image quality). Mastering UDP's sendto()/recvfrom() programming model is the foundation for later advanced experiments such as custom protocol packaging, real-time data reporting, and LAN device discovery.

In this experiment the ESP32-S3 acts as a UDP client, communicating bidirectionally with the PC network debugging assistant — active handshake, non-blocking command reception, periodic heartbeat, and support for commands such as ping / status / led on / led off / who.

Typical application scenarios:

Application TypeExample
Real-time data streamsHigh-frequency sensor sampling reporting (losing a few frames doesn't matter)
LAN discoverymDNS / SSDP device discovery protocols
Voice/videoVoLTE, real-time audio/video (low latency > reliability)
Broadcast/multicastSending one message to multiple devices

UDP requires no connection; you can send directly with sendto(). The peer address is obtained in each recvfrom().


2. Core Concepts

2.1 UDP Communication Model

┌──────────┐    sendto()     ┌──────────┐
│ ESP32-S3 │ ──────────────→ │  PC      │
│ (Client) │ ←────────────── │          │
└──────────┘    recvfrom()   └──────────┘
  • UDP is connectionless; no connect() is needed; you send directly to the target address
  • Each packet is independent; the sender and receiver do not need to maintain connection state
  • sendto(data, (ip, port)) sends, recvfrom(bufsize) receives and obtains the source address

2.2 Command Table

PC CommandESP32 ReplyDescription
pingpongConnectivity test
statusRuntime + free memoryDevice status
led onLED ON (OK)Turn on GPIO48 LED
led offLED OFF (OK)Turn off GPIO48 LED
whoESP32-S3 UIDDevice unique ID
Other textecho: <original>Echo

2.3 Core API

​x
import socket
​
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # UDP socket
sock.bind(("0.0.0.0", 8080))                # bind local port
sock.sendto(b"hello", ("192.168.11.183", 8080)) # send
data, addr = sock.recvfrom(256)              # recv + get sender

3. Hardware Dependencies

The ESP32-S3 has built-in WiFi. The PC network debugging assistant selects UDP mode, listens on port 8080, and the target port is 8080 (the ESP32 bind port).


4. Project Architecture and Flow

xxxxxxxxxx
Script execution (single-file .py)
  |
  |- wifi_connect(): connect to WiFi (STA mode)
  |- udp_socket(): create UDP socket -> bind(8080)
  |- udp_send(): handshake -> HELLO
  |
  `- while True:
       |- WiFi disconnect watchdog -> auto reconnect
       |- udp_recv(): non-blocking receive (0.3s timeout)
       |- handle_command(): command dispatch
       `- send heartbeat every 30s

5. Source Code Explained

Full source code: Source Code -> MicroPython -> 4.Network Protocols -> UDP_Client -> UDP_Client.py

5.1 Creating a UDP Socket and Handshake

SOCK_DGRAM = UDP; bind specifies the local port and then sends a HELLO handshake packet to the PC.

xxxxxxxxxx
ESP32_PORT = 8080                # local bind port
PC_IP      = "192.168.11.183"     # PC IP address
PC_PORT    = 8080                # PC listen port
​
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", ESP32_PORT))
sock.settimeout(0.3)             # non-blocking polling
​
udp_send(sock, PC_IP, PC_PORT, f"HELLO from ESP32-S3 [{esp_ip}]")

5.2 Non-Blocking Reception

settimeout(0.3) implements non-blocking behavior — if no data arrives within 0.3s, an OSError is raised; catching it returns None and continues the main loop.

xxxxxxxxxx
def udp_recv(sock, bufsize=256):
    try:
        data, addr = sock.recvfrom(bufsize)
        return data, addr
    except OSError:
        return None, None          # timeout, no data

5.3 Command Dispatch

Supports five commands ping/status/led on/led off/who; unmatched commands echo the original content.

xxxxxxxxxx
def handle_command(sock, addr, cmd):
    if cmd == "ping":
        udp_send(sock, *addr, "pong")
    elif cmd == "status":
        import gc
        reply = f"ESP32 up {time.time():.0f}s | free heap: {gc.mem_free()} B"
        udp_send(sock, *addr, reply)
    elif cmd == "led on":
        from machine import Pin; Pin(48, Pin.OUT, value=1)
        udp_send(sock, *addr, "LED ON (OK)")
    elif cmd == "who":
        import machine, ubinascii
        uid = ubinascii.hexlify(machine.unique_id()).decode()
        udp_send(sock, *addr, f"ESP32-S3 UID: {uid}")
    else:
        udp_send(sock, *addr, f"echo: {cmd}")

Command set:

CommandReplyDescription
pingpongConnectivity test
statusESP32 up Xs / free heap: X BRunning status
led onLED ON (OK)Turn on GPIO48
led offLED OFF (OK)Turn off GPIO48
whoESP32-S3 / UID: xxxDevice unique ID
Other textecho: <original>Echo if unmatched

5.4 Main Entry

wifi_connect connects to WiFi -> udp_socket creates and binds -> HELLO handshake -> main loop: receive commands + reconnect on disconnect + 30s heartbeat.

xxxxxxxxxx
def main():
    wlan = wifi_connect(WIFI_SSID, WIFI_PASSWORD)  # connect to WiFi
    esp_ip = wlan.ifconfig()[0]
​
    sock = udp_socket(ESP32_PORT)                   # create socket
    udp_send(sock, PC_IP, PC_PORT, f"HELLO from ESP32-S3 [{esp_ip}]")
​
    while True:
        if not wlan.isconnected():                  # WiFi watchdog
            wlan = wifi_connect(WIFI_SSID, WIFI_PASSWORD)
        data, addr = udp_recv(sock)                 # non-blocking recv
        if data:
            handle_command(sock, addr, fmt(data))   # dispatch
        if time.time() - last_hb >= 30:             # heartbeat every 30s
            udp_send(sock, PC_IP, PC_PORT, f"heartbeat [{esp_ip}]")

6. Operating Procedures

6.1 Environment Preparation

Thonny IDE steps: See MicroPython -> 1. Before Development -> 3.Thonny IDE.

  1. PC network debugging assistant 网络调试助手图标 UDP mode, listen on port 8080

    网络调试串口设置

  2. Modify WIFI_SSID / WIFI_PASS / PC_IP

    修改ID、PASSWORD、IP

     

6.2 Flashing and Running

Run UDP_Client.py; the ESP32 sends HELLO to the PC. The PC sends commands and the ESP32 replies.

6.3 Serial Output Example

xxxxxxxxxx
==================================================
  ESP32-S3 UDP Client  <-->  PC Network Assistant
==================================================
[WiFi] Already connected. IP: 192.168.11.229
[SOCK] UDP bound to 0.0.0.0:8080
[TX] -> 192.168.11.27:8080  |  HELLO from ESP32-S3 [192.168.11.229]
--------------------------------------------------
  PC   listen : 192.168.11.27:8080
  ESP32 bind  : 192.168.11.229:8080
--------------------------------------------------
[RX] <- 192.168.11.27:8080  |  led off
[TX] -> 192.168.11.27:8080  |  LED OFF (OK)  # GPIO44=1
[RX] <- 192.168.11.27:8080  |  led on
[TX] -> 192.168.11.27:8080  |  LED ON  (OK)  # GPIO44=0
[TX] -> 192.168.11.27:8080  |  heartbeat [192.168.11.229]
[RX] <- 192.168.11.27:8080  |  who
[TX] -> 192.168.11.27:8080  |  ESP32-S3  |  UID: a4cb8fc6b864
[RX] <- 192.168.11.27:8080  |  ping
[TX] -> 192.168.11.27:8080  |  pong
[RX] <- 192.168.11.27:8080  |  status
[TX] -> 192.168.11.27:8080  |  ESP32 up 838500500s | free heap: 120352 B

Network debugging assistant print:

助手打印

6.4 Verification Methods

OperationExpected Result
PC sends pingReplies pong
PC sends led onGPIO48 LED turns on, replies LED ON (OK)
PC sends whoReplies the ESP32-S3 unique ID

7. Common Problems

SymptomCauseSolution
PC cannot receive HELLOTarget IP/port mismatchConfirm PC_IP and PC_PORT match the PC
ESP32 cannot receive commandsWrong target port when the PC sendsThe target port should be 8080 (the ESP32 bind port)
Occasional packet lossUDP does not guarantee deliveryNormal; handle resending at the application layer
WIFI AP Mode
UDP Server 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/中文