forked from donskytech/micropython-raspberry-pi-pico
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pico_W_web_robot
111 lines (103 loc) · 3.77 KB
/
Pico_W_web_robot
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
import socket
import network
from machine import Pin, PWM
import time
import gc
ssid = 'RPI_PICO_AP' #Set access point name
password = '12345678' #Set your access point password
ctrl_pos = 0
m_dir = [(0,0,0,0),(.9,0,1,0),(1,0,1,0),(1,0,.9,0),(0,1,1,0),(1,0,0,1),(0,.9,0,1),(0,1,0,1),(0,1,0,.9)]
m_pins = [3,2,6,7]
m_drive = [PWM(Pin(x)) for x in m_pins]
for x in m_drive:
x.freq(10000)
x.duty_u16(0)
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True) #activating
while ap.active() == False:
pass
print('Connection is successful')
print(ap.ifconfig()[0])
s = socket.socket() #creating socket object
s.bind(('', 80))
s.listen(5)
def webpage():
#Template HTML
html = f"""
<!DOCTYPE html>
<html>
<head>
<style>
.b {{height:200px;width:200px;font-size:40px;background-color: #008CBA;border-radius: 15%;color: white;user-select: none;}}
</style>
</head>
<body>
<h1 style= "text-align: center;font-size: 48px;">Robot Control</h1>
<br><br>
<table style="margin-left: auto;margin-right: auto;">
<tr><td>
<button type="button" class="b" onpointerdown="mDN('1')" onpointerup="mUP('0')">Forward<br>Left</button>
</td><td>
<button type="button" class="b" onpointerdown="mDN('2')" onpointerup="mUP('0')">Forward</button>
</td><td>
<button type="button" class="b" onpointerdown="mDN('3')" onpointerup="mUP('0')">Forward<br>Right</button>
</td></tr>
<tr><td>
<button type="button" class="b" onpointerdown="mDN('4')" onpointerup="mUP('0')">Spin<br>Left</button>
</td><td>
<button type="button" class="b" onpointerdown="mDN('0')" onpointerup="mUP('0')">Stop</button>
</td><td>
<button type="button" class="b" onpointerdown="mDN('5')" onpointerup="mUP('0')">Spin<br>Right</button>
</td></tr>
<tr><td>
<button type="button" class="b" onpointerdown="mDN('6')" onpointerup="mUP('0')">Back<br>Left</button>
</td><td>
<button type="button" class="b" onpointerdown="mDN('7')" onpointerup="mUP('0')">Back</button>
</td><td>
<button type="button" class="b" onpointerdown="mDN('8')" onpointerup="mUP('0')">Back<br>Right</button>
</td></tr>
</table>
<script>
function mDN(s_obj) {{
var xhr = new XMLHttpRequest();
xhr.open("GET", "tEvent?"+s_obj, true);
xhr.send();
}}
function mUP(s_obj) {{
var xhr = new XMLHttpRequest();
xhr.open("GET", "tEvent?"+s_obj, true);
xhr.send();
}}
document.body.addEventListener("click", ev => {{
ev.preventDefault();
ev.stopImmediatePropagation();
}}, true);
</script>
</body>
</html>
"""
return str(html)
try:
while True:
conn, addr = s.accept()
#print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
request = str(request)
#print(request)
try:
request = request.split()[1]
except IndexError:
pass
if request.find('tEvent') > -1:
ctrl_pos = int(request.split('?')[1])
#print(ctrl_pos)
m_now = m_dir[ctrl_pos]
for e,x in enumerate(m_drive):
x.duty_u16(int(m_now[e] * 65535))
response = webpage()
conn.send(response)
conn.close()
gc.collect()
except KeyboardInterrupt:
machine.reset()