Skip to content

Instantly share code, notes, and snippets.

Last active Dec 12, 2021
What would you like to do?
circuitpython clock for Lilygo TTGO T8 ESP32-S2
import struct
import time
import board
import displayio
import rtc
import socketpool
import terminalio
import wifi
from adafruit_display_text import label
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in, please add them there!")
TZ_OFFSET = 3600
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))
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)
TEXT_COLOR = 0xd257ff
def init_ui(display):
splash = displayio.Group(max_size=10)
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)
# 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)
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(
x=display.width // 2 - text_width // 2,
y=display.height // 2,
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)
print("Connecting to ", secrets["ssid"])["ssid"], password=secrets["password"])
print("Connected with IP ",
text_label.text = "Connected"
pool = socketpool.SocketPool(
now = get_ntp_time(pool)
rtc.RTC().datetime = now
print("Synced with NTP")
text_label.text = "Synced"
while True:
Copy link

bernschneider commented Jun 2, 2021

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 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

Copy link

netroy commented Jun 2, 2021

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?

Copy link

netroy commented Jun 2, 2021

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.

Copy link

bernschneider commented Jun 2, 2021

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 from mu editor.

Copy link

bernschneider commented Jun 3, 2021

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

Copy link

netroy commented Jun 3, 2021

I'm glad it works now. That makes me happy.

Copy link

joelhaasnoot commented Jul 4, 2021

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)

Copy link

netroy commented Jul 19, 2021

Thanks @joelhaasnoot. I updated it 💟

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment