例程讲解14-static_ip 静态ip

# 使用静态IP的NTP例子。
#
# 这个例子展示了如何使用带有NTP的WiFi扩展板获取当前时间。
import network, usocket, ustruct, utime

SSID='' # Network SSID
KEY=''  # Network key

TIMESTAMP = 2208988800+946684800

# 初始化wlan模块并连接到网络
print("Trying to connect... (may take a while)...")

wlan = network.WINC()
# 在connect()之前必须调用ifconfig
wlan.ifconfig(('192.168.1.200', '255.255.255.0', '192.168.1.1', '192.168.1.1'))
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)

# 创建新套接字
client = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)

# 通过DNS获取addr信息
addr = usocket.getaddrinfo("pool.ntp.org", 123)[0][4]

# 发送查询
client.sendto('\x1b' + 47 * '\0', addr)
data, address = client.recvfrom(1024)

# 打印时间
t = ustruct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP
print ("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (utime.localtime(t)[0:6]))

results matching ""

    No results matching ""