1+ import time
2+ import gc
3+ import board
4+ import displayio
5+ import busio
6+ from adafruit_esp32spi import adafruit_esp32spi_socket as socket
7+ from adafruit_esp32spi import adafruit_esp32spi , adafruit_esp32spi_wifimanager
8+ import adafruit_requests as requests
9+ import digitalio
10+ import analogio
11+ from adafruit_pyportal import PyPortal
12+ from adafruit_bitmap_font import bitmap_font
13+ from adafruit_display_text import label
14+ import adafruit_touchscreen
15+ from adafruit_ntp import NTP
16+
17+
18+ try :
19+ from secrets import secrets
20+ except ImportError :
21+ print ("""WiFi settings are kept in secrets.py, please add them there!
22+ the secrets dictionary must contain 'ssid' and 'password' at a minimum""" )
23+ raise
24+
25+ esp32_cs = digitalio .DigitalInOut (board .ESP_CS )
26+ esp32_ready = digitalio .DigitalInOut (board .ESP_BUSY )
27+ esp32_reset = digitalio .DigitalInOut (board .ESP_RESET )
28+
29+ spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
30+ esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset , debug = False )
31+ requests .set_socket (socket , esp )
32+
33+ if esp .status == adafruit_esp32spi .WL_IDLE_STATUS :
34+ print ("ESP32 found and in idle mode" )
35+ print ("Firmware vers." , esp .firmware_version )
36+ print ("MAC addr:" , [hex (i ) for i in esp .MAC_address ])
37+
38+ print ("Connecting to AP..." )
39+ while not esp .is_connected :
40+ try :
41+ esp .connect_AP (secrets ['ssid' ], secrets ['password' ])
42+ except RuntimeError as e :
43+ print ("could not connect to AP, retrying: " , e )
44+ continue
45+
46+ pyportal = PyPortal (esp = esp ,
47+ external_spi = spi )
48+
49+ # Set the font and preload letters
50+ font = bitmap_font .load_font ("/fonts/Helvetica-Bold-36.bdf" )
51+ font .load_glyphs (b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()' )
52+
53+ # Set up display group
54+ splash = displayio .Group (max_size = 10 ) # The Main Display Group
55+ view = displayio .Group (max_size = 15 ) # Group for Text objects
56+ splash .append (view )
57+
58+ # Set up text labels
59+ label_time = label .Label (font , color = 0x538ab1 , max_glyphs = 200 )
60+ label_time .x = board .DISPLAY .width // 7
61+ label_time .y = 150
62+ view .append (label_time )
63+
64+ label_day = label .Label (font , color = 0xFFFFFF , max_glyphs = 200 )
65+ label_day .x = board .DISPLAY .width // 7
66+ label_day .y = 40
67+ view .append (label_day )
68+
69+ # Show group splash
70+ board .DISPLAY .show (splash )
71+
72+ # Initialize the NTP object
73+ ntp = NTP (esp )
74+
75+ # Fetch and set the microcontroller's current UTC time
76+ # keep retrying until a valid time is returned
77+ while not ntp .valid_time :
78+ ntp .set_time ()
79+ print ("Failed to obtain time, retrying in 15 seconds..." )
80+ time .sleep (15 )
81+
82+ # Get the current time in seconds since Jan 1, 1970
83+ current_time = time .time ()
84+ print ("Seconds since Jan 1, 1970: {} seconds" .format (current_time ))
85+
86+ # Convert the current time in seconds since Jan 1, 1970 to a struct_time
87+ now = time .localtime (current_time )
88+ print (now )
89+
90+ # Pretty-parse the struct_time
91+ print (
92+ "It is currently {}/{}/{} at {}:{}:{} UTC" .format (
93+ now .tm_mon , now .tm_mday , now .tm_year , now .tm_hour , now .tm_min , now .tm_sec
94+ )
95+ )
96+
97+
98+ def wday_to_weekday_name (tm_wday ):
99+ """Returns the name of the weekday based on tm_wday.
100+ :param int tm_wday: Days since Sunday.
101+
102+ """
103+ switcher = {
104+ 0 : "Monday" ,
105+ 1 : "Tuesday" ,
106+ 2 : "Wednesday" ,
107+ 3 : "Thursday" ,
108+ 4 : "Friday" ,
109+ 5 : "Saturday" ,
110+ 6 : "Sunday" ,}
111+ return switcher .get (tm_wday , "Not found" )
112+
113+ # Convert date to weekday name
114+ weekday = wday_to_weekday_name (now .tm_wday )
115+ label_day .text = "{}" .format (weekday )
116+
117+
118+ # Time names
119+ time_name = ["midnight-ish" , "late night" , "late" , "super late" ,
120+ "super early" ,"really early" ,"dawn" ,"morning" ,
121+ "morning" ,"mid-morning" ,"mid-morning" ,"late morning" ,
122+ "noon-ish" ,"afternoon" ,"afternoon" ,"mid-afternoon" ,
123+ "late afternoon" ,"early evening" ,"early evening" ,"dusk-ish" ,
124+ "evening" ,"evening" ,"late evening" ,"late evening" ]
125+
126+ label_time .text = "({})" .format (time_name [0 ])
127+
128+
129+ while True :
130+ pass
0 commit comments