W5500 esp32 micropython 驱动测试 网线直连电脑静态IP设置
W5500 esp32 mpy 参考视频
参考网址
W5500 Lite 丝印简写:
接线按照:sck=Pin(26),mosi=Pin(25),miso=Pin(13) cs=Pin(27,Pin.OUT),电源3.3V
1、网线直连电脑,初始化失败,默认是有DHCP服务器
#创建W55xx驱动对象
nic =WIZNET5K(spi,cs,rst)
在ESP32的mpy下运行上述代码报错:
File "wiznet5k.py", line 170, in __init__ AssertionError: Failed to configure DHCP Server!
静态IP有构造方法 nic=WIZNET5K(spi,cs,rst,is_dhcp=False)
def __init__(self, spi_bus, cs, reset=None, is_dhcp=True, mac=DEFAULT_MAC, hostname=None, dhcp_timeout=30, debug=False)
2、手动设置网络参数
nic.ifconfig = ('192.168.1.100', '255.255.255.0', '192.168.1.1', '8.8.8.8')
报错 TypeError: can't convert str to int
@property
def ifconfig(self):
"""Returns the network configuration as a tuple."""
print('IFCONFIG')
return (self.ip_address, self.read(REG_SUBR, 0x00, 4), self.read(REG_GAR, 0x00, 4), self._dns)
@ifconfig.setter
def ifconfig(self, params):
"""Sets network configuration to provided tuple in format:
(ip_address, subnet_mask, gateway_address, dns_server).
"""
ip_address, subnet_mask, gateway_address, dns_server = params
self.write(REG_SIPR, 0x04, ip_address)
self.write(REG_SUBR, 0x04, subnet_mask)
self.write(REG_GAR, 0x04, gateway_address)
self._dns = dns_server
解决办法:
def ip_to_bytes(ip_str):
"""将IP地址字符串转换为字节格式"""
return bytes([int(part) for part in ip_str.split('.')])
nic.ifconfig = (
ip_to_bytes('172.16.30.119'),
ip_to_bytes('255.255.255.0'),
ip_to_bytes('172.16.30.254'),
ip_to_bytes('8.8.8.8')
)
from wiznet5k import WIZNET5K
from machine import Pin,SPI
import wiznet5k_socket as socket
import sma_esp32_w5500_requests as requests
import time
#自定义接线引脚
spi=SPI(2,baudrate=8000000,sck=Pin(26),mosi=Pin(25),miso=Pin(13))
#CS对应的GPIO
cs=Pin(27,Pin.OUT)
#虚指GPIO实际接高电平即可
rst=Pin(39)
#创建W5500驱动对象 is_dhcp=False
nic=WIZNET5K(spi,cs,rst,is_dhcp=False)
#打印相关信息
print("\n\n以太网芯片版本:", nic.chip)
print("网卡MAC地址:",[hex(i) for i in nic.mac_address])
print("IP地址:",nic.pretty_ip(nic.ip_address))# def ip_to_bytes(ip_str):
# parts = ip_str.split('.')
# # 将每个部分转换为整数,然后转换为字节
# return bytes(int(part) for part in parts)def ip_to_bytes(ip_str):"""将IP地址字符串转换为字节格式"""return bytes([int(part) for part in ip_str.split('.')])# 手动设置网络参数
# nic.ifconfig = ('192.168.1.100', '255.255.255.0', '192.168.1.1', '8.8.8.8')
# TypeError: can't convert str to int
nic.ifconfig = (ip_to_bytes('172.16.30.119'),ip_to_bytes('255.255.255.0'),ip_to_bytes('172.16.30.254'),ip_to_bytes('8.8.8.8')
)
print("网络配置设置成功")'''设置网络接口
关键点:在创建 socket 之前,必须调用 socket.set_interface(nic) 来设置 W5500 为默认网络接口。
这是解决 'NoneType' object has no attribute 'get_socket' 错误的关键步骤'''
socket.set_interface(nic)
print("网络接口设置成功")#创建udp套接字
udp_socket =socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print("UDP socket创建成功!")# 测试socket功能
udp_socket.settimeout(1.0)
print("Socket配置完成")#准备接收方的地址
dest_addr = ('172.16.30.133', 8080)
print("IP地址:",nic.pretty_ip(nic.ip_address))#发送数据到指定的电脑上的指定程序中
for i in range(1000000):send_data="hello world--%d"%iprint(send_data)udp_socket.sendto(send_data.encode('utf-8'),dest_addr)time.sleep(0.1)#关闭套接字
udp_socket.close()