Herunterladen Diese Seite drucken

Arduino Alvik Bedienungsanleitung Seite 47

Werbung

user-manual-de.md
print(f'Listening on
Verarbeitung von HTTP-Anfragen
Der folgende Code verarbeitet eingehende HTTP-Anfragen und bewegt den Roboter in die angegebene
Richtung. Beachten Sie, dass der Roboter die Aktion so lange fortsetzt, bis die Pause-Taste gedrückt wird.
def
handle_request(conn):
try:
request = conn.recv(1024).decode('utf-8')
first_line = request.split('\n')[0]
path =
# Strip query strings (e.g., "/up?")
if
'?'
path = path.split('?')[0]
print(f"Request path:
# Ignore favicon requests
if
path == '/favicon.ico':
conn.send('HTTP/1.1 204 No
conn.send('Connection:
return
# Control the robot based on the request path
if
path == '/up':
alvik.set_wheels_speed(30, 30)
elif
path == '/down':
alvik.set_wheels_speed(-30, -30)
elif
path == '/left':
alvik.set_wheels_speed(-30, 30)
elif
path == '/right':
alvik.set_wheels_speed(30, -30)
else:
alvik.brake()
# Send the response for valid paths
conn.send('HTTP/1.1 200
conn.send('Content-Type:
conn.send('Connection:
conn.sendall(html)
except
OSError
if
e.errno == 104:
print("Connection reset by
else:
print(f"Error:
finally:
conn.close()
{sta_if.ifconfig()[0]}:{port}')
first_line.split('
in
path:
{path}")
OK\n')
text/html\n')
close\n\n')
as
e:
# ECONNRESET error
{e}")
# Get the first line of the request
')[1]
# Extract the path (e.g., "/up")
Content\n')
close\n\n')
client.")
47 / 70
2024-11-14

Werbung

loading

Diese Anleitung auch für:

Akx00066