Created
May 2, 2022 20:53
-
-
Save Neradoc/f27e293f5cab2e30a04aba4e3d7ba844 to your computer and use it in GitHub Desktop.
Simple NTP clock for any generic board with built-in display and wifi
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
import board | |
import time | |
import uiclock_lib as uiclock | |
# timezone offset in seconds (fomr UTC) | |
TZ_OFFSET = 3600 * 2 | |
current_time = None | |
def update_time(text_label): | |
global current_time | |
now = time.localtime() | |
if current_time != now: | |
text_label.text = "{hour:02d}:{min:02d}:{seconds:02d}".format(hour = now.tm_hour, min = now.tm_min, seconds = now.tm_sec) | |
current_time = now | |
display = board.DISPLAY | |
text_label = uiclock.init_ui(display) | |
time.sleep(1) | |
uiclock.connect_wifi(verbose=True) | |
text_label.text = "Connected" | |
time.sleep(1) | |
now = uiclock.set_ntp_time(TZ_OFFSET) | |
print("Synced with NTP") | |
text_label.text = "Synced" | |
time.sleep(1.0) | |
while True: | |
update_time(text_label) | |
time.sleep(0.2) | |
#display.rotation = (display.rotation + 180) % 360 |
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
# fill in and uncomment the secrets dictionary | |
# secrets = { | |
# 'ssid' : "YOURSSID", | |
# 'password' : "YOURPASSWORD", | |
# } |
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
import board | |
import rtc | |
import struct | |
import time | |
FONTSCALE = 3 | |
BORDER_WIDTH = 10 | |
BORDER_COLOR = 0x004488 | |
BACKGROUND_COLOR = 0x000022 | |
TEXT_COLOR = 0xd257ff | |
TZ_OFFSET = 3600 * 2 | |
NTP_TIME_CORRECTION = 2_208_988_800 | |
NTP_SERVER = "pool.ntp.org" | |
NTP_PORT = 123 | |
socket_pool = None | |
ssl_context = None | |
def init_ui(display, | |
text="Starting", *, | |
border_color=BORDER_COLOR, | |
background=BACKGROUND_COLOR, | |
text_color=TEXT_COLOR, | |
border=BORDER_WIDTH): | |
import terminalio | |
import displayio | |
from adafruit_display_text import label | |
splash = displayio.Group() | |
display.show(splash) | |
color_bitmap = displayio.Bitmap(display.width, display.height, 1) | |
color_palette = displayio.Palette(1) | |
color_palette[0] = border_color | |
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) | |
splash.append(bg_sprite) | |
# Draw a smaller inner rectangle | |
inner_bitmap = displayio.Bitmap(display.width - border * 2, display.height - border * 2, 1) | |
inner_palette = displayio.Palette(1) | |
inner_palette[0] = background | |
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=border, y=border) | |
splash.append(inner_sprite) | |
text_label = label.Label(terminalio.FONT, color=text_color, text=text, anchor_point=(0.5, 0.5)) | |
text_width = text_label.bounding_box[2] * FONTSCALE | |
text_group = displayio.Group( | |
scale=FONTSCALE, | |
x=display.width // 2 - text_width // 2, | |
y=display.height // 2, | |
) | |
text_group.append(text_label) | |
splash.append(text_group) | |
return text_label | |
def connect_wifi(verbose=False): | |
global socket_pool | |
import wifi | |
import socketpool | |
try: | |
from secrets import secrets | |
except ImportError: | |
print("WiFi secrets are kept in secrets.py, please add them there!") | |
raise | |
if verbose: | |
print("Connecting to ", secrets["ssid"]) | |
wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"]) | |
socket_pool = socketpool.SocketPool(wifi.radio) | |
if verbose: | |
print("Connected with IP ", wifi.radio.ipv4_address) | |
def get_ntp_time(pool, offset): | |
packet = bytearray(48) | |
packet[0] = 0b00100011 | |
for i in range(1, len(packet)): | |
packet[i] = 0 | |
with pool.socket(pool.AF_INET, pool.SOCK_DGRAM) as sock: | |
sock.settimeout(None) | |
sock.sendto(packet, (NTP_SERVER, NTP_PORT)) | |
sock.recv_into(packet) | |
destination = time.monotonic_ns() | |
seconds = struct.unpack_from("!I", packet, offset=len(packet) - 8)[0] | |
monotonic_start = seconds - NTP_TIME_CORRECTION - (destination // 1_000_000_000) | |
return time.localtime(time.monotonic_ns() // 1_000_000_000 + monotonic_start + offset) | |
def set_ntp_time(offset=TZ_OFFSET): | |
now = get_ntp_time(socket_pool, offset) | |
rtc.RTC().datetime = now | |
def setup_requests(): | |
global socket_pool | |
global ssl_context | |
import ssl | |
import adafruit_requests | |
if not ssl_context: | |
ssl_context = ssl.create_default_context() | |
requests = adafruit_requests.Session(socket_pool, ssl_context) | |
return requests | |
def _test(): | |
display = board.DISPLAY | |
text_label = init_ui(display) | |
return (display,text_label) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment