Close

Using TCP Server, TCP Client, and UDP with W5500 Ethernet on SP2302

securepiSecurePi wrote 06/03/2025 at 08:29 • 3 min read • Like

Overview

W5500 Ethernet chip (via WIZ850io) enables SPI-based Ethernet communication on the SP2302, creating a second Ethernet interface (eth1). This interface can fully support TCP server, TCP client, and UDP socket communication, allowing you to develop embedded applications like device controllers, IoT gateways, or data relays.

Setup Prerequisites

  1. WIZ850io properly wired via SPI to SP2302
  2. Dual-port firmware flashed to SP2302 with support for eth1
  3. Network check:
    bash
    
    ip addr show eth1
  4. Assign a static IP or use DHCP: Example netplan:
    yaml
    
    network:  version: 2  ethernets:    eth1:      dhcp4: yes

Apply:

bash


sudo netplan apply

Testing TCP & UDP on W5500 (eth1)

All examples below use eth1 (W5500) for communication. Replace IPs with your actual eth1 IP when needed.

1. TCP Server on W5500

Python TCP Server Code

python

# tcp_server_w5500.py
import socket

HOST = '192.168.2.50'  # eth1 IP
PORT = 5000

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:    s.bind((HOST, PORT))    s.listen()    print(f"[+] Listening on {HOST}:{PORT}")    conn, addr = s.accept()    with conn:        print(f"[✓] Connected by {addr}")        while True:            data = conn.recv(1024)            if not data:                break            print(f"[<] Received: {data.decode()}")            conn.sendall(b"ACK: " + data)

Run:

bash

python3 tcp_server_w5500.py

2. TCP Client on W5500

Python TCP Client Code

python

# tcp_client_w5500.py
import socket

SERVER_IP = '192.168.2.100'  # Remote server IP
PORT = 5000
LOCAL_IP = '192.168.2.50'    # eth1 IP

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:    s.bind((LOCAL_IP, 0))  # Bind to eth1    s.connect((SERVER_IP, PORT))    s.sendall(b"Hello from W5500 client!")    data = s.recv(1024)    print(f"[<] Received: {data.decode()}")

3. UDP Communication on W5500

Python UDP Echo Server

python

# udp_server_w5500.py
import socket

HOST = '192.168.2.50'  # eth1 IP
PORT = 6000

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:    s.bind((HOST, PORT))    print(f"[+] UDP Server listening on {HOST}:{PORT}")    while True:        data, addr = s.recvfrom(1024)        print(f"[<] Received from {addr}: {data.decode()}")        s.sendto(b"ACK: " + data, addr)

Python UDP Client

python

# udp_client_w5500.py
import socket

SERVER_IP = '192.168.2.100'
PORT = 6000
LOCAL_IP = '192.168.2.50'

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:    s.bind((LOCAL_IP, 0))  # Bind to W5500    s.sendto(b"Hello UDP", (SERVER_IP, PORT))    data, addr = s.recvfrom(1024)    print(f"[<] Reply: {data.decode()}")

Testing Tools

Results:

TCP Server

TCP Client

UDP

Debugging Tips

Applications for W5500 Socket Usage

Use CaseDescription
Local Device ControllerAccept commands over TCP
Sensor GatewaySend UDP packets to edge/cloud
Secure ProxyUse TCP to bridge isolated and trusted networks
Logging Stream ServerUDP-based fire-and-forget telemetry

Summary

RoleScriptBinds to Interface
TCP Servertcp_server_w5500.pyeth1
TCP Clienttcp_client_w5500.pyeth1
UDP Serverudp_server_w5500.pyeth1
UDP Clientudp_client_w5500.pyeth1
Like

Discussions