|
def disp_sntp_time(sntp_server):
ntpserver_ip = eth.pretty_ip(eth.get_host_by_name(sntp_server))
print("NTP : %s" % ntpserver_ip) #DNS Domain
ntp = NTP(iface = eth, ntp_address =ntpserver_ip ,utc=8)
cal = ntp.get_time()
disp_str("date:", 40)
disp_str("time:", 60)
str_date = "%s %d/%d/%d" %(days[cal.tm_wday], cal.tm_mday,cal.tm_mon,cal.tm_year)
print(str_date)
disp_str(str_date, 50)
str_date = "%d:%02d:%02d" %(cal.tm_hour,cal.tm_min,cal.tm_sec)
print(str_date)
disp_str(str_date, 70)
date:
Monday 6/2/2024
time:
16:27:01
# 测试进阶任务
import time
import board
import busio
import digitalio
import array
import struct
import adafruit_requests as requests
import adafruit_sharpmemorydisplay
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
from adafruit_wiznet5k.adafruit_wiznet5k_ntp import NTP
import adafruit_wiznet5k.adafruit_wiznet5k_dns as dns
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
IP_ADDRESS = (192, 168, 1, 100)
SUBNET_MASK = (255, 255, 255, 0)
GATEWAY_ADDRESS = (192, 168, 1, 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 init_w5500_dhcp():
##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 (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=True)
# 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 disp_sntp_time(sntp_server):
ntpserver_ip = eth.pretty_ip(eth.get_host_by_name(sntp_server))
print("NTP : %s" % ntpserver_ip) #DNS Domain
ntp = NTP(iface = eth, ntp_address =ntpserver_ip ,utc=8)
cal = ntp.get_time()
disp_str("date:", 40)
disp_str("time:", 60)
str_date = "%s %d/%d/%d" %(days[cal.tm_wday], cal.tm_mday,cal.tm_mon,cal.tm_year)
print(str_date)
disp_str(str_date, 50)
str_date = "%d:%02d:%02d" %(cal.tm_hour,cal.tm_min,cal.tm_sec)
print(str_date)
disp_str(str_date, 70)
if __name__ == '__main__':
# 初始化led和显示模块
led,display = init()
clear_disp()
#静态初始化w5500
eth = init_w5500(IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
#初始化w5500 dhcp
# eth = init_w5500_dhcp()
disp_w5500()
time.sleep(1)
disp_str("SNTP TEST", 30)
# ntp.aliyun.com 阿里云授时服务器
disp_sntp_time("ntp.aliyun.com")
while True:
led_flush()