-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
290 lines (250 loc) · 12.1 KB
/
code.py
File metadata and controls
290 lines (250 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# SPDX-FileCopyrightText: 2023 Frank Grootens, Berlin
# SPDX-License-Identifier: MIT
import os
import time
import ssl
import wifi
import socketpool
import microcontroller
import supervisor
import board
import rotaryio
import digitalio
from digitalio import DigitalInOut, Direction, Pull
import adafruit_requests
from adafruit_debouncer import Button
# Define your Photobooth IP and Hardware Button Server Port
photobooth_ip = "Your_Server_IP"
button_server_port = "Your_Server_Port" ## Default port is 14711
btn1_pin = DigitalInOut(board.GP10) ### Connect to GND
btn1_pin.direction = Direction.INPUT
btn1_pin.pull = Pull.UP
btn1 = Button(btn1_pin, short_duration_ms=500, long_duration_ms=1000, value_when_pressed=False)
led1 = digitalio.DigitalInOut(board.GP11) ### Connect to GND
led1.direction = digitalio.Direction.OUTPUT
btn2_pin = DigitalInOut(board.GP12) ### Connect to GND
btn2_pin.direction = Direction.INPUT
btn2_pin.pull = Pull.UP
btn2 = Button(btn2_pin, short_duration_ms=500, long_duration_ms=1000, value_when_pressed=False)
led2 = digitalio.DigitalInOut(board.GP13) ### Connect to GND
led2.direction = digitalio.Direction.OUTPUT
btn3_pin = DigitalInOut(board.GP14) ### Connect to GND
btn3_pin.direction = Direction.INPUT
btn3_pin.pull = Pull.UP
btn3 = Button(btn3_pin, short_duration_ms=500, long_duration_ms=1000, value_when_pressed=False)
led3 = digitalio.DigitalInOut(board.GP15) ### Connect to GND
led3.direction = digitalio.Direction.OUTPUT
btn4_pin = DigitalInOut(board.GP16) ### Connect to GND
btn4_pin.direction = Direction.INPUT
btn4_pin.pull = Pull.UP
btn4 = Button(btn4_pin, short_duration_ms=500, long_duration_ms=1000, value_when_pressed=False)
led4 = digitalio.DigitalInOut(board.GP17) ### Connect to GND
led4.direction = digitalio.Direction.OUTPUT
btn5_pin = DigitalInOut(board.GP18) ### Connect to GND
btn5_pin.direction = Direction.INPUT
btn5_pin.pull = Pull.UP
btn5 = Button(btn5_pin, short_duration_ms=500, long_duration_ms=1000, value_when_pressed=False)
led5 = digitalio.DigitalInOut(board.GP19) ### Connect to GND
led5.direction = digitalio.Direction.OUTPUT
sw_pin = DigitalInOut(board.GP2) ### Connect to GND
sw_pin.direction = Direction.INPUT
sw_pin.pull = Pull.UP
sw = Button(sw_pin, short_duration_ms=500, long_duration_ms=1000, value_when_pressed=False)
encoder = rotaryio.IncrementalEncoder(board.GP4, board.GP3)
last_position = encoder.position
# Web requests - URLs that triggers a certain action on the Photobooth Remote Server
picture_url = f"http://{photobooth_ip}:{button_server_port}/commands/start-picture" #### Specify your Photobooth IP and Hardware Button Server Port ####
collage_url = f"http://{photobooth_ip}:{button_server_port}/commands/start-collage" #### Specify your Photobooth IP and Hardware Button Server Port ####
print_url = f"http://{photobooth_ip}:{button_server_port}/commands/start-print" #### Specify your Photobooth IP and Hardware Button Server Port ####
video_url = f"http://{photobooth_ip}:{button_server_port}/commands/start-video" #### Specify your Photobooth IP and Hardware Button Server Port ####
custom_url = f"http://{photobooth_ip}:{button_server_port}/commands/start-custom" #### Specify your Photobooth IP and Hardware Button Server Port ####
shutdown_url = f"http://{photobooth_ip}:{button_server_port}/commands/shutdown-now" #### Specify your Photobooth IP and Hardware Button Server Port ####
cw_url = f"http://{photobooth_ip}:{button_server_port}/commands/rotary-cw" #### Specify your Photobooth IP and Hardware Button Server Port ####
ccw_url = f"http://{photobooth_ip}:{button_server_port}/commands/rotary-ccw" #### Specify your Photobooth IP and Hardware Button Server Port ####
btn_press_url = f"http://{photobooth_ip}:{button_server_port}/commands/rotary-btn-press" #### Specify your Photobooth IP and Hardware Button Server Port ####
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
try:
# Connect to SSID using your credentials
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
print("Connected to WiFi...")
# prints MAC address to REPL
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])
# prints IP address to REPL
print("My IP address is", wifi.radio.ipv4_address)
except Exception as e:
print("Cannot connect to WiFi. Try again 10 seconds...", e)
time.sleep(10)
def wifi_connect():
while not wifi.radio.connected:
try:
# Connect to SSID using your credentials
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
print("Reconnected to WiFi...")
# prints MAC address to REPL
print("wifi_connect: My MAC addr:", [hex(i) for i in wifi.radio.mac_address])
# prints IP address to REPL
print("My IP address is", wifi.radio.ipv4_address)
time.sleep(1)
except Exception as e:
print("Cannot connect to WiFi. Try again in 10 seconds...", e)
time.sleep(10)
try:
while True:
btn1.update()
led1.value = not btn1.value
btn2.update()
led2.value = not btn2.value
btn3.update()
led3.value = not btn3.value
btn4.update()
led4.value = not btn4.value
btn5.update()
led5.value = not btn5.value
sw.update()
try:
if btn1.short_count:
# Execute the get request to the server
response = requests.get(picture_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if btn1.long_press:
# Execute the get request to the server
response = requests.get(collage_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if btn2.short_count:
# Execute the get request to the server
response = requests.get(print_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if btn2.long_press:
# Execute the get request to the server
response = requests.get(print_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if btn3.short_count:
# Execute the get request to the server
response = requests.get(video_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if btn3.long_press:
# Execute the get request to the server
response = requests.get(video_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if btn4.short_count:
response = requests.get(custum_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if btn4.long_press:
# Execute the get request to the server
response = requests.get(custum_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if btn5.short_count:
# Execute the get request to the server
response = requests.get(shutdown_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if btn5.long_press:
# Execute the get request to the server
response = requests.get(shutdown_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if sw.short_count:
# Execute the get request to the server
response = requests.get(btn_press_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
if sw.long_press:
# Execute the get request to the server
response = requests.get(btn_press_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
current_position = encoder.position
position_change = current_position - last_position
if position_change > 0:
for _ in range(position_change):
# Execute the get request to the server
response = requests.get(ccw_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
elif position_change < 0:
for _ in range(-position_change):
# Execute the get request to the server
response = requests.get(cw_url)
# Show server response
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()
time.sleep(0.2)
last_position = current_position
except Exception as e:
print("An error occured" ,e)
time.sleep(2)
while not wifi.radio.connected:
print("Not connected to wifi - trying to reconnect")
wifi_connect()
while wifi.radio.connected:
print("HTTP server down? Restart photobooth application or press F5 to refresh browser. Pico will soft reset in 2 seconds")
time.sleep(2)
supervisor.reload()
except Exception as e:
print("An error occured" ,e)
print("Pico will hard reset in 10 seconds")
time.sleep(10)
micorcontroller.reset()