12
12
from adafruit_display_text .label import Label
13
13
from adafruit_button import Button
14
14
import adafruit_touchscreen
15
- from adafruit_minimqtt import MQTT
15
+ import adafruit_minimqtt . adafruit_minimqtt as MQTT
16
16
17
17
# ------------- WiFi ------------- #
18
18
@@ -62,17 +62,22 @@ def set_backlight(val):
62
62
board .DISPLAY .auto_brightness = False
63
63
board .DISPLAY .brightness = val
64
64
65
+
65
66
# 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
+ )
70
75
71
76
# ---------- Set the font and preload letters ----------
72
77
# Be sure to put your font into a folder named "fonts".
73
78
font = bitmap_font .load_font ("/fonts/Helvetica-Bold-16.bdf" )
74
79
# This will preload the text images.
75
- font .load_glyphs (b' abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()' )
80
+ font .load_glyphs (b" abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()" )
76
81
77
82
# ------------- User Inretface Eliments ------------- #
78
83
@@ -84,8 +89,7 @@ def set_backlight(val):
84
89
color_bitmap = displayio .Bitmap (320 , 240 , 1 )
85
90
color_palette = displayio .Palette (1 )
86
91
color_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 )
89
93
splash .append (bg_sprite )
90
94
91
95
buttons = []
@@ -95,18 +99,32 @@ def set_backlight(val):
95
99
BUTTON_MARGIN = 10
96
100
97
101
# 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
+ )
103
114
buttons .append (button_1 )
104
115
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
+ )
110
128
buttons .append (button_2 )
111
129
112
130
for b in buttons :
@@ -140,14 +158,14 @@ def set_backlight(val):
140
158
141
159
# ------------- MQTT Topic Setup ------------- #
142
160
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"
151
169
152
170
# ------------- MQTT Functions ------------- #
153
171
@@ -156,54 +174,64 @@ def set_backlight(val):
156
174
def connect (client , userdata , flags , rc ):
157
175
# This function will be called when the client is connected
158
176
# 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
+
161
180
162
181
def disconnected (client , userdata , rc ):
163
182
# This method is called when the client is disconnected
164
- print ('Disconnected from MQTT Broker!' )
183
+ print ("Disconnected from MQTT Broker!" )
184
+
165
185
166
186
def subscribe (client , userdata , topic , granted_qos ):
167
187
# 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
+
169
190
170
191
def publish (client , userdata , topic , pid ):
171
192
# 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
+
173
195
174
196
def message (client , topic , message ):
175
197
"""Method callled when a client's subscribed feed has a new
176
198
value.
177
199
:param str topic: The topic of the feed with a new value.
178
200
:param str message: The new value
179
201
"""
180
- print (' New message on topic {0}: {1}' .format (topic , message ))
202
+ print (" New message on topic {0}: {1}" .format (topic , message ))
181
203
if topic == "pyportal/feed1" :
182
- feed1_label .text = ' Next Bus: {}' .format (message )
204
+ feed1_label .text = " Next Bus: {}" .format (message )
183
205
if topic == "pyportal/feed2" :
184
- feed2_label .text = ' Weather: \n {}' .format (message )
206
+ feed2_label .text = " Weather: \n {}" .format (message )
185
207
if topic == "pyportal/button1" :
186
208
if message == "1" :
187
- buttons [0 ].label = "ON"
209
+ buttons [0 ].label = "ON"
188
210
buttons [0 ].selected = False
189
211
print ("Button 1 ON" )
190
212
else :
191
- buttons [0 ].label = "OFF"
213
+ buttons [0 ].label = "OFF"
192
214
buttons [0 ].selected = True
193
215
print ("Button 1 OFF" )
194
216
217
+
195
218
# ------------- Network Connection ------------- #
196
219
197
220
# Connect to WiFi
221
+ print ("Connecting to WiFi..." )
198
222
wifi .connect ()
223
+ print ("Connected to WiFi!" )
224
+
225
+ # Initialize MQTT interface with the esp interface
226
+ MQTT .set_socket (socket , esp )
199
227
200
228
# 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
+ )
207
235
208
236
# Connect callback handlers to client
209
237
client .on_connect = connect
@@ -212,10 +240,13 @@ def message(client, topic, message):
212
240
client .on_publish = publish
213
241
client .on_message = message
214
242
215
- print (' Attempting to connect to %s' % client .broker )
243
+ print (" Attempting to connect to %s" % client .broker )
216
244
client .connect ()
217
245
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
+ )
219
250
client .subscribe (mqtt_feed1 )
220
251
client .subscribe (mqtt_feed2 )
221
252
client .subscribe (mqtt_button1 )
@@ -228,18 +259,18 @@ def message(client, topic, message):
228
259
229
260
# Read sensor data and format
230
261
light_value = lux = light_sensor .value
231
- light_label .text = ' Light Sensor: {}' .format (light_value )
262
+ light_label .text = " Light Sensor: {}" .format (light_value )
232
263
temperature = round (adt .temperature )
233
- temperature_label .text = ' Temp Sensor: {}' .format (temperature )
264
+ temperature_label .text = " Temp Sensor: {}" .format (temperature )
234
265
movement_value = movement_sensor .value
235
- motion_label .text = ' PIR Sensor: {}' .format (movement_value )
266
+ motion_label .text = " PIR Sensor: {}" .format (movement_value )
236
267
237
268
# Read display button press
238
269
touch = ts .touch_point
239
270
if touch :
240
271
for i , b in enumerate (buttons ):
241
272
if b .contains (touch ):
242
- print (' Sending button%d pressed' % i )
273
+ print (" Sending button%d pressed" % i )
243
274
if i == 0 :
244
275
# Toggle switch button type
245
276
if button1_state == 0 :
@@ -252,30 +283,30 @@ def message(client, topic, message):
252
283
b .label = "OFF"
253
284
b .selected = True
254
285
print ("Button 1 OFF" )
255
- print (' Sending button 1 state: ' )
286
+ print (" Sending button 1 state: " )
256
287
client .publish (mqtt_button1 , button1_state )
257
288
# for debounce
258
289
while ts .touch_point :
259
290
print ("Button 1 Pressed" )
260
291
if i == 1 :
261
292
# Momentary button type
262
293
b .selected = True
263
- print (' Sending button 2 state: ' )
294
+ print (" Sending button 2 state: " )
264
295
client .publish (mqtt_button2 , 1 )
265
296
# for debounce
266
297
while ts .touch_point :
267
298
print ("Button 2 Pressed" )
268
299
print ("Button 2 reliced" )
269
- print (' Sending button 2 state: ' )
300
+ print (" Sending button 2 state: " )
270
301
client .publish (mqtt_button2 , 0 )
271
302
b .selected = False
272
303
273
304
# Publish sensor data to MQTT
274
- print (' Sending light sensor value: %d' % light_value )
305
+ print (" Sending light sensor value: %d" % light_value )
275
306
client .publish (mqtt_lux , light_value )
276
307
277
- print (' Sending temperature value: %d' % temperature )
308
+ print (" Sending temperature value: %d" % temperature )
278
309
client .publish (mqtt_temperature , temperature )
279
310
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