Here is the clock.py file:>>> import clock
connecting to last APTrue
Connection successful
('192.168.0.33', '255.255.255.0', '192.168.0.1', '192.168.0.1') OpenWrt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "clock.py", line 4, in <module>
File "ntptime.py", line 23, in time
OSError: [Errno 110] ETIMEDOUT
Code: Select all
import quickoled, ntptime, utime, random, network, wificonnect
while True:
quickoled.oled.fill(0)
time = ntptime.time()
tm = utime.localtime(time)
randomY = random.randint(0,108)
randomX = random.randint(0,64)
print((tm[0], tm[1], tm[2], tm[6] + 7, tm[3], tm[4], tm[5], 0))
dateStr = str(tm[2]) + " - " + str(tm[1]) + " - " + str(tm[0])
timeStr = str(tm[6] + 7) + ":" + str(tm[3]) + ":" + str(tm[4]) + ":" + str(tm[5])
quickoled.oled.text(dateStr, randomX, randomY, 1)
quickoled.oled.text(dateStr, randomX, randomY + 10, 1)
quickoled.oled.show()
Code: Select all
try:
import usocket as socket
except:
import socket
try:
import ustruct as struct
except:
import struct
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600
host = "pool.ntp.org"
def time():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.settimeout(5)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
finally:
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
# There's currently no timezone support in MicroPython, so
# utime.localtime() will return UTC time (as if it was .gmtime())
def settime():
t = time()
import machine
import utime
tm = utime.localtime(t)
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))