import network import socket from time import sleep import machine from machine import Pin # Yes, these could be in another file. But on the Pico! So no more secure. :) ssid = 'Your_Network_Name' password = 'Your_WiFi_Password' # Define pins to pin motors! Mot_A_Forward = Pin(18, Pin.OUT) Mot_A_Back = Pin(19, Pin.OUT) Mot_B_Forward = Pin(20, Pin.OUT) Mot_B_Back = Pin(21, Pin.OUT) def move_forward(): Mot_A_Forward.value(1) Mot_B_Forward.value(1) Mot_A_Back.value(0) Mot_B_Back.value(0) def move_backward(): Mot_A_Forward.value(0) Mot_B_Forward.value(0) Mot_A_Back.value(1) Mot_B_Back.value(1) def move_stop(): Mot_A_Forward.value(0) Mot_B_Forward.value(0) Mot_A_Back.value(0) Mot_B_Back.value(0) def move_left(): Mot_A_Forward.value(1) Mot_B_Forward.value(0) Mot_A_Back.value(0) Mot_B_Back.value(1) def move_right(): Mot_A_Forward.value(0) Mot_B_Forward.value(1) Mot_A_Back.value(1) Mot_B_Back.value(0) #Stop the robot as soon as possible move_stop() def connect(): #Connect to WLAN wlan = network.WLAN(network.STA_IF) wlan.connect(ssid, password) while wlan.isconnected() == False: print('Waiting for connection...') sleep(1) ip = wlan.ifconfig()[0] print(f'Connected on {ip}') return ip def open_socket(ip): # Open a socket address = (ip, 80) connection = socket.socket() connection.bind(address) connection.listen(1) return connection def webpage(): #Template HTML html = f""" Zumo Robot Control
""" return str(html) def serve(connection): #Start web server while True: client = connection.accept()[0] request = client.recv(1024) request = str(request) try: request = request.split()[1] except IndexError: pass if request == '/forward?': move_forward() elif request =='/left?': move_left() elif request =='/stop?': move_stop() elif request =='/right?': move_right() elif request =='/back?': move_backward() html = webpage() client.send(html) client.close() try: ip = connect() connection = open_socket(ip) serve(connection) except KeyboardInterrupt: machine.reset()