Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
NTP update micropython time
# borrowed from
import socket
import pyb
import network
import utime
import socket
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600
host = ""
def getntptime():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
import struct
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
def settime():
import time
from pyb import RTC
t = getntptime()
tm = time.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
rtc = RTC()
nic = network.CC3100()
print("Get NTP Time")
# set the RTC using time from ntp
print("Display RTC Time")
# print out RTC datetime
print("Set NIC Time")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.