-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (109 loc) · 3.18 KB
/
main.py
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
from machine import Pin, PWM
import network
import socket
import utime, time
import random
import _thread
from EngineLED import EngineLED
def webpage():
html = f"""
<!DOCTYPE html>
<html>
<body>
<br><br><br><br>
<form action="./on">
<input type="submit" value="Engine On" style="font-size:40px; font-weight:bold; height:60px; width:220px; color:red;"/>
</form>
<br><br><br><br>
</form>
<form action="./off">
<input type="submit" value="Engine Off" style="font-size:40px; font-weight:bold; height:60px; width:220px; color:green;"/>
</form>
</body>
</html>
"""
return html
def serve(connection):
global EngineOn
while True:
client = connection.accept()[0]
request = client.recv(1024)
request = str(request)
try:
request = request.split()[1]
except IndexError:
pass
print(request)
if request == '/on?':
EngineOn = True
elif request == '/off?':
EngineOn = False
print("EngineOn: ", EngineOn)
html=webpage()
client.send(html)
client.close()
def open_socket(ip):
# Open a socket
address = (ip, 80)
connection = socket.socket()
connection.bind(address)
connection.listen(1)
print(connection)
return(connection)
# The Pi Pico has 2 cores, core 0 and core 1
# This core, core 0 will run the webservice
def core0_thread():
global EngineOn
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Your_SSID","Your_Password")
# Wait for connect or fail
wait = 10
while wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('wifi connection failed')
else:
print('connected')
ip=wlan.ifconfig()[0]
print('IP: ', ip)
try:
if ip is not None:
connection=open_socket(ip)
serve(connection)
except KeyboardInterrupt:
machine.reset()
# The Pi Pico has 2 cores, core 0 and core 1
# This core, core 1 will drive the LEDs and randomly
# change the brightness of each of the 5 engines
def core1_thread():
global EngineOn
Engine1 = EngineLED(9)
Engine2 = EngineLED(10)
Engine3 = EngineLED(11)
Engine4 = EngineLED(12)
Engine5 = EngineLED(13)
while True:
utime.sleep_ms(10)
if EngineOn == True:
Engine1.On()
Engine2.On()
Engine3.On()
Engine4.On()
Engine5.On()
else:
Engine1.Off()
Engine2.Off()
Engine3.Off()
Engine4.Off()
Engine5.Off()
# Global variable to send signals between threads
EngineOn = False
# Start both threads on core 0 and 1
second_thread = _thread.start_new_thread(core1_thread, ())
core0_thread()