I'm putting a drone together and I have a Python script running on a Raspberry Pi taking Xbox controller inputs and turning them into a Python output I can use like:
if (event.code == 'ABS_Y'):
if event.state in range(25001,32768):
print("Full Throttle Reverse Left")
kit.motor1.throttle = -1
The controller however needs to be connected to the Raspberry Pi and that limits my range. Can I connect the controller to a laptop and send those outputs to the Raspberry Pi over a router to execute Python commands?
I'm new to programming as a whole and I'm certain there are smarter ways of doing this so any assistance or alternate suggestions are welcome.
I recommend using a socket for your application. The client (running on the laptop) could take the xbox controller inputs and send it to the server (running on raspberry pi).
Here is a simple socket application:
Client:
import socket
ip = "192.168.xxx.yyy" # IP of Raspberry Pi
# connect to server
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((ip, 8080))
print("CLIENT: connected")
# send a message
msg = "I am CLIENT"
client.send(msg.encode())
# recive a message and print it
from_server = client.recv(4096).decode()
print("Recieved: " + from_server)
# exit
client.close()
Server:
import socket
ip = "192.168.xxx.yyy" # IP of Raspberry Pi
# start server
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.bind((ip, 8080))
serv.listen(5)
print("SERVER: started")
while True:
# establish connection
conn, addr = serv.accept()
from_client = ''
print("SERVER: connection to Client established")
while True:
# receive data and print
data = conn.recv(4096).decode()
if not data: break
from_client += data
print("Recieved: " + from_client)
# send message back to client
msg = "I am SERVER"
conn.send(msg.encode())
# close connection and exit
conn.close()
break
More information can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With