UDP Server

UiFlow2 Example

echo server

Open the cores3_udp_server_example.m5f2 project in UiFlow2.

This example creates a UDP server that echoes the received data.

UiFlow2 Code Block:

cores3_udp_server_example.png

Example output:

None

MicroPython Example

echo server

This example creates a UDP server that echoes the received data.

MicroPython Code Block:

  1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
  2#
  3# SPDX-License-Identifier: MIT
  4
  5import os, sys, io
  6import M5
  7from M5 import *
  8import network
  9import socket
 10
 11
 12title0 = None
 13label0 = None
 14label1 = None
 15label2 = None
 16label3 = None
 17label4 = None
 18label5 = None
 19label6 = None
 20label7 = None
 21label8 = None
 22label9 = None
 23wlan = None
 24udps = None
 25
 26
 27recv_data = None
 28
 29
 30def setup():
 31    global \
 32        title0, \
 33        label0, \
 34        label1, \
 35        label2, \
 36        label3, \
 37        label4, \
 38        label5, \
 39        label6, \
 40        label7, \
 41        label8, \
 42        label9, \
 43        wlan, \
 44        udps, \
 45        recv_data
 46
 47    M5.begin()
 48    Widgets.fillScreen(0x222222)
 49    title0 = Widgets.Title("UDP Server", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
 50    label0 = Widgets.Label("Local IP:", 4, 32, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 51    label1 = Widgets.Label("Local Port:", 4, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 52    label2 = Widgets.Label("Remote IP:", 4, 88, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 53    label3 = Widgets.Label("Remote Port:", 4, 116, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 54    label4 = Widgets.Label("Recv Data:", 4, 144, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 55    label5 = Widgets.Label("label5", 136, 32, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 56    label6 = Widgets.Label("label6", 136, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 57    label7 = Widgets.Label("label7", 136, 88, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 58    label8 = Widgets.Label("label8", 136, 116, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 59    label9 = Widgets.Label("label9", 136, 144, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 60
 61    wlan = network.WLAN(network.STA_IF)
 62    wlan.active(True)
 63    udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 64    udps.bind(("0.0.0.0", 8000))
 65    udps.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 66    label5.setText(str(wlan.ifconfig()[0]))
 67    label6.setText(str("8000"))
 68
 69
 70def loop():
 71    global \
 72        title0, \
 73        label0, \
 74        label1, \
 75        label2, \
 76        label3, \
 77        label4, \
 78        label5, \
 79        label6, \
 80        label7, \
 81        label8, \
 82        label9, \
 83        wlan, \
 84        udps, \
 85        recv_data
 86    M5.update()
 87    recv_data = udps.recvfrom(1024)
 88    label7.setText(str(recv_data[1][0]))
 89    label8.setText(str(recv_data[1][1]))
 90    label9.setText(str(recv_data[0]))
 91    udps.sendto(recv_data[0], recv_data[1])
 92
 93
 94if __name__ == "__main__":
 95    try:
 96        setup()
 97        while True:
 98            loop()
 99    except (Exception, KeyboardInterrupt) as e:
100        try:
101            from utility import print_error_msg
102
103            print_error_msg(e)
104        except ImportError:
105            print("please update to latest firmware")

Example output:

None

API

socket.socket()
socket.bind(address)

Create a socket object and bind it to an address.

Parameters:

address (tuple) – A tuple containing the server IP address and port number.

Returns:

A socket object.

Return type:

socket.socket

UiFlow2 Code Block:

init.png

MicroPython Code Block:

import socket

udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind(('0.0.0.0', 8000))
socket.close()

Close the socket connection.

UiFlow2 Code Block:

close.png

MicroPython Code Block:

udps.close()
socket.recv(bufsize)

Receive data from the socket.

Parameters:

bufsize (int) – The maximum amount of data to be received at once.

Returns:

The received data.

Return type:

bytes

UiFlow2 Code Block:

recv.png

MicroPython Code Block:

data = udps.recv(1024)
print(data)
socket.recvfrom(bufsize)

Receive data from the socket. The return value is a pair (bytes, address) where bytes is a bytes object representing the data received and address is the address of the socket sending the data.

Parameters:

bufsize (int) – The maximum amount of data to be received at once.

Returns:

The received data.

Return type:

tuple

UiFlow2 Code Block:

recvfrom.png

MicroPython Code Block:

data, address = udps.recvfrom(1024)
print(data)
socket.read()

Read data from the socket.

Returns:

The received data.

Return type:

bytes

UiFlow2 Code Block:

read.png

MicroPython Code Block:

data = udps.read()
print(data)
socket.send(data)

Send data to the socket. The socket must be connected to a remote socket.

Parameters:

data (bytes | str) – The data to be sent.

Returns:

The number of bytes sent.

Return type:

int

UiFlow2 Code Block:

send.png

MicroPython Code Block:

udps.send(b'Hello, World!')
udps.send('Hello, World!')
socket.sendto(data, address)

Send data to the socket. The socket should not be connected to a remote socket, since the destination socket is specified by address.

Parameters:
  • data (bytes | str) – The data to be sent.

  • address (tuple) – A tuple containing the destination IP address and port number.

Returns:

The number of bytes sent.

Return type:

int

UiFlow2 Code Block:

sendto.png

MicroPython Code Block:

udps.sendto("hello", ("192.168.8.8", 8000))
socket.write(data)

Write the buffer of bytes to the socket. This function will try to write all data to a socket (no “short writes”). This may be not possible with a non-blocking socket though, and returned value will be less than the length of buf.

Parameters:

data (bytes | str) – The data to be sent.

Returns:

The number of bytes sent.

Return type:

int

UiFlow2 Code Block:

write.png

MicroPython Code Block:

udps.write(b'Hello, World!')
udps.write('Hello, World!')
socket.setsockopt(level, optname, value)

Set the value of the given socket option. The needed symbolic constants are defined in the socket module (SO_* etc.). The value can be an integer or a bytes-like object representing a buffer.

Parameters:
  • level (int) – The level at which the option is defined.

  • optname (int) – The name of the option to set.

  • value – The value to set for the option.

UiFlow2 Code Block:

setsockopt.png

MicroPython Code Block:

udps.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 0)
udps.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)