3
3
import board
4
4
import busio
5
5
from adafruit_esp32spi import adafruit_esp32spi_socket as socket
6
- #import adafruit_esp32spi.adafruit_esp32spi_socket as socket
7
6
from adafruit_esp32spi import adafruit_esp32spi , adafruit_esp32spi_wifimanager
8
7
import adafruit_requests as requests
9
8
import digitalio
25
24
26
25
FEED_NAME = "pyportal-switch"
27
26
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
-
50
27
months = ["JAN" , "FEB" , "MAR" , "APR" , "MAY" , "JUN" ,
51
28
"JUL" , "AUG" , "SEP" , "OCT" , "NOV" , "DEC" ]
52
29
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:' )
86
30
87
31
def get_local_timestamp (location = None ):
88
32
# pylint: disable=line-too-long
@@ -183,10 +127,11 @@ def __init__(self, my_pyportal):
183
127
self .pyportal = my_pyportal
184
128
self .current_time = 0
185
129
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 )]
189
133
self .text_areas = create_text_areas (text_area_configs )
134
+ self .text_areas [2 ].text = "starting ..."
190
135
for ta in self .text_areas :
191
136
self .pyportal .splash .append (ta )
192
137
@@ -250,6 +195,66 @@ def message(client, topic, message):
250
195
251
196
############################################
252
197
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
+
253
258
# Set up a MiniMQTT Client
254
259
mqtt_client = MQTT (socket ,
255
260
broker = 'io.adafruit.com' ,
@@ -262,7 +267,7 @@ def message(client, topic, message):
262
267
mqtt_client .on_message = message
263
268
264
269
mqtt_client .connect ()
265
- clock = Clock ( pyportal )
270
+
266
271
switch = Switch (board .D4 , pyportal )
267
272
268
273
second_timer = time .monotonic ()
0 commit comments