Created
August 5, 2016 22:53
-
-
Save drrk/4a17c4394f93d0f9123560af056f6f30 to your computer and use it in GitHub Desktop.
NTP update micropython time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# borrowed from https://github.com/micropython/micropython/blob/master/esp8266/scripts/ntptime.py | |
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 = "pool.ntp.org" | |
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) | |
s.close() | |
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() | |
rtc.init() | |
rtc.datetime(tm) | |
nic = network.CC3100() | |
nic.connect("emfcamp-insecure") | |
print("Get NTP Time") | |
# set the RTC using time from ntp | |
settime() | |
print("Display RTC Time") | |
# print out RTC datetime | |
print(pyb.RTC().datetime()) | |
print("Set NIC Time") | |
nic.settime(utime.localtime()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment