1212from adafruit_display_text .label import Label
1313from adafruit_button import Button
1414import adafruit_touchscreen
15- from adafruit_minimqtt import MQTT
15+ import adafruit_minimqtt . adafruit_minimqtt as MQTT
1616
1717# ------------- WiFi ------------- #
1818
@@ -62,17 +62,22 @@ def set_backlight(val):
6262 board .DISPLAY .auto_brightness = False
6363 board .DISPLAY .brightness = val
6464
65+
6566# Touchscreen setup
66- ts = adafruit_touchscreen .Touchscreen (board .TOUCH_XL , board .TOUCH_XR ,
67- board .TOUCH_YD , board .TOUCH_YU ,
68- calibration = ((5200 , 59000 ), (5800 , 57000 )),
69- size = (320 , 240 ))
67+ ts = adafruit_touchscreen .Touchscreen (
68+ board .TOUCH_XL ,
69+ board .TOUCH_XR ,
70+ board .TOUCH_YD ,
71+ board .TOUCH_YU ,
72+ calibration = ((5200 , 59000 ), (5800 , 57000 )),
73+ size = (320 , 240 ),
74+ )
7075
7176# ---------- Set the font and preload letters ----------
7277# Be sure to put your font into a folder named "fonts".
7378font = bitmap_font .load_font ("/fonts/Helvetica-Bold-16.bdf" )
7479# This will preload the text images.
75- font .load_glyphs (b' abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()' )
80+ font .load_glyphs (b" abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()" )
7681
7782# ------------- User Inretface Eliments ------------- #
7883
@@ -84,8 +89,7 @@ def set_backlight(val):
8489color_bitmap = displayio .Bitmap (320 , 240 , 1 )
8590color_palette = displayio .Palette (1 )
8691color_palette [0 ] = 0x3D0068
87- bg_sprite = displayio .TileGrid (color_bitmap , x = 0 , y = 0 ,
88- pixel_shader = color_palette )
92+ bg_sprite = displayio .TileGrid (color_bitmap , x = 0 , y = 0 , pixel_shader = color_palette )
8993splash .append (bg_sprite )
9094
9195buttons = []
@@ -95,18 +99,32 @@ def set_backlight(val):
9599BUTTON_MARGIN = 10
96100
97101# Button Objects
98- button_1 = Button (x = BUTTON_MARGIN , y = BUTTON_MARGIN ,
99- width = BUTTON_WIDTH , height = BUTTON_HEIGHT ,
100- label = "Button 1" , label_font = font ,
101- style = Button .SHADOWROUNDRECT , label_color = 0x505050 ,
102- fill_color = 0x9e9e9e , outline_color = 0x464646 )
102+ button_1 = Button (
103+ x = BUTTON_MARGIN ,
104+ y = BUTTON_MARGIN ,
105+ width = BUTTON_WIDTH ,
106+ height = BUTTON_HEIGHT ,
107+ label = "Button 1" ,
108+ label_font = font ,
109+ style = Button .SHADOWROUNDRECT ,
110+ label_color = 0x505050 ,
111+ fill_color = 0x9E9E9E ,
112+ outline_color = 0x464646 ,
113+ )
103114buttons .append (button_1 )
104115
105- button_2 = Button (x = BUTTON_MARGIN , y = BUTTON_MARGIN * 2 + BUTTON_HEIGHT ,
106- width = BUTTON_WIDTH , height = BUTTON_HEIGHT ,
107- label = "Button 2" , label_font = font ,
108- style = Button .SHADOWROUNDRECT , label_color = 0x505050 ,
109- fill_color = 0x9e9e9e , outline_color = 0x464646 )
116+ button_2 = Button (
117+ x = BUTTON_MARGIN ,
118+ y = BUTTON_MARGIN * 2 + BUTTON_HEIGHT ,
119+ width = BUTTON_WIDTH ,
120+ height = BUTTON_HEIGHT ,
121+ label = "Button 2" ,
122+ label_font = font ,
123+ style = Button .SHADOWROUNDRECT ,
124+ label_color = 0x505050 ,
125+ fill_color = 0x9E9E9E ,
126+ outline_color = 0x464646 ,
127+ )
110128buttons .append (button_2 )
111129
112130for b in buttons :
@@ -140,14 +158,14 @@ def set_backlight(val):
140158
141159# ------------- MQTT Topic Setup ------------- #
142160
143- mqtt_topic = ' test/topic'
144- mqtt_temperature = ' pyportal/temperature'
145- mqtt_lux = ' pyportal/lux'
146- mqtt_PIR = ' pyportal/pir'
147- mqtt_button1 = ' pyportal/button1'
148- mqtt_button2 = ' pyportal/button2'
149- mqtt_feed1 = ' pyportal/feed1'
150- mqtt_feed2 = ' pyportal/feed2'
161+ mqtt_topic = " test/topic"
162+ mqtt_temperature = " pyportal/temperature"
163+ mqtt_lux = " pyportal/lux"
164+ mqtt_PIR = " pyportal/pir"
165+ mqtt_button1 = " pyportal/button1"
166+ mqtt_button2 = " pyportal/button2"
167+ mqtt_feed1 = " pyportal/feed1"
168+ mqtt_feed2 = " pyportal/feed2"
151169
152170# ------------- MQTT Functions ------------- #
153171
@@ -156,54 +174,64 @@ def set_backlight(val):
156174def connect (client , userdata , flags , rc ):
157175 # This function will be called when the client is connected
158176 # successfully to the broker.
159- print ('Connected to MQTT Broker!' )
160- print ('Flags: {0}\n RC: {1}' .format (flags , rc ))
177+ print ("Connected to MQTT Broker!" )
178+ print ("Flags: {0}\n RC: {1}" .format (flags , rc ))
179+
161180
162181def disconnected (client , userdata , rc ):
163182 # This method is called when the client is disconnected
164- print ('Disconnected from MQTT Broker!' )
183+ print ("Disconnected from MQTT Broker!" )
184+
165185
166186def subscribe (client , userdata , topic , granted_qos ):
167187 # This method is called when the client subscribes to a new feed.
168- print ('Subscribed to {0} with QOS level {1}' .format (topic , granted_qos ))
188+ print ("Subscribed to {0} with QOS level {1}" .format (topic , granted_qos ))
189+
169190
170191def publish (client , userdata , topic , pid ):
171192 # This method is called when the client publishes data to a feed.
172- print ('Published to {0} with PID {1}' .format (topic , pid ))
193+ print ("Published to {0} with PID {1}" .format (topic , pid ))
194+
173195
174196def message (client , topic , message ):
175197 """Method callled when a client's subscribed feed has a new
176198 value.
177199 :param str topic: The topic of the feed with a new value.
178200 :param str message: The new value
179201 """
180- print (' New message on topic {0}: {1}' .format (topic , message ))
202+ print (" New message on topic {0}: {1}" .format (topic , message ))
181203 if topic == "pyportal/feed1" :
182- feed1_label .text = ' Next Bus: {}' .format (message )
204+ feed1_label .text = " Next Bus: {}" .format (message )
183205 if topic == "pyportal/feed2" :
184- feed2_label .text = ' Weather: \n {}' .format (message )
206+ feed2_label .text = " Weather: \n {}" .format (message )
185207 if topic == "pyportal/button1" :
186208 if message == "1" :
187- buttons [0 ].label = "ON"
209+ buttons [0 ].label = "ON"
188210 buttons [0 ].selected = False
189211 print ("Button 1 ON" )
190212 else :
191- buttons [0 ].label = "OFF"
213+ buttons [0 ].label = "OFF"
192214 buttons [0 ].selected = True
193215 print ("Button 1 OFF" )
194216
217+
195218# ------------- Network Connection ------------- #
196219
197220# Connect to WiFi
221+ print ("Connecting to WiFi..." )
198222wifi .connect ()
223+ print ("Connected to WiFi!" )
224+
225+ # Initialize MQTT interface with the esp interface
226+ MQTT .set_socket (socket , esp )
199227
200228# Set up a MiniMQTT Client
201- client = MQTT (socket ,
202- broker = secrets [' broker' ],
203- port = 1883 ,
204- username = secrets [' user' ],
205- password = secrets [' pass' ],
206- network_manager = wifi )
229+ client = MQTT (
230+ broker = secrets [" broker" ],
231+ port = 1883 ,
232+ username = secrets [" user" ],
233+ password = secrets [" pass" ],
234+ )
207235
208236# Connect callback handlers to client
209237client .on_connect = connect
@@ -212,10 +240,13 @@ def message(client, topic, message):
212240client .on_publish = publish
213241client .on_message = message
214242
215- print (' Attempting to connect to %s' % client .broker )
243+ print (" Attempting to connect to %s" % client .broker )
216244client .connect ()
217245
218- print ('Subscribing to %s, %s, %s, and %s' % (mqtt_feed1 , mqtt_feed2 , mqtt_button1 , mqtt_button2 ))
246+ print (
247+ "Subscribing to %s, %s, %s, and %s"
248+ % (mqtt_feed1 , mqtt_feed2 , mqtt_button1 , mqtt_button2 )
249+ )
219250client .subscribe (mqtt_feed1 )
220251client .subscribe (mqtt_feed2 )
221252client .subscribe (mqtt_button1 )
@@ -228,18 +259,18 @@ def message(client, topic, message):
228259
229260 # Read sensor data and format
230261 light_value = lux = light_sensor .value
231- light_label .text = ' Light Sensor: {}' .format (light_value )
262+ light_label .text = " Light Sensor: {}" .format (light_value )
232263 temperature = round (adt .temperature )
233- temperature_label .text = ' Temp Sensor: {}' .format (temperature )
264+ temperature_label .text = " Temp Sensor: {}" .format (temperature )
234265 movement_value = movement_sensor .value
235- motion_label .text = ' PIR Sensor: {}' .format (movement_value )
266+ motion_label .text = " PIR Sensor: {}" .format (movement_value )
236267
237268 # Read display button press
238269 touch = ts .touch_point
239270 if touch :
240271 for i , b in enumerate (buttons ):
241272 if b .contains (touch ):
242- print (' Sending button%d pressed' % i )
273+ print (" Sending button%d pressed" % i )
243274 if i == 0 :
244275 # Toggle switch button type
245276 if button1_state == 0 :
@@ -252,30 +283,30 @@ def message(client, topic, message):
252283 b .label = "OFF"
253284 b .selected = True
254285 print ("Button 1 OFF" )
255- print (' Sending button 1 state: ' )
286+ print (" Sending button 1 state: " )
256287 client .publish (mqtt_button1 , button1_state )
257288 # for debounce
258289 while ts .touch_point :
259290 print ("Button 1 Pressed" )
260291 if i == 1 :
261292 # Momentary button type
262293 b .selected = True
263- print (' Sending button 2 state: ' )
294+ print (" Sending button 2 state: " )
264295 client .publish (mqtt_button2 , 1 )
265296 # for debounce
266297 while ts .touch_point :
267298 print ("Button 2 Pressed" )
268299 print ("Button 2 reliced" )
269- print (' Sending button 2 state: ' )
300+ print (" Sending button 2 state: " )
270301 client .publish (mqtt_button2 , 0 )
271302 b .selected = False
272303
273304 # Publish sensor data to MQTT
274- print (' Sending light sensor value: %d' % light_value )
305+ print (" Sending light sensor value: %d" % light_value )
275306 client .publish (mqtt_lux , light_value )
276307
277- print (' Sending temperature value: %d' % temperature )
308+ print (" Sending temperature value: %d" % temperature )
278309 client .publish (mqtt_temperature , temperature )
279310
280- print (' Sending motion sensor value: %d' % movement_value )
281- client .publish (mqtt_PIR , '{}' .format (movement_value ))
311+ print (" Sending motion sensor value: %d" % movement_value )
312+ client .publish (mqtt_PIR , "{}" .format (movement_value ))
0 commit comments