Socket icon indicating copy to clipboard operation
Socket copied to clipboard

Wiznet w550-evb-pico UDP server

Open andrewmk opened this issue 1 year ago • 2 comments

I managed to get a UDP server working on a Wiznet w5500-evb-pico board. Loaded up CircuitPython 9.0.4 from https://downloads.circuitpython.org/bin/wiznet_w5500_evb_pico/en_GB/adafruit-circuitpython-wiznet_w5500_evb_pico-en_GB-9.0.4.uf2 and the matching Adafruit adafruit_wiznet5k and adafruit_ticks libraries.

import board
import busio
import digitalio
import time

from adafruit_wiznet5k.adafruit_wiznet5k import *
import adafruit_wiznet5k.adafruit_wiznet5k_socketpool as socketpool

##SPI0
SPI0_SCK = board.GP18
SPI0_TX = board.GP19
SPI0_RX = board.GP16
SPI0_CSn = board.GP17

##reset
W5x00_RSTn = board.GP20

ethernetRst = digitalio.DigitalInOut(W5x00_RSTn)
ethernetRst.direction = digitalio.Direction.OUTPUT

cs = digitalio.DigitalInOut(SPI0_CSn)
spi_bus = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_RX)

# Reset W5x00 first
print("Initialising...")
ethernetRst.value = False
time.sleep(1)
ethernetRst.value = True

MY_MAC = "00:01:02:03:04:05"
PORT = 5000
TIMEOUT = None
MAXBUF = 1024

# Initialize ethernet interface with DHCP
eth = WIZNET5K(spi_bus, cs, is_dhcp=True, mac=MY_MAC, debug=False)
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))

pool = socketpool.SocketPool(eth)
sock = pool.socket(pool.AF_INET, pool.SOCK_DGRAM)
sock.bind((eth.pretty_ip(eth.ip_address), PORT))
buffer = bytearray(MAXBUF)

while True:
    eth.maintain_dhcp_lease()
    size, addr = sock.recvfrom_into(buffer)
    print(buffer[:size].decode())

andrewmk avatar May 06 '24 13:05 andrewmk

Thanks. I don't recall why I never got around to doing a WIZnet server, but I'll try to add an example for completeness. WIZnet socket examples should work like native wifi (once the custom setup is done).

anecdata avatar May 06 '24 15:05 anecdata

I had great difficulty finding a working example of receiving UDP packets. Maybe it's because I wanted to use CircuitPython 9.04, or maybe it's that the Adafruit wiznet library seems to have changed recently, or both. Anyway in the end I picked apart the adafruit_ntp library and some other examples and got it working.

andrewmk avatar May 06 '24 17:05 andrewmk