EasyUDPClient
EasyUDPClient provides a simple way to create UDP clients in an event-driven manner.
UiFlow2 Example
simple client
Open the udp_client_core2_example.m5f2 project in UiFlow2.
This example creates a UDP client that connects to a server and sends data.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
simple client
This example creates a UDP client that connects to a server and sends data.
MicroPython Code Block:
1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from easysocket import EasyUDPClient 9import network 10import time 11 12 13title0 = None 14label2 = None 15label0 = None 16label1 = None 17wlan_sta = None 18udpc_0 = None 19 20 21import random 22 23received_data = None 24client_address_port = None 25 26 27def udpc_0_received_event(args): 28 global title0, label2, label0, label1, wlan_sta, udpc_0, received_data, client_address_port 29 client, client_address_port, received_data = args 30 label1.setText(str("Receive Msg:")) 31 label2.setText(str((str(received_data) + str(client_address_port)))) 32 33 34def setup(): 35 global title0, label2, label0, label1, wlan_sta, udpc_0, received_data, client_address_port 36 37 M5.begin() 38 Widgets.setRotation(1) 39 Widgets.fillScreen(0x222222) 40 title0 = Widgets.Title( 41 "UDPClient Core2 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18 42 ) 43 label2 = Widgets.Label("", 2, 156, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 44 label0 = Widgets.Label("Local IP:", 2, 77, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 45 label1 = Widgets.Label("Receive Msg:", 1, 122, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 46 47 wlan_sta = network.WLAN(network.STA_IF) 48 wlan_sta.active(True) 49 print("wait network connecting") 50 while not (wlan_sta.isconnected()): 51 print(".") 52 time.sleep(1) 53 print("connect success") 54 print(wlan_sta.ifconfig()[0]) 55 label0.setText(str((str("Local IP:") + str((wlan_sta.ifconfig()[0]))))) 56 udpc_0 = EasyUDPClient( 57 "Please enter the UDP server IP address.", 8000, EasyUDPClient.MODE_UNICAST 58 ) 59 udpc_0.on_data_received(udpc_0_received_event) 60 61 62def loop(): 63 global title0, label2, label0, label1, wlan_sta, udpc_0, received_data, client_address_port 64 M5.update() 65 udpc_0.check_event(timeout=1) 66 if BtnA.wasPressed(): 67 udpc_0.send(str((random.randint(1, 100)))) 68 69 70if __name__ == "__main__": 71 try: 72 setup() 73 while True: 74 loop() 75 except (Exception, KeyboardInterrupt) as e: 76 try: 77 from utility import print_error_msg 78 79 print_error_msg(e) 80 except ImportError: 81 print("please update to latest firmware")
Example output:
None
API
- class software.easysocket.udp_client.EasyUDPClient(remote_host, remote_port, mode=0)
Bases:
objectCreate an EasyUDPClient object.
- Parameters:
Note
connection is initiated in the background when the object is created.
Note
This class is non-blocking and event-driven. You need to call check_event() periodically to process events.
UiFlow2 Code Block:

MicroPython Code Block:
from easysocket import EasyUDPClient udp_client = EasyUDPClient("192.168.1.100", 8080, mode=EasyUDPClient.MODE_UNICAST)
- connect()
Connect to the remote server.
MicroPython Code Block:
udp_client.connect()
- on_data_received(callback)
Set the callback function for data received event.
- Parameters:
callback – The callback function.
UiFlow2 Code Block:

MicroPython Code Block:
def on_data_received_cb(client, data): print("Received:", data) udp_client.on_data_received(on_data_received_cb)
- check_event(timeout=-1)
Check for events.
- Parameters:
timeout (int) – The timeout in milliseconds. Default is -1 (no timeout).
UiFlow2 Code Block:

MicroPython Code Block:
udp_client.check_event()
- send(data)
Send data to the remote server.
- Parameters:
data (bytes) – The data to send.
- Returns:
The number of bytes sent.
UiFlow2 Code Block:

MicroPython Code Block:
udp_client.send(b"Hello")
- sendto(data, address=None)
Send data to the remote server.
- Parameters:
- Returns:
The number of bytes sent.
UiFlow2 Code Block:


MicroPython Code Block:
udp_client.sendto(b"Hello", ("192.168.1.100", 8080))
- close()
Close the socket.
UiFlow2 Code Block:

MicroPython Code Block:
udp_client.close()
- getsockname(*args, **kwargs)
Return the socket’s own address.
- Returns:
The socket’s own address. the format is (host, port).
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
# get local ip address client_socket.getsockname()[0]

