-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_WiFi_Controlled.py
176 lines (157 loc) · 4.98 KB
/
05_WiFi_Controlled.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
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
import os
import time
import ipaddress
import wifi
import socketpool
import board
import microcontroller
import digitalio
import pwmio
from adafruit_motor import motor
from digitalio import DigitalInOut, Direction
from adafruit_httpserver import Server, Request, Response, POST
# Left Motor
PWM_M1A = board.GP8
PWM_M1B = board.GP9
# Right Motor
PWM_M2A = board.GP10
PWM_M2B = board.GP11
# DC motor setup
pwm_1a = pwmio.PWMOut(PWM_M1A, frequency=10000)
pwm_1b = pwmio.PWMOut(PWM_M1B, frequency=10000)
motorL = motor.DCMotor(pwm_1a, pwm_1b)
pwm_2a = pwmio.PWMOut(PWM_M2A, frequency=10000)
pwm_2b = pwmio.PWMOut(PWM_M2B, frequency=10000)
motorR = motor.DCMotor(pwm_2a, pwm_2b)
def Robot_Movement(sL, sR):
motorL.throttle = sL
motorR.throttle = sR
def move_forward():
print("Forward")
Robot_Movement(0.5, 0.53)
def move_backward():
print("Backward")
Robot_Movement(-0.5, -0.53)
def move_left():
print("Left")
Robot_Movement(0, 0.5)
def move_right():
print("Right")
Robot_Movement(0.5, 0)
def move_stop():
print("Stop")
Robot_Movement(0, 0)
# Connect to network
print("Connecting to WiFi")
try:
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
print("Connected to WiFi")
print("IP address:", wifi.radio.ipv4_address)
except Exception as e:
print("Failed to connect to WiFi:", str(e))
microcontroller.reset()
pool = socketpool.SocketPool(wifi.radio)
server = Server(pool, "/static", debug=False) # Set debug to False to reduce output
def webpage():
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Robo Pico Control</title>
<script>
function buttonDown(button) {{
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(button + "=true");
}}
function buttonUp() {{
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("stop=true");
}}
</script>
<style>
h1 {{
text-align: center;
}}
body {{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 80vh;
margin: 0;
}}
.controls {{
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}}
.button {{
font-size: 90px;
display: flex;
align-items: center;
justify-content: center;
background: white;
padding: 10px;
user-select: none;
width: 80px;
height: 80px;
}}
</style>
</head>
<body>
<h1>Robo Pico Wifi Control Car</h1>
<div class="controls">
<div></div>
<div class="button" id="forward" ontouchstart="buttonDown(this.id)" ontouchend="buttonUp()"
onmousedown="buttonDown(this.id)" onmouseup="buttonUp()">⬆️</div>
<div></div>
<div class="button" id="left" ontouchstart="buttonDown(this.id)" ontouchend="buttonUp()"
onmousedown="buttonDown(this.id)" onmouseup="buttonUp()">⬅️</div>
<div></div>
<div class="button" id="right" ontouchstart="buttonDown(this.id)" ontouchend="buttonUp()"
onmousedown="buttonDown(this.id)" onmouseup="buttonUp()">➡️</div>
<div></div>
<div class="button" id="backward" ontouchstart="buttonDown(this.id)" ontouchend="buttonUp()"
onmousedown="buttonDown(this.id)" onmouseup="buttonUp()">⬇️</div>
<div></div>
</div>
</body>
</html>
"""
return html
@server.route("/")
def base(request: Request):
return Response(request, webpage(), content_type='text/html')
@server.route("/", POST)
def buttonpress(request: Request):
raw_text = request.body.decode("utf8") # Use .body instead of raw_request
if "forward" in raw_text:
move_forward()
elif "backward" in raw_text:
move_backward()
elif "right" in raw_text:
move_right()
elif "left" in raw_text:
move_left()
elif "stop" in raw_text:
move_stop()
return Response(request, webpage(), content_type='text/html')
print("Starting server...")
try:
server.start(str(wifi.radio.ipv4_address), port=80)
print(f"Server running at http://{wifi.radio.ipv4_address}")
except Exception as e:
print("Failed to start server:", str(e))
time.sleep(5)
microcontroller.reset()
while True:
try:
server.poll()
except Exception as e:
continue