edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
MohammadReza Sharifi
Published © MIT

Control an ESP32 Robot with a Logitech Racing Wheel

Turn an old Logitech racing wheel into a wireless robot controller!

IntermediateFull instructions provided5 hours63
Control an ESP32 Robot with a Logitech Racing Wheel

Things used in this project

Story

Read more

Schematics

Circuit Diagram

robot circuit diagram

Code

main.py

Python
this codes reads data from logitech device
import socket
import pygame
import time

ESP32_IP = '192.168.0.104'
PORT = 80

pygame.init()
pygame.joystick.init()

print("Joystick Count:", pygame.joystick.get_count())

if pygame.joystick.get_count() == 0:
    print("No joystick detected!")
    exit()

joy = pygame.joystick.Joystick(0)
joy.init()

print("Name:", joy.get_name())
print("Axes:", joy.get_numaxes())
print("Buttons:", joy.get_numbuttons())
print("Hats:", joy.get_numhats())
print("-" * 40)


# ===============================
# TCP Connection
# ===============================

client = None
last_command = None


def connect():
    global client

    while True:
        try:
            print("Connecting to ESP32...")
            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            client.connect((ESP32_IP, PORT))
            print("Connected.")
            return

        except Exception as e:
            print("Connection Failed:", e)
            time.sleep(1)


connect()


# ===============================
# Send Command
# ===============================

def send_command(command):
    global client
    global last_command

    #      
    if command == last_command:
        return

    try:
        client.sendall((command + "\n").encode())
        last_command = command

    except Exception as e:

        print("Connection Lost:", e)

        try:
            client.close()
        except:
            pass

        connect()

        try:
            client.sendall((command + "\n").encode())
            last_command = command

        except Exception as e:
            print("Send Failed:", e)


# ===============================
# Robot Functions
# ===============================

def go_forward():
    send_command("forward")
    print("forward")


def go_backward():
    send_command("backward")
    print("backward")


def go_left():
    send_command("left")
    print("left")


def go_right():
    send_command("right")
    print("right")


# ===============================
# Main Loop
# ===============================

running = True

while running:

    pygame.event.pump()

    axes = []

    for i in range(joy.get_numaxes()):
        axes.append(round(joy.get_axis(i), 3))

    if axes[0] < -0.3 and axes[1] < -0.49:
        go_right()

    elif axes[0] > 0.3 and axes[1] < -0.49:
        go_left()
        

    elif -0.3 < axes[0] < 0.3 and axes[1] < -0.5:
        go_forward()

    elif -0.158 < axes[0] < 0 and axes[1] > 0.49:
        go_backward()

    else:
        send_command("stop")

    time.sleep(0.02)

robot_hardware.py

MicroPython
this code controls robot
import network
import socket
import time
from machine import Pin

# ==========================
# WiFi Settings
# ==========================
SSID = ""
PASSWORD = ""

# ==========================
# Motor Pins
# ==========================

# Right Motor
IN3 = Pin(2, Pin.OUT)
IN4 = Pin(3, Pin.OUT)

# Left Motor
IN1 = Pin(4, Pin.OUT)
IN2 = Pin(5, Pin.OUT)

ENA = Pin(6,Pin.OUT)
ENB = Pin(7,Pin.OUT)

# ==========================
# Motor Functions
# ==========================

def stop():
    IN1.off()
    IN2.off()
    IN3.off()
    IN4.off()


def forward():
    IN3.on()
    IN4.off()

    IN1.on()
    IN2.off()


def backward():
    IN3.off()
    IN4.on()

    IN1.off()
    IN2.on()


def left():
    IN3.on()
    IN4.off()

    IN1.off()
    IN2.off()


def right():
    IN3.off()
    IN4.off()

    IN1.on()
    IN2.off()


# ==========================
# Connect WiFi
# ==========================

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

print("Connecting to WiFi...")
wlan.connect(SSID, PASSWORD)

while not wlan.isconnected():
    time.sleep(1)
    print("Connecting...")

print("Connected!")
print("IP Address:", wlan.ifconfig()[0])

# ==========================
# TCP Server
# ==========================

addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1]

server = socket.socket()
server.bind(addr)
server.listen(1)

print("Server Started")
print("Listening on Port 80")

ENA.on()
ENB.on()

stop()

while True:

    client, client_addr = server.accept()
    print("Client Connected:", client_addr)

    try:

        while True:

            data = client.recv(1024)

            if not data:
                break

            command = data.decode().strip()

            print("Received:", command)

            if command == "forward":
                forward()

            elif command == "backward":
                backward()

            elif command == "left":
                left()

            elif command == "right":
                right()

            elif command == "stop":
                stop()

    except Exception as e:
        print("Error:", e)

    finally:
        client.close()
        stop()
        print("Client Disconnected")

Control-an-ESP32-Robot-with-a-Logitech-Steering-Wheel

Credits

MohammadReza Sharifi
16 projects • 13 followers
I'm an Electrical Engineer and Maker.

Comments