当前位置: 首页 > news >正文

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()

http://www.dtcms.com/a/499118.html

相关文章:

  • 1panel docker开启swap内存
  • 动态规划的“降维”艺术:二维矩阵中的建筑奇迹——最大矩形
  • switch语句在汇编层面的几种优化方式 ,为什么能进行优化
  • Apache Spark算法开发指导-特征转换VectorIndexer
  • 企业网站的高跳出率应该如何解决广州物流网站开发
  • Docker 与 K8s 网络模型全解析
  • 【算法与数据结构】拓扑排序实战(栈+邻接表+环判断,附可运行代码)
  • AWS Elastic Load Balancing(ELB)—— 多站点负载均衡的正确打开方式
  • 如何用域名建网站主流建站公司
  • 企业网站模板源代码jz做网站
  • 深入 Spring 内核:解密 15 种设计模式的实战应用与底层实现
  • 【S32K3XX系列MCAL配置-第一节开发环境搭建】
  • 中矿资源的财报估值分析
  • 网站关键词优化方案分为几个步骤嘉兴微信网站建设
  • stm32驱动LTC2494详解
  • AI写作赋能SEO:用提示词打造从标题到发布的全流程优化策略
  • PVT(Pyramid Vision Transformer):金字塔结构,适合检测/分割
  • SSRF 漏洞深度剖析:从原理到实战
  • Python第十八节 命名空间作用域详细介绍及注意事项
  • 网站怎么做跳转链接域名备案要多少钱
  • 哪个网站查公司信息比较准网站设计像素
  • mq和rocketmq
  • AI搜索自由:Perplexica+cpolar构建你的私人知识引擎
  • C++基础:(十五)queue的深度解析和模拟实现
  • VSR 项目解析
  • 软件工程新纪元:AI协同编程架构师的修养与使命
  • 一、RPA基础认知与环境搭建
  • 网站域名过期了怎么办怎样做网站导航界面
  • armbian 滚动更新锁定
  • Rust 设计模式 Marker Trait + Blanket Implementation