使用Micropython通过以太网连接Wiznet W5500 Pico Pi

MicroPython相关代码、库、软件、工具
回复
头像
shaoziyang
帖子: 3969
注册时间: 2019年 10月 21日 13:48

使用Micropython通过以太网连接Wiznet W5500 Pico Pi

#1

帖子 shaoziyang »

DHCP

代码: 全选

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

led = Pin(25, Pin.OUT)

#W5x00 chip init
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)
    # The only difference from the example linked above, using 
    # 'dhcp' instead of manually specifying the network info
    nic.ifconfig('dhcp')
    while not nic.isconnected():
        time.sleep(1)
        print(nic.regs())
    print(nic.ifconfig())
        
def main():
    w5x00_init()

    while True:
        led.value(1)
        time.sleep(1)
        led.value(0)
        time.sleep(1)

if __name__ == "__main__":
    main()
  1. 首先调用mainw5x00_init()
  2. 在w5x00_init() 中,调用 SPI准备以太网设备。
  3. 使用 ifconfig('dhcp') 得到一个IP地址
  4. 在循环while not nic.isconnected()中等待网络连接
  5. 主循环中闪灯

 
两个 W5500 之间的双向通信

代码: 全选

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

is_server=False
led = Pin(25, Pin.OUT)

#W5x00 chip init
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)

    if is_server: 
        nic.ifconfig(('192.168.1.20','255.255.255.0','192.168.1.1','8.8.8.8'))
    else:
        nic.ifconfig(('192.168.1.21','255.255.255.0','192.168.1.1','8.8.8.8'))
    
    print('IP address :', nic.ifconfig())
    while not nic.isconnected():
        time.sleep(1)
        print(nic.regs())
    
def server_loop(): 
    s = socket()
    s.bind(('192.168.1.20', 5000)) #Source IP Address
    s.listen(5)
    
    print("TEST server")
    led.value(1)
    conn, addr = s.accept()
    print("Connect to:", conn, "address:", addr) 
    print("Loopback server Open!")
    while True:
        data = conn.recv(2048)
        # Server blinks three times fast on each data reception
        for x in range(3):
            led.value(1)
            utime.sleep_ms(50)
            led.value(0)
            utime.sleep_ms(50)
        print(data.decode('utf-8'))
        if data != 'NULL':
            conn.send(data)

def client_loop():
    print("Attempt Loopback client Connect!")

    s = socket()
    s.connect(('192.168.1.20', 5000)) #Destination IP Address
    
    s.send('1')

    print("Loopback client Connect!")
    while True:
        data = s.recv(2048)
        # Client blinks on off slow on data reception
        print(data.decode('utf-8'))
        if data != 'NULL' :
            led.value(1)
            utime.sleep_ms(500)
            led.value(0)
            utime.sleep_ms(500)
            data_int = int(data) + 1
            s.send(str(data_int))
        
def main():
    # Server blinks twice fast, three separate times
    if is_server:
        for x in range(3):
            for y in range(2):
                led.value(1)
                utime.sleep_ms(50)
                led.value(0)
                utime.sleep_ms(50)
            time.sleep(1)
    # Client blinks three times slow
    else:
        for y in range(3):
            led.value(1)
            utime.sleep_ms(500)
            led.value(0)
            utime.sleep_ms(500)

    w5x00_init()
    
    if is_server:
        server_loop()
    else:
        client_loop()

if __name__ == "__main__":
    main()
完整说明:

https://sjhennion.github.io/jekyll/upda ... intro.html

头像
shaoziyang
帖子: 3969
注册时间: 2019年 10月 21日 13:48

Re: 使用Micropython通过以太网连接Wiznet W5500 Pico Pi

#2

帖子 shaoziyang »

图片

skylin008
帖子: 22
注册时间: 2020年 7月 21日 20:26

Re: 使用Micropython通过以太网连接Wiznet W5500 Pico Pi

#3

帖子 skylin008 »

看测试报告,ping 的速度比以前快了豪多。

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章