-
-
Save netroy/d1bff654fa58b884d63894ca2c890fc6 to your computer and use it in GitHub Desktop.
import struct | |
import time | |
import board | |
import displayio | |
import rtc | |
import socketpool | |
import terminalio | |
import wifi | |
from adafruit_display_text import label | |
try: | |
from secrets import secrets | |
except ImportError: | |
print("WiFi secrets are kept in secrets.py, please add them there!") | |
raise | |
TZ_OFFSET = 3600 | |
NTP_SERVER = "pool.ntp.org" | |
NTP_PORT = 123 | |
def get_ntp_time(pool): | |
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.sendto(packet, (NTP_SERVER, NTP_PORT)) | |
sock.recvfrom_into(packet) | |
destination = time.monotonic_ns() | |
seconds = struct.unpack_from("!I", packet, offset=len(packet) - 8)[0] | |
monotonic_start = seconds - 2_208_988_800 - (destination // 1_000_000_000) | |
return time.localtime(time.monotonic_ns() // 1_000_000_000 + monotonic_start + TZ_OFFSET) | |
BORDER = 10 | |
FONTSCALE = 3 | |
BACKGROUND_COLOR = 0x0e1115 | |
FOREGROUND_COLOR = 0x0f1929 | |
TEXT_COLOR = 0xd257ff | |
def init_ui(display): | |
splash = displayio.Group(max_size=10) | |
display.show(splash) | |
color_bitmap = displayio.Bitmap(display.width, display.height, 1) | |
color_palette = displayio.Palette(1) | |
color_palette[0] = BACKGROUND_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] = FOREGROUND_COLOR | |
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="Starting", anchor_point=(0.5, 0.5)) | |
text_width = text_label.bounding_box[2] * FONTSCALE | |
text_group = displayio.Group( | |
max_size=10, | |
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 update_time(text_label): | |
now = time.localtime() | |
text_label.text = "%02.d:%02.d:%02.d" % (now.tm_hour, now.tm_min, now.tm_sec) | |
display = board.DISPLAY | |
text_label = init_ui(display) | |
update_time(text_label) | |
print("Connecting to ", secrets["ssid"]) | |
wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"]) | |
print("Connected with IP ", wifi.radio.ipv4_address) | |
text_label.text = "Connected" | |
pool = socketpool.SocketPool(wifi.radio) | |
now = get_ntp_time(pool) | |
rtc.RTC().datetime = now | |
print("Synced with NTP") | |
text_label.text = "Synced" | |
time.sleep(1.0) | |
while True: | |
update_time(text_label) | |
time.sleep(1.0) |
Hey Peter,
It's likely that the code is crashing somewhere between line 114 and 120,
Can you please comment out lines 118-123, and try again?
Looks like board.LCD_MISO
was removed here.
Also, the LCD is enabled by default starting Circuitpython 6.2. So, I have updated this code to avoid re-initialization of the LCD.
Can you please comment out lines 118-123, and try again?
I did, but got same behavior (working from mu, not working after boot).
Also tried your new display = board.DISPLAY call from line 78 above, but this didn´t work neither even when saving code.py from mu editor.
Today I flashed the EN-US bin file version 6.3 and your above version is working now, also after power-off/ power-on!
(Before I used the DE-DE version 6.2)
Many thanks, Peter
I'm glad it works now. That makes me happy.
Cheers
This works great! One small improvement, if you change the formatting string, the seconds show up correctly:
text_label.text = "%02.d:%02.d:%02.d" % (now.tm_hour, now.tm_min, now.tm_sec)
Thanks @joelhaasnoot. I updated it 💟
Hi netroy,
Many thanks for your example code! Your code didn´t run on my board: There was an error message re unknown board.LCD_MISO.
I modified line 55 tft_miso = board.LCD_MISO into tft_miso = board.board.IO4. Then I was able to run your script on the LilyGo TTGO T8 ESP32-S2 by saving it from the mu editor to the board. Everything looked fine now.
But when I disconnected and powered-up the board again I shortly saw the clock, then shortly the initial getting started text screen (this text also was visible after boot before I created the cody.py file). After less than a second the screen went black and nothing happened.
I can repeat the last two steps without change or success.
Do you have any idea how to fix this?
Many thanks, Peter