Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views1 page

Working Code

This document contains a Python script that establishes a socket connection to an ESP32 device to send movement commands. Users can input commands to control the ESP32, with options to move in various directions or stop. The script includes error handling and a loop for continuous user input until the user decides to quit.

Uploaded by

masteraashish143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Working Code

This document contains a Python script that establishes a socket connection to an ESP32 device to send movement commands. Users can input commands to control the ESP32, with options to move in various directions or stop. The script includes error handling and a loop for continuous user input until the user decides to quit.

Uploaded by

masteraashish143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import socket

# ESP32 IP address and port (change this to match your ESP32's IP address)
esp32_ip = '192.168.1.12' # Replace with your ESP32 IP address
port = 80 # The same port you used in the ESP32 server code

def send_command(command):
try:
# Create a socket connection
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.settimeout(5) # Set a timeout for the connection

# Connect to the ESP32


print(f"Connecting to {esp32_ip}:{port}...")
client_socket.connect((esp32_ip, port))
print("Connected to ESP32")

# Send command to ESP32


command = command.strip() + '\n' # Add newline to the command
client_socket.sendall(command.encode()) # Send the command as bytes

print(f"Command sent: {command.strip()}")

except socket.error as e:
print(f"Error in communication: {e}")
finally:
# Ensure the socket is closed after sending the command
client_socket.close()

# Main loop for user input


if __name__ == "__main__":
while True:
print("\nEnter a command:")
print("1 - Move Up")
print("2 - Move Right")
print("3 - Move Left")
print("4 - Move Forward")
print("5 - Move Backward")
print("6 - Move Down")
print("7 - Stop")

command = input("Enter the command (1-7, or 'q' to quit): ").strip()

if command in ['1', '2', '3', '4', '5', '6', '7']:


send_command(command)
elif command == 'q':
print("Exiting...")
break
else:
print("Invalid command. Please enter a number between 1 and 7.")

You might also like