openmv/scripts/examples/10-Arduino-Boards/Nano-RP2040/03-WiFi/ntp.py
2022-09-01 00:02:30 +02:00

38 lines
920 B
Python

# NTP Example
#
# This example shows how to get the current time using NTP with the WiFi shield.
import network, usocket, ustruct, utime
# AP info
SSID='' # Network SSID
KEY='' # Network key
TIMESTAMP = 2208988800
# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = network.WLAN()
wlan.active(True)
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
# We should have a valid IP now via DHCP
print(wlan.ifconfig())
# Create new socket
client = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
client.bind(("", 8080))
#client.settimeout(3.0)
# Get addr info via DNS
addr = usocket.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]))