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.")