注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
我爱下载的个人空间 https://home.eeworld.com.cn/space-uid-101026.html [收藏] [复制] [分享] [RSS]
日志

得捷电子Follow me第4期】进阶任务:从NTP服务器获取时间并显示

已有 524 次阅读2024-2-8 17:29 |个人分类:产品评测

【得捷电子Follow me4期】进阶任务NTP服务器获取时间并显示

硬件环境

硬件基本环境构建了一个可以连接网络的局域网,局域网内有一台PC和W5500-EVB-Pico设备。

 

图1 网络架构

软件实现

采用基础任务一的软件为基础,搭建一个sntp的客户端从ntp授时服务器获取时间。

创建sntp时间获取

利用adafruit_wiznet5k_ntp库,创建一个sntp客户端获取时间。函数输入参数为授时服务器的地址。
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)

 

接收信息显示

接收到授时服务器返回的时间信息后,会显示到LCD显示器上。分别显示日期和时间。
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()

 

运行输出

SNTP时间获取

 

本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章