898 views|1 replies

47

Posts

2

Resources
The OP
 

[Digi-Key Follow me 4th issue] Submit homework [Copy link]

 

Code:

https://download.eeworld.com.cn/detail/leihaozhuce/631463

video:

https://training.eeworld.com.cn/video/39558

According to official requirements, the development was completed using two devices : W5500-EVB-Pico and Adafruit Sharp Memory Display . The software was programmed in Python. Circuit Python has a complete library, which is updated very quickly and is very easy to use, greatly reducing the difficulty of development.

The development experience on the Raspberry Pi platform is pretty good.

The compiler used is Thonny, which comes with the Raspberry Pi and is simple and easy to use.

First, build the development environment, download the latest firmware from the official website, and write it into w5500. The routine operations will not be repeated.

The first step is the light, the code is as follows:

import machine  
import time

# 初始化LED引脚为输出模式  
led = machine.Pin('LED', machine.Pin.OUT)  
  
while True:  
    # 点亮LED  
    led.on()  
    time.sleep(1)  # 等待1秒钟  
      
    # 熄灭LED  
    led.off()  
    time.sleep(1)  # 等待1秒钟

Run the program

The effect is shown in the figure below

To drive the LCD display, first install the necessary library files and download them from the official website. It was relatively slow and took a lot of effort to download them.

Copy the two library files adafruit_sharpmemorydisplay.mpy and adafruit_framebuf.mpy to the lib directory of w5500.

Connect the LCD display, the wiring is as follows, there are 3.3v and GND

GP15 Display module DI
GP14 Display module CLK
GP13

Display module CS

as the picture shows

code show as below

import time
import board
import busio
import digitalio

import adafruit_sharpmemorydisplay

def led_flush():
    led.value = not led.value
    time.sleep(0.5)

def clear_disp():
    # Clear the display.  Always call show after changing pixels to make the display
    # update visible!
    display.fill(1)
    display.show()

def disp_helloworld():
    print("hello EEWORLD")
    display.fill(1)
    display.text(" hello EEWORLD!", 30, 50, 0)
    display.show()

if __name__ == '__main__':
    led = digitalio.DigitalInOut(board.GP25)
    led.direction = digitalio.Direction.OUTPUT

    # Initialize SPI bus and control pins
    spi = busio.SPI(board.GP14, MOSI=board.GP15)
    scs = digitalio.DigitalInOut(board.GP13)  # inverted chip select

    # pass in the display size, width and height, as well
    # display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 96, 96)
    display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 144, 168)

    clear_disp()
    disp_helloworld()
    while True:
        led_flush()




Complete the initialization of the main control board W5500 (static IP configuration), and be able to use the LAN computer to ping, and W5500 can ping Internet sites; use packet capture software (Wireshark, Sniffer, etc.) to capture the local PC's ping message, display and analyze it.

Connect the w5500 to the switch via a network cable.

Install the Raspberry Pi version of Wireshark

After installation, use

$ sudo wireshark &

The command must obtain root privileges to see the port.

Because the Raspberry Pi uses a wireless link, select wlan0 here and you can start capturing packets.

The configuration code is as follows

import board
import busio
import digitalio
import time
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K

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

##reset
W5x00_RSTn = board.GP20

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, 1, 100)
SUBNET_MASK = (255, 255, 255, 0)
GATEWAY_ADDRESS = (192, 168, 1, 1)
DNS_SERVER = (8, 8, 8, 8)

led = digitalio.DigitalInOut(board.GP25)
led.direction = digitalio.Direction.OUTPUT

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

# For Adafruit Ethernet FeatherWing
cs = digitalio.DigitalInOut(SPI0_CSn)
# For Particle Ethernet FeatherWing
# cs = digitalio.DigitalInOut(board.D5)
spi_bus = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_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!")


The running results are as follows

The following shows that the development board address is 192.168.1.100. The following is a ping test.
The development board can be pinged. The packet capture result is as shown below.
Packet capture results

The main control board establishes a TCPIP or UDP server, and the LAN PC uses a TCPIP or UDP client to connect and send data. After the main control board receives the data, it sends it to the LCD screen for display (if not, it will be displayed through the serial port print); the interactive messages are captured by the packet capture software, displayed and analyzed. (Choose one of TCP and UDP, or operate both)
Using the software of Basic Task 1 as the basis, complete the TCP and UDP servers, server IP = 172.17.100.101, port number Port = 40001.

Use the adafruit_wiznet5k_socket library to create a TCP server. The function input parameter is the server port.

The received information will be displayed on the LCD display, which is divided into three areas:
Area 1: Ethernet parameter area, Area 2: TCP connection information display area, Area 3: Received information display area, the input parameters of the display function are the string to be displayed and the display position.
code show as below:
import time
import board
import busio
import digitalio
import array
import struct

import adafruit_sharpmemorydisplay
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket

IP_ADDRESS = (172, 17, 100, 101)
SUBNET_MASK = (255, 255, 0, 0)
GATEWAY_ADDRESS = (172, 17, 64, 1)
DNS_SERVER = (218, 203, 59, 116)


def init():
    led = digitalio.DigitalInOut(board.GP25)
    led.direction = digitalio.Direction.OUTPUT

    # Initialize SPI bus and control pins
    spi = busio.SPI(board.GP14, MOSI=board.GP15)
    scs = digitalio.DigitalInOut(board.GP13)  # inverted chip select

    # pass in the display size, width and height, as well
    # display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 96, 96)
    display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 144, 168)
    
    return led,display

def led_flush():
    led.value = not led.value
    time.sleep(0.5)

def clear_disp():
    # Clear the display.  Always call show after changing pixels to make the display
    # update visible!
    display.fill(1)
    display.show()

def disp_helloworld():
    print("hello world")
    display.fill(1)
    display.text(" hello world!", 30, 50, 0)
    display.show()

def init_w5500(ip, subnet, gateway, dns):
    ##SPI0
    SPI0_SCK = board.GP18
    SPI0_TX = board.GP19
    SPI0_RX = board.GP16
    SPI0_CSn = board.GP17    
    ##reset
    W5x00_RSTn = board.GP20
    
    print("Wiznet5k Ping Test (no DHCP)")
    
    ethernetRst = digitalio.DigitalInOut(W5x00_RSTn)
    ethernetRst.direction = digitalio.Direction.OUTPUT
    
    # For Adafruit Ethernet FeatherWing
    cs = digitalio.DigitalInOut(SPI0_CSn)
    # For Particle Ethernet FeatherWing
    # cs = digitalio.DigitalInOut(board.D5)
    spi_bus = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_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)

    # Set network configuration
    eth.ifconfig = (ip, subnet, gateway, dns)

    print("Chip Version:", eth.chip)
    print("My IP address is:", eth.pretty_ip(eth.ip_address))

    return eth

def disp_w5500():
    clear_disp()
    display.fill(1)
    cst = "chip version:" + eth.chip
    display.text(cst, 0, 0, 0)
    cst = "ip:" + eth.pretty_ip(eth.ip_address)
    display.text(cst, 0, 10, 0)
    cst = "mac:" + eth.pretty_mac(eth.mac_address)
    display.text(cst, 0, 20, 0)
    display.show()

def disp_str(s, addr):
    display.fill_rect(0,addr,144,10,1)
    display.text(s, 0, addr, 0)
    display.show()
    

def inCksum(packet):
    if len(packet) & 1:
        packet = packet + '\0'
    words = array.array('h', packet)
    sum = 0
    for word in words:
        sum += (word & 0xffff)
    sum = (sum>>16) + (sum & 0xffff)
    sum = sum + (sum >> 16)
    return (~sum) & 0xffff

def create_tcpserver(port):
    # Initialize a socket for our server
    socket.set_interface(eth)
    sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # Allocate socket for the server
    sc_ip = None  # IP address of server
    sc_port = port  # Port to listen on
    sc.bind((sc_ip, sc_port))  # Bind to IP and Port
    sc.listen()  # Begin listening for incoming clients
    
    return sc

def create_udpserver(port):
    # Initialize a socket for our server
    socket.set_interface(eth)
    sc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # Allocate socket for the server
#    sc_ip = None  # IP address of server
    sc_port = port  # Port to listen on
    sc.bind((eth.pretty_ip(eth.ip_address), sc_port))  # Bind to IP and Port
    
    return sc


if __name__ == '__main__':
    # 初始化led和显示模块
    led,display = init()
    clear_disp()
    #静态初始化w5500
    eth = init_w5500(IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
    disp_w5500()
    
    # 创建一个tcp服务器
#    server = create_tcpserver(40001)
    # 创建一个udp服务器
    server = create_udpserver(40001)
    
#    conn, addr = server.accept()  # Wait for a connection from a client.
    print("socket connected")
#    print(addr)
    disp_str("UDP Server TEST", 30)
    disp_str("socket connected", 40)
#    disp_str('cli:'+addr[0]+','+str(addr[1]), 50)
    disp_str('receive:', 60)
    linenum = 70
    while True:
        led_flush()
        
        with server:
            while True:
#                data = conn.recv(20)
                data,addr = server.recvfrom(20)
                disp_str('cli:'+addr[0]+','+str(addr[1]), 50)
                if data:
                    print(data)
                    disp_str(data.decode('utf-8'),linenum)
                    linenum += 10
                    if linenum > 150:
                        linenum = 70
#                    conn.send(data)  # Echo message back to client




The running results are as follows:

The server is set up.

However, the TCP and UDP initialization software for Raspberry Pi has not been found yet, and specific tests have not been carried out.

Build a sntp client to get time from the ntp timing server.

code show as below

import ntptime
import time
for i in range(10):
    try:
        ntptime.settime()
        t = time.localtime(time.time() + 8*3600)
        print("ntp time(BeiJing): %s-%s-%s %s:%s:%s" % (t[0],t[1],t[2],t[3],t[4],t[5]))
        break
    except:
        print("Can not get time!")

    time.sleep_ms(1000)

The running results are as follows

Experience

Python's development library is well maintained and has a wide range of devices, which greatly speeds up the development process. It is a good choice for development. However, there is still a lot to learn about network programming, and the difficulty is still quite high.

Finally, I would like to thank Dejie and EEWORLD for the suggestion.

This post is from DigiKey Technology Zone

Latest reply

Python's development library is well maintained and has a wide range of devices, which greatly speeds up the development process. It is a good choice for development. However, there is still a lot to learn about network programming, and the difficulty is still quite high. It seems that the getting started tasks are very simple. Thanks to the forum for organizing such a great event.   Details Published on 2024-3-9 16:19
 
 

6820

Posts

11

Resources
2
 

Python's development library is well maintained and has a wide range of devices, which greatly speeds up the development process. It is a good choice for development. However, there is still a lot to learn about network programming, and the difficulty is still quite high.

It seems that the getting started tasks are very simple. Thanks to the forum for organizing such a great event.

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list