Example explanation 14-static_ip static ip

# 使用静态IP的NTP例子。
#
# 这个例子展示了如何使用WiFi扩展板或者OpenMV RT的内置WIFI的NTP获取当前时间。
import network
import socket
import struct
import time

SSID = ""  # Network SSID
KEY = ""  # Network key

TIMESTAMP = 2208988800 + 946684800

# 初始化wlan模块并连接到网络
wlan = network.WLAN(network.STA_IF)
wlan.active(True)

# 在connect()之前必须调用ifconfig
wlan.ifconfig(("192.168.1.200", "255.255.255.0", "192.168.1.1", "192.168.1.1"))
wlan.connect(SSID, KEY)

while not wlan.isconnected():
    print('Trying to connect to "{:s}"...'.format(SSID))
    time.sleep_ms(1000)

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

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

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

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

results matching ""

    No results matching ""