3. MicroPython development for W5100S/W5500+RP2040<DNS example>
3. MicroPython development for W5100S/W5500+RP2040<DNS example>
Software Apps and online services
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 perform DNS resolution using MicroPython development method.
2. Related network information
2.1 Protocol Introduction
DNS, or Domain Name System, is a core service on the Internet. It is a distributed database used to map domain names and IP addresses to each other, allowing users to access the Internet more conveniently without having to remember IP strings that can be directly read by machines.
In the Internet, each computer has a unique identifier called an IP address. However, IP addresses are composed of numbers and are not easy for people to remember. Therefore, we usually use domain names to access websites. For example, we can visit Microsoft's official website by entering www.microsoft.com without needing to remember its IP address.
When we enter a domain name in the browser, the computer will send a request to the DNS server to ask what the IP address corresponding to the domain name is. The DNS server looks up its database, finds the corresponding IP address and returns it to the computer. The computer can then access the website through this IP address.
In general, the DNS protocol is a process of converting domain names into IP addresses, so that people can access the Internet through domain names that are easy to understand and remember, instead of complex IP addresses. This greatly improves the ease of use of the Internet and allows websites to change their server's IP address without affecting users.
2.2 DNS working process
The client makes a domain name resolution request and sends the request to the local domain name server.
When the local domain name server receives the request, it first queries the local cache. If the record entry exists, the local domain name server directly returns the query result.
If there is no such record in the local cache, the local domain name server will directly send the request to the root domain name server, and then the root domain name server will return to the local domain name server the address of the primary domain name server of the queried domain (subdomain of the root).
The local domain name server then sends a request to the domain name server returned in the previous step.
The primary domain name server queries its own cache. If there is no such record, it returns the address of the relevant subordinate domain name server. Repeat step four until the correct record is found.
The local domain name server saves the returned results to the cache for next time use, and returns the results to the client at the same time.
2.3 Advantages
Easy to remember: People often find it easier to remember words and phrases than numbers. Therefore, it is more convenient to use a domain name (such as www.microsoft.com) than an IP address (such as 207.46.197.32).
Flexibility: If a website needs to change the IP address of its server, it only needs to update the record on the DNS server without notifying all users of the new IP address. Users can still access the website through the same domain name.
Load balancing: DNS can resolve a domain name into multiple IP addresses, and the servers corresponding to these IP addresses can share the load of processing user requests. This improves website usability and performance.
Security: DNSSEC (Domain Name System Security Extensions) is a technology used to protect DNS query results from tampering. This prevents DNS spoofing attacks and improves DNS security.
2.4 Application
The DNS protocol allows us to access the Internet through domain names that are easy to understand and remember, instead of complex IP addresses. Therefore, DNS is widely used in the following scenarios.
Web browsing: When we enter a URL (such as www.microsoft.com) in the browser and press the Enter key, the computer will send a request to the DNS server to ask for the IP address corresponding to the URL. The DNS server looks up its database, finds the corresponding IP address and returns it to the computer. The computer can then access the website through this IP address.
Email: When we send an email, the mail server needs to know the IP address of the mail server of the recipient's mailbox. This is done by querying the DNS server.
Internet calls: When we use Internet calls (such as Skype or VoIP), our device needs to know the IP address of the other party's device. This is also done by querying the DNS server.
Online games: When we play online games, our game clients need to know the IP address of the game server. This is also done by querying the DNS server.
Cloud services: When we use cloud services (such as AWS or Azure), we need to know the IP address of the cloud server. This is also done by querying the DNS server.
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. DNS resolution example explanation and usage
4.1 Program flow chart
4.2 Test preparation
Software:
Thonny
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 dns.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 perform DNS resolution operation and print the resolved IP address.
#import library
import usocket
from machine import Pin,SPI
import network
import time
#LED define
led = Pin(25, Pin.OUT)
"""
W5x00 chip initialization.
param: None
returns: None
"""
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)) #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())
"""
DNS Domain Name Resolution.
param: domain name
returns: IP Address
"""
def dns_query(domain):
ip = usocket.getaddrinfo(domain, 80,0, usocket.SOCK_STREAM)
return ip[0][4][0]
def main():
print("WIZnet chip DNS example");
domain = "www.wiznet.io"
w5x00_init()
ip = dns_query(domain)
print("IP address of %s is %s"%(domain,ip))
while True:
led.value(1)
time.sleep(1)
led.value(0)
time.sleep(1)
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
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.