import time import network import socket import asyncio ssid = '5' password = 'nevem123' move = 0 dir = 0 def read_html(filename): try: with open(filename, "r") as file: return file.read() except: return "

File not found

" def init_wifi(ssid, password): wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) connection_timeout = 10 while connection_timeout > 0: if wlan.status() >= 3: break connection_timeout -= 1 print('Waiting for Wi-Fi connection...') time.sleep(1) if wlan.status() != 3: print("Failed to connect.") return False else: print('Connection successful!') print('IP address:', wlan.ifconfig()[0]) return True async def handle_client(reader, writer): global move, dir try: request_line = await reader.readline() if not request_line: return request = request_line.decode().strip() if '/up' in request: move += 5 elif '/down' in request: move -= 5 elif '/right' in request: dir += 5 elif '/left' in request: dir -= 5 while True: line = await reader.readline() if line == b"\r\n" or line == b"": break response_body = read_html("./webpage/index.html") writer.write(b"HTTP/1.0 200 OK\r\n") writer.write(b"Content-type: text/html\r\n") writer.write(b"Connection: close\r\n\r\n") writer.write(response_body.encode("utf-8")) await writer.drain() except Exception as e: print("Error handling client:", e) finally: await writer.aclose() async def move_car_loop(): global move, dir print("Car control loop started.") while True: print(f"Executing: Speed {move} , Direction {dir}") await asyncio.sleep(0.5) move = move * 0.9 dir = dir * 0.9 if(abs(move) < 1): move = 0 if(abs(dir) < 1): dir = 0 async def main(): if not init_wifi(ssid, password): return print("Starting async web server...") server = await asyncio.start_server(handle_client, "0.0.0.0", 80) car_task = asyncio.create_task(move_car_loop()) while True: await asyncio.sleep(1) try: asyncio.run(main()) except KeyboardInterrupt: print("Program Interrupted")