Wiznet makers

ronpang

Published January 02, 2024 ©

125 UCC

10 WCC

32 VAR

0 Contests

1 Followers

0 Following

Original Link

6. MicroPython development for W5100S/W5500+RP2040 <UDP example>

6. MicroPython development for W5100S/W5500+RP2040 <UDP example>

COMPONENTS Hardware components

WIZnet - W5100S-EVB-Pico

x 1


WIZnet - W5500-EVB-Pico

x 1


PROJECT DESCRIPTION

1 Introduction

In this era of smart hardware and the Internet of Things, MicroPython and Raspberry Pi PICO are leading the new trend of embedded development with their unique advantages. MicroPython, as a streamlined and optimized Python 3 language, provides efficient development and easy debugging for microcontrollers and embedded devices.

 When we combine it with the WIZnet W5100S/W5500 network module, the development potential of MicroPython and Raspberry Pi PICO is further amplified. Both modules have built-in TCP/IP protocol stacks, making it easier to implement network connections on embedded devices. Whether it is data transmission, remote control, or building IoT applications, they provide powerful support.

 In this chapter, we will take WIZnet W5100S as an example to use MicroPython development method for UDP communication example.

2. Related network information

2.1 Introduction

UDP is the abbreviation of User Datagram Protocol. The Chinese name is User Datagram Protocol. It is a connectionless transport layer protocol in the OSI reference model that provides simple and unreliable transaction-oriented information transmission services.

2.2 UDP communication process

Step 1: Create a UDP Socket Each device (we call it a host) first needs to create a UDP socket. A socket is an endpoint for network communication and can be thought of as a door for sending or receiving data.

Step 2: Bind to Port Each UDP socket is bound to a specific port number. This port number is like a specific house number on the host, allowing external packets to find the correct application.

Step 3: Send data When the host needs to send data, it will pass the data to the UDP protocol. The UDP protocol adds a UDP header in front of the data and then sends the data (now called a "datagram") over the network.

Step 4: Receive Data On the receiving end, the datagram is routed to the correct host and port and then passed on to the appropriate application.

It is worth noting that UDP is connectionless, which means that it does not require a pre-established connection to send data. In addition, UDP does not guarantee the arrival of datagrams, nor does it sequence datagrams.

2.3 Advantages

Simple: The UDP protocol has fewer control options, so the delay during data transmission is small and the data transmission efficiency is high.

Lightweight: The UDP protocol has less overhead and is suitable for applications that do not require high reliability.

Connectionless: UDP is connectionless, which means it does not require a pre-established connection to send data.

Fast speed: The transmission speed of the UDP protocol is faster than that of TCP. This is because when the UDP protocol sends data, as long as the application process passes the data to UDP, UDP will package the data into a UDP segment and immediately pass it to the network layer.

Real-time: UDP is suitable for real-time data transmission, such as voice and video communications, because even if they occasionally lose one or two data packets, it will not have much impact on the reception results.

Supports one-to-one, one-to-many, and many-to-many interactive communication: This is because UDP supports broadcast and multicast.

2.4 Application

Real-time data transmission: UDP protocol is suitable for real-time data transmission, such as voice and video communications. In IoT devices, this real-time nature is particularly important. For example, in smart home systems, users may need to monitor security cameras in their homes in real time, or in industrial IoT applications, the transmission of real-time data is crucial for monitoring the operating status of equipment. important.

Lightweight communication: Since the UDP protocol has low overhead, it is well suited for resource-constrained IoT devices such as sensors and microcontrollers.

Broadcast and multicast communication: UDP supports broadcast and multicast, which are very useful in IoT applications. For example, a central controller may need to send the same instructions to all connected devices, or one device may need to broadcast its status to all other devices.

Fast data exchange: Due to the fast transmission speed of the UDP protocol, it is suitable for IoT applications that require fast data exchange. For example, in smart grids or Internet of Vehicles, data may need to be exchanged between devices within milliseconds.

3. WIZnet Ethernet chip

WIZnet mainstream hardware protocol stack Ethernet chip parameter comparison

ModelEmbedded CoreHost I/FTX/RX BufferHW SocketNetwork Performance
W5100STCP/IPv4, MAC & PHY8bit BUS, SPI16KB4Max 25Mbps
W6100TCP/IPv4/IPv6, MAC & PHY8bit BUS, Fast SPI32KB8Max 25Mbps
W5500TCP/IPv4, MAC & PHYFast SPI32KB8Max 15Mbps

W5100S/W6100 supports 8-bit data bus interface, and the network transmission speed will be better than W5500.

W6100 supports IPv6 and is compatible with W5100S hardware. If users who already use W5100S need to support IPv6, they can be Pin to Pin compatible.

W5500 has more Sockets and send and receive buffers than W5100S

Compared with the software protocol stack, WIZnet's hardware protocol stack Ethernet chip has the following advantages:

Hardware TCP/IP protocol stack: WIZnet's hardware protocol stack chip provides a hardware-implemented TCP/IP protocol stack. This hardware-implemented protocol stack has better performance and stability than software-implemented protocol stacks.

No additional embedded system software stack and memory resources required: Since all Ethernet transmit and receive operations are handled by the independent Ethernet controller, no additional embedded system software stack and memory resources are required.

Resistant to network environment changes and DDoS attacks: Compared with software TCP/IP protocol stacks that are susceptible to network environment changes and DDoS attacks, hardware protocol stack chips can provide more stable Ethernet performance.

Suitable for low-specification embedded systems: Even in low-specification embedded systems, hardware protocol stack chips using WIZnet can show more efficient Internet application operating performance than high-specification systems using software TCP/IP protocol stacks.

4. UDP communication example explanation and usage

4.1 Program flow chart

4.2 Test preparation

Software:

Thonny

SocketTester

Hardware:

W5100S IO module + RP2040 Raspberry Pi Pico development board or WIZnet W5100S-EVB-Pico development board

Micro USB interface data cable

cable

4.3 Connection method

Connect to PC USB port via data cable

When using W5100S/W5500 IO module to connect to RP2040

RP2040 GPIO 16 <----> W5100S/W5500 MISO

RP2040 GPIO 17 <----> W5100S/W5500 CS

RP2040 GPIO 18 <----> W5100S/W5500 SCK

RP2040 GPIO 19 <----> W5100S/W5500 MOSI

RP2040 GPIO 20 <----> W5100S/W5500 RST

Directly connect to the PC network port through a network cable (or: both the PC and the device are connected to the switch or router LAN port through a network cable)

4.4 Related code

We open the udp.py file directly.

Step one: You can see that SPI is initialized in the w5x00_init() function. And register the spi-related pins and reset pins into the library. The subsequent step is to activate the network and use DHCP to configure the network address information. When DHCP fails, configure the static network address information. When the configuration is not successful, the information about the network address-related registers will be printed out, which can help us better troubleshoot the problem.

Step 2: Then open the UDP SOCKET and bind it to the port. Finally, monitor whether there is data in the loop, and return the data if there is data.

import usocket
from machine import Pin,SPI
import network
import time

local_port = 5000
nic = None

"""
W5x00 chip initialization.

param: None
returns: None

"""
def w5x00_init():
   global nic
   spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
   nic = network.WIZNET5K(spi,Pin(17),Pin(20)) #spi,cs,reset pin
   nic.active(True)
   
   try:
       #DHCP
       print("\r\nConfiguring DHCP")
       nic.ifconfig('dhcp')
   except:
       #None DHCP
       print("\r\nDHCP fails, use static configuration")
       nic.ifconfig(('192.168.1.20','255.255.255.0','192.168.1.1','8.8.8.8'))#Set static network address information
   
   #Print network address information
   print("IP         :",nic.ifconfig()[0])
   print("Subnet Mask:",nic.ifconfig()[1])
   print("Gateway   :",nic.ifconfig()[2])
   print("DNS       :",nic.ifconfig()[3],"\r\n")
   
   #If there is no network connection, the register address information is printed
   while not nic.isconnected():
       time.sleep(1)
       print(nic.regs())
       
"""
UDP loop testing.

param: None
returns: None

"""    
def udp_loop():
   global nic
   s = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
   s.bind((nic.ifconfig()[0], local_port)) #Source IP Address
   print(f"Opened UDP loopback\r\nip:{nic.ifconfig()[0]},port:{local_port}")
   time.sleep(1)
   while True:
       data,addr = s.recvfrom(2048)
       print(f'Received:{data} from:{addr}')
       s.sendto(b'%s' % data, addr)
   
def main():
   print("WIZnet chip UDP example")
   w5x00_init()
   udp_loop()

if __name__ == "__main__":
   main()

4.5 Burning Verification

To test the Ethernet examples, the development environment must be configured to use a Raspberry Pi Pico.

Required development environment

Thonny

If you must compile MicroPython, you must use a Linux or Unix environment.

Step 1: Copy the program to Thonny, then select the environment as Raspberry Pi Pico, and finally click Run.

Step 2: Open a UDP in the network debugging assistant and connect to the development board.

Step 3: Conduct communication loopback test.

Note: Because MicroPython's print function enables stdout buffering, sometimes the content will not be printed out immediately.

5. Precautions

If you use WIZnet's W5500 to implement the examples in this chapter, you only need to burn the firmware of the W5500 and run the example program.

Documents
  • Code for this article

  • YouTube Demo

Comments Write