2. MicroPython development for W5100S/W5500+RP2040<DHCP example>
2. MicroPython development for W5100S/W5500+RP2040<DHCP example>
Software Apps and online services
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 use WIZnet W5100S as an example to perform DHCP parsing using MicroPython development method.
2 Related network information
2 .1 Protocol Introduction
DHCP (Dynamic Host Configuration Protocol) is a dynamic host configuration protocol. It is a technology for dynamic management and allocation of IP addresses. In DHCP, there are three main roles: DHCP server, DHCP client and DHCP relay.
The DHCP server is responsible for selecting an IP address from the address pool and assigning it to the DHCP client. It can also provide other network parameters for the DHCP client, such as the default gateway address, DNS server or WINS server address, etc. A DHCP client can be understood as a device that requests an IP address, such as an IP phone, mobile phone, PC, etc. Many times, the DHCP server is not on the same network segment as the DHCP client, so a proxy is needed, which is the relay here.
2.2 How DHCP works
DHCP works based on UDP, and the steps on its communication link are as follows:
The DHCP client will broadcast a DHCP discovery message (DHCP DISCOVER) transaction ID and the MAC address of the DHCP client. The source IP address of the IP datagram encapsulating the message is 0.0.0.0. This is because the host has not yet been assigned an IP address. address, so this address is used instead. The destination IP address is the broadcast address 255.255.255.255. The reason why the broadcast is sent is because the host does not know how many DHCP servers there are in the network.
The DHCP client initiates a DHCP request packet on the communication link. The request packet mainly tells the DHCP server that it wants to use the network settings provided in the previous step. Then the DHCP server sends a confirmation packet to the DHCP client, indicating that the DHCP client is allowed to use the second network setting. Send network settings step by step. At this point, the network settings of DHCP are completed, and then TCP/IP communication can be carried out between hosts on the communication link.
When an IP address is not needed, the client can send a DHCP release packet (DHCPRELEASE) to release it. In addition, there is usually a lease time setting in the DHCP settings. Within this time limit, the DHCP client can send a DHCP request packet to notify that it wants to extend the period.
2.3 Advantages of DHCP
The advantages of DHCP mainly include:
Automatic configuration: DHCP can automatically assign network configurations such as IP addresses, subnet masks, and default gateways to clients, greatly reducing the workload and error rate of manual configuration.
Save IP address resources: DHCP can dynamically allocate IP addresses, making the utilization of IP addresses higher and avoiding the waste of IP address resources.
Improve network security: DHCP can restrict clients to obtain IP addresses only from specific servers, avoiding unauthorized access and attacks and improving network security.
Supports multiple operating systems: DHCP can be implemented and applied on multiple operating systems and has good compatibility and portability.
Convenient client configuration: The DHCP client only needs to set the IP address of the DHCP server as the default gateway, and it can automatically obtain the IP address and other network configurations without manual configuration.
Reduce maintenance costs: Using DHCP can reduce the workload of administrators and reduce maintenance costs.
Improve efficiency: Using DHCP can reduce possible errors due to manual configuration, greatly improving work efficiency.
Easy to manage: When the IP address segment used by the network changes, you only need to modify the IP address pool of the DHCP server instead of modifying the addresses of all computers in the network one by one.
2.4 Application scenarios
The application scenarios of DHCP are as follows:
Mobile office: When a user moves within the LAN, such as from an office to a conference room, or from one device to another, DHCP can automatically assign a new IP address to the user, eliminating the need for the user to manually change the configuration.
Server deployment: In server deployment, DHCP can automatically assign IP addresses to servers, reducing the complexity and error rate of manual configuration.
Network device configuration: DHCP can be used to configure network devices, such as routers, switches, etc., without manually entering IP addresses, simplifying the device configuration process.
Virtualized environment: In a virtualized environment, DHCP can dynamically allocate IP addresses to virtual machines, improving the flexibility and efficiency of the virtualized environment.
IoT devices: For a large number of IoT devices, DHCP can automatically assign IP addresses to each device, facilitating centralized management and monitoring of the devices.
3 WIZnet Ethernet chip
WIZnet mainstream hardware protocol stack Ethernet chip parameter comparison
Model | Embedded Core | Host I/F | TX/RX Buffer | HW Socket | Network Performance |
---|---|---|---|---|---|
W5100S | TCP/IPv4, MAC & PHY | 8bit BUS, SPI | 16KB | 4 | Max.25Mbps |
W6100 | TCP/IPv4/IPv6, MAC & PHY | 8bit BUS, Fast SPI | 32KB | 8 | Max.25Mbps |
W5500 | TCP/IPv4, MAC & PHY | Fast SPI | 32KB | 8 | Max 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 DHCP network setting example overview and usage
4.1 Flowchart
The running block diagram of the program is as follows:
4.2 Core preparation work
Software
Thonny
Hardware
W5100SIO 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 the USB port of the PC through the data cable (mainly used for burning programs, but can also be used as a virtual serial port)
When wiring using module connection RP2040
RP2040 GPIO16 <----> W5100S MISO
RP2040 GPIO17 <----> W5100S CS
RP2040 GPIO18 <----> W5100S SCK
RP2040 GPIO19 <----> W5100S MOSI
RP2040 GPIO20 <----> W5100S RST
Connect the PC and device to the router LAN port through network cables
4.4 Main code overview
We open the dhcp.py file directly.
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.
from usocket import socket
from machine import Pin,SPI
import time,network
def w5x00_init():
spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
nic = network.WIZNET5K(spi,Pin(17),Pin(20))
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())
def main():
w5x00_init()
if __name__ == "__main__":
main()
4.5 Results demonstration
To test the Ethernet examples, the development environment must be configured to use a Raspberry Pi Pico.
Required development environment
If you must compile MicroPython, you must use a Linux or Unix environment.
After copying the code to Thonny, select the running environment as Raspberry Pi Pico, and then click Run.
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.