# Mars Rover Control — Wokwi Simulation
# Uses Access Point mode so the web preview works
import network
import socket
from machine import Pin, PWM
import time
# --- Hardware Setup ---
led = Pin(2, Pin.OUT) # LED represents drive motors
servo = PWM(Pin(18), freq=50) # Servo represents the arm
def set_servo(angle):
"""Move servo to a given angle (0 to 180 degrees)"""
duty = int((angle / 180) * 102 + 26)
servo.duty(duty)
# --- Wi-Fi Access Point Setup ---
# This creates a hotspot the simulation can connect to internally
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid='MarsRover', password='rover1234')
# Wait until the AP is ready
while not ap.active():
time.sleep(0.1)
print("Access Point active!")
print("Network: MarsRover")
print("Password: rover1234")
print("IP Address:", ap.ifconfig()[0])
# --- Web Page HTML ---
# This is the control page your phone/browser will see
def get_html(status="Ready"):
return """<!DOCTYPE html>
<html>
<head>
<title>Mars Rover</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial; text-align: center; background: #1a1a2e; color: white; padding: 20px; }
h1 { color: #e94560; }
button {
display: block; width: 200px; margin: 10px auto;
padding: 15px; font-size: 18px; border: none;
border-radius: 8px; cursor: pointer;
}
.drive { background: #16213e; color: #e94560; border: 2px solid #e94560; }
.arm { background: #16213e; color: #0f3460; border: 2px solid #53d8fb; color: #53d8fb; }
.stop { background: #e94560; color: white; }
.status { margin-top: 20px; padding: 10px; background: #16213e; border-radius: 8px; }
</style>
</head>
<body>
<h1>🚀 Mars Rover</h1>
<p class="status">Status: """ + status + """</p>
<h3>Drive</h3>
<a href="/forward"> <button class="drive">▲ Forward</button></a>
<a href="/backward"><button class="drive">▼ Backward</button></a>
<a href="/left"> <button class="drive">◀ Turn Left</button></a>
<a href="/right"> <button class="drive">▶ Turn Right</button></a>
<a href="/stop"> <button class="stop"> ■ STOP</button></a>
<h3>Arm</h3>
<a href="/arm_up"> <button class="arm">⇧ Arm Up</button></a>
<a href="/arm_down"><button class="arm">⇩ Arm Down</button></a>
</body>
</html>"""
# --- Web Server ---
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 80))
s.listen(5)
print("\nRover web server ready!")
print("Open the Web Server tab in Wokwi to see controls")
# --- Main Loop ---
while True:
try:
conn, addr = s.accept()
request = conn.recv(1024).decode()
# Read the first line of the request to get the path
first_line = request.split('\n')[0]
path = first_line.split(' ')[1] if len(first_line.split(' ')) > 1 else '/'
print("Request:", path)
# Handle each button press
if '/forward' in path:
led.on()
set_servo(90)
status = "Moving Forward"
elif '/backward' in path:
led.on()
set_servo(90)
status = "Moving Backward"
elif '/left' in path:
led.on()
status = "Turning Left"
elif '/right' in path:
led.on()
status = "Turning Right"
elif '/stop' in path:
led.off()
status = "Stopped"
elif '/arm_up' in path:
set_servo(160)
status = "Arm Up"
elif '/arm_down' in path:
set_servo(20)
status = "Arm Down"
else:
status = "Ready"
# Send the web page back
response = get_html(status)
conn.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')
conn.send(response)
conn.close()
except Exception as e:
print("Error:", e)
conn.close()