33import board
44import busio
55from adafruit_esp32spi import adafruit_esp32spi_socket as socket
6- #import adafruit_esp32spi.adafruit_esp32spi_socket as socket
76from adafruit_esp32spi import adafruit_esp32spi , adafruit_esp32spi_wifimanager
87import adafruit_requests as requests
98import digitalio
2524
2625FEED_NAME = "pyportal-switch"
2726
28- try :
29- from secrets import secrets
30- except ImportError :
31- print ("""WiFi settings are kept in secrets.py, please add them there!
32- the secrets dictionary must contain 'ssid' and 'password' at a minimum""" )
33- raise
34-
35- esp32_cs = digitalio .DigitalInOut (board .ESP_CS )
36- esp32_ready = digitalio .DigitalInOut (board .ESP_BUSY )
37- esp32_reset = digitalio .DigitalInOut (board .ESP_RESET )
38-
39- WIDTH = board .DISPLAY .width
40- HEIGHT = board .DISPLAY .height
41-
42- ts = adafruit_touchscreen .Touchscreen (board .TOUCH_XL , board .TOUCH_XR ,
43- board .TOUCH_YD , board .TOUCH_YU ,
44- calibration = (
45- (5200 , 59000 ),
46- (5800 , 57000 )
47- ),
48- size = (WIDTH , HEIGHT ))
49-
5027months = ["JAN" , "FEB" , "MAR" , "APR" , "MAY" , "JUN" ,
5128 "JUL" , "AUG" , "SEP" , "OCT" , "NOV" , "DEC" ]
5229
53- spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
54- esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset , debug = False )
55- requests .set_socket (socket , esp )
56-
57- if esp .status == adafruit_esp32spi .WL_IDLE_STATUS :
58- print ("ESP32 found and in idle mode" )
59- print ("Firmware vers." , esp .firmware_version )
60- print ("MAC addr:" , [hex (i ) for i in esp .MAC_address ])
61-
62- pyportal = PyPortal (esp = esp , external_spi = spi )
63-
64- for ap in esp .scan_networks ():
65- print ("\t %s\t \t RSSI: %d" % (str (ap ['ssid' ], 'utf-8' ), ap ['rssi' ]))
66-
67- print ("Connecting to AP..." )
68- while not esp .is_connected :
69- try :
70- esp .connect_AP (secrets ['ssid' ], secrets ['password' ])
71- except RuntimeError as e :
72- print ("could not connect to AP, retrying: " ,e )
73- continue
74- print ("Connected to" , str (esp .ssid , 'utf-8' ), "\t RSSI:" , esp .rssi )
75- print ("My IP address is" , esp .pretty_ip (esp .ip_address ))
76-
77- wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (
78- esp , secrets , debug = True )
79-
80- ampm_font = bitmap_font .load_font ("/fonts/RobotoMono-18.bdf" )
81- ampm_font .load_glyphs (b'ampAMP' )
82- date_font = bitmap_font .load_font ("/fonts/RobotoMono-18.bdf" )
83- date_font .load_glyphs (b'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' )
84- time_font = bitmap_font .load_font ("/fonts/RobotoMono-72.bdf" )
85- time_font .load_glyphs (b'0123456789:' )
8630
8731def get_local_timestamp (location = None ):
8832 # pylint: disable=line-too-long
@@ -183,10 +127,11 @@ def __init__(self, my_pyportal):
183127 self .pyportal = my_pyportal
184128 self .current_time = 0
185129 self .light = analogio .AnalogIn (board .LIGHT )
186- text_area_configs = [dict (x = 0 , y = 110 , size = 72 , color = DISPLAY_COLOR , font = time_font ),
187- dict (x = 260 , y = 165 , size = 18 , color = DISPLAY_COLOR , font = ampm_font ),
188- dict (x = 10 , y = 40 , size = 18 , color = DISPLAY_COLOR , font = date_font )]
130+ text_area_configs = [dict (x = 0 , y = 110 , size = 10 , color = DISPLAY_COLOR , font = time_font ),
131+ dict (x = 260 , y = 165 , size = 3 , color = DISPLAY_COLOR , font = ampm_font ),
132+ dict (x = 10 , y = 40 , size = 20 , color = DISPLAY_COLOR , font = date_font )]
189133 self .text_areas = create_text_areas (text_area_configs )
134+ self .text_areas [2 ].text = "starting ..."
190135 for ta in self .text_areas :
191136 self .pyportal .splash .append (ta )
192137
@@ -250,6 +195,66 @@ def message(client, topic, message):
250195
251196############################################
252197
198+ try :
199+ from secrets import secrets
200+ except ImportError :
201+ print ("""WiFi settings are kept in secrets.py, please add them there!
202+ the secrets dictionary must contain 'ssid' and 'password' at a minimum""" )
203+ raise
204+
205+ esp32_cs = digitalio .DigitalInOut (board .ESP_CS )
206+ esp32_ready = digitalio .DigitalInOut (board .ESP_BUSY )
207+ esp32_reset = digitalio .DigitalInOut (board .ESP_RESET )
208+
209+ WIDTH = board .DISPLAY .width
210+ HEIGHT = board .DISPLAY .height
211+
212+ ts = adafruit_touchscreen .Touchscreen (board .TOUCH_XL , board .TOUCH_XR ,
213+ board .TOUCH_YD , board .TOUCH_YU ,
214+ calibration = (
215+ (5200 , 59000 ),
216+ (5800 , 57000 )
217+ ),
218+ size = (WIDTH , HEIGHT ))
219+
220+ spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
221+ esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset , debug = False )
222+ requests .set_socket (socket , esp )
223+
224+ if esp .status == adafruit_esp32spi .WL_IDLE_STATUS :
225+ print ("ESP32 found and in idle mode" )
226+ print ("Firmware vers." , esp .firmware_version )
227+ print ("MAC addr:" , [hex (i ) for i in esp .MAC_address ])
228+
229+ pyportal = PyPortal (esp = esp , external_spi = spi )
230+
231+ ampm_font = bitmap_font .load_font ("/fonts/RobotoMono-18.bdf" )
232+ ampm_font .load_glyphs (b'ampAMP' )
233+ date_font = bitmap_font .load_font ("/fonts/RobotoMono-18.bdf" )
234+ date_font .load_glyphs (b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' )
235+ time_font = bitmap_font .load_font ("/fonts/RobotoMono-72.bdf" )
236+ time_font .load_glyphs (b'0123456789:' )
237+
238+ clock = Clock (pyportal )
239+
240+ for ap in esp .scan_networks ():
241+ print ("\t %s\t \t RSSI: %d" % (str (ap ['ssid' ], 'utf-8' ), ap ['rssi' ]))
242+
243+ print ("Connecting to AP..." )
244+ while not esp .is_connected :
245+ try :
246+ esp .connect_AP (secrets ['ssid' ], secrets ['password' ])
247+ except RuntimeError as e :
248+ print ("could not connect to AP, retrying: " ,e )
249+ continue
250+ print ("Connected to" , str (esp .ssid , 'utf-8' ), "\t RSSI:" , esp .rssi )
251+ print ("My IP address is" , esp .pretty_ip (esp .ip_address ))
252+
253+
254+ wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (
255+ esp , secrets , debug = True )
256+
257+
253258# Set up a MiniMQTT Client
254259mqtt_client = MQTT (socket ,
255260 broker = 'io.adafruit.com' ,
@@ -262,7 +267,7 @@ def message(client, topic, message):
262267mqtt_client .on_message = message
263268
264269mqtt_client .connect ()
265- clock = Clock ( pyportal )
270+
266271switch = Switch (board .D4 , pyportal )
267272
268273second_timer = time .monotonic ()
0 commit comments