mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
34 lines
905 B
Python
34 lines
905 B
Python
# NTP Example
|
|
#
|
|
# This example shows how to get the current time using NTP with the WiFi shield.
|
|
|
|
import network, socket, ustruct, utime
|
|
|
|
SSID='' # Network SSID
|
|
KEY='' # Network key
|
|
|
|
TIMESTAMP = 2208988800+946684800
|
|
|
|
# Init wlan module and connect to network
|
|
print("Trying to connect... (This may take a while)...")
|
|
wlan = network.WLAN(network.STA_IF)
|
|
wlan.deinit()
|
|
wlan.active(True)
|
|
wlan.connect(SSID, KEY, timeout=30000)
|
|
# We should have a valid IP now via DHCP
|
|
print("WiFi Connected ", wlan.ifconfig())
|
|
|
|
# Create new socket
|
|
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
# Get addr info via DNS
|
|
addr = socket.getaddrinfo("pool.ntp.org", 123)[0][4]
|
|
|
|
# Send query
|
|
client.sendto('\x1b' + 47 * '\0', addr)
|
|
data, address = client.recvfrom(1024)
|
|
|
|
# Print time
|
|
t = ustruct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP
|
|
print ("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (utime.localtime(t)[0:6]))
|