树莓派Pico-Raspberry Pi Pico 有线连接上网

树莓派Pi Pico有线连接

使用器件

  • 树莓派Pi Pico
  • W5500模块
    在这里插入图片描述

导入CircuitPython库

在这里插入图片描述

接线:

Pi PicoW5500
GP10SCLK
GP11MOSI
GP12MISO
GP13SCS
GP15RST
GNDGND
3.3V3.3V

在这里插入图片描述
在这里插入图片描述

代码测试:

  • Ping-test

    import board
    import busio
    import digitalio
    import time
    from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
    
    SPI1_SCK = board.GP10
    SPI1_TX = board.GP11
    SPI1_RX = board.GP12
    SPI1_CSn = board.GP13
    W5500_RSTn = board.GP15
    
    print("Wiznet5k Ping Test (no DHCP)")
    
    # Setup your network configuration below
    # random MAC, later should change this value on your vendor ID
    MY_MAC = (0x00, 0x01, 0x02, 0x03, 0x04, 0x05)
    IP_ADDRESS = (192, 168, 3, 111)
    SUBNET_MASK = (255, 255, 255, 0)
    GATEWAY_ADDRESS = (192, 168, 3, 1)
    DNS_SERVER = (8, 8, 8, 8)
    
    led = digitalio.DigitalInOut(board.GP25)
    led.direction = digitalio.Direction.OUTPUT
    
    ethernetRst = digitalio.DigitalInOut(W5500_RSTn)
    ethernetRst.direction = digitalio.Direction.OUTPUT
    
    # For Adafruit Ethernet FeatherWing
    cs = digitalio.DigitalInOut(SPI1_CSn)
    # For Particle Ethernet FeatherWing
    # cs = digitalio.DigitalInOut(board.D5)
    spi_bus = busio.SPI(SPI1_SCK, MOSI=SPI1_TX, MISO=SPI1_RX)
    
    # Reset W5500 first
    ethernetRst.value = False
    time.sleep(1)
    ethernetRst.value = True
    
    # Initialize ethernet interface with DHCP
    # eth = WIZNET5K(spi_bus, cs)
    # Initialize ethernet interface without DHCP
    eth = WIZNET5K(spi_bus, cs, is_dhcp=False, mac=MY_MAC)
    
    # Set network configuration
    eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
    
    print("Chip Version:", eth.chip)
    print("MAC Address:", [hex(i) for i in eth.mac_address])
    print("My IP address is:", eth.pretty_ip(eth.ip_address))
    
    while True:
        led.value = not led.value
        time.sleep(1)
    
    print("Done!")
    
  • socket代码

    import board
    import busio
    import digitalio
    import time
    from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
    import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
    
    SPI1_SCK = board.GP10
    SPI1_TX = board.GP11
    SPI1_RX = board.GP12
    SPI1_CSn = board.GP13
    W5500_RSTn = board.GP15
    
    print("Wiznet5k SimpleServer Test (DHCP)")
    
    # Setup your network configuration below
    # random MAC, later should change this value on your vendor ID
    MY_MAC = (0x00, 0x01, 0x02, 0x03, 0x04, 0x05)
    IP_ADDRESS = (192, 168, 3, 111)
    SUBNET_MASK = (255, 255, 255, 0)
    GATEWAY_ADDRESS = (192, 168, 3, 1)
    DNS_SERVER = (8, 8, 8, 8)
    
    led = digitalio.DigitalInOut(board.GP25)
    led.direction = digitalio.Direction.OUTPUT
    
    ethernetRst = digitalio.DigitalInOut(W5500_RSTn)
    ethernetRst.direction = digitalio.Direction.OUTPUT
    
    # For Adafruit Ethernet FeatherWing
    cs = digitalio.DigitalInOut(SPI1_CSn)
    # For Particle Ethernet FeatherWing
    # cs = digitalio.DigitalInOut(board.D5)
    
    spi_bus = busio.SPI(SPI1_SCK, MOSI=SPI1_TX, MISO=SPI1_RX)
    
    # Reset W5500 first
    ethernetRst.value = False
    time.sleep(1)
    ethernetRst.value = True
    
    # # Initialize ethernet interface without DHCP
    # eth = WIZNET5K(spi_bus, cs, is_dhcp=False, mac=MY_MAC, debug=True)
    # # Set network configuration
    # eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
    
    # Initialize ethernet interface with DHCP
    eth = WIZNET5K(spi_bus, cs, is_dhcp=True, mac=MY_MAC, debug=True)
    
    print("Chip Version:", eth.chip)
    print("MAC Address:", [hex(i) for i in eth.mac_address])
    print("My IP address is:", eth.pretty_ip(eth.ip_address))
    
    # Initialize a socket for our server
    socket.set_interface(eth)
    server = socket.socket()  # Allocate socket for the server
    server_ip = None  # IP address of server
    server_port = 50007  # Port to listen on
    server.bind((server_ip, server_port))  # Bind to IP and Port
    server.listen()  # Begin listening for incoming clients
    
    conn, addr = server.accept()  # Wait for a connection from a client.
    print("socket connected")
    while True:
        # Maintain DHCP lease
        eth.maintain_dhcp_lease()
    
        led.value = not led.value
        time.sleep(1)
        with conn:
            # data = conn.recv()
            # print(data)
            # conn.send(data)  # Echo message back to client
            while True:
                data = conn.recv(10)
                if data:
                    print(data)
                    conn.send(data)  # Echo message back to client
                    break
    
    print("Done!")
    
  • 代码参考:https://github.com/bjnhur/pico-W5500

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值