4

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
derpynodes
  • 41
  • 1
  • 2
  • Sending bytes over a network is trivial but designing a protocol stack which meets your requirements is not. Perhaps look into running an SSH server on the system where you want to accept remote connections. In its current form, this is arguably too broad for Stack Overflow. – tripleee Feb 28 '21 at 14:56

2 Answers2

3

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.

Shraft
  • 332
  • 1
  • 5
  • 1
    Without encryption and authentication, anyone who can connect to the socket can control the system. – tripleee Feb 28 '21 at 16:17
0

Here's how to do it:

In the Raspberry Pi, make sure you have OpenSSH (you probably do, try running apt install openssh-server if you cannot connect), and you also need the OpenSSH client on your computer(the 'ssh' command).

  1. First, let's look at your script. On your Python Script, you can write something like this to output JSON:

print(json.dumps(your message here....))

  1. On your Raspberry Pi, find out your private IP. You can do something like:ifconfig | grep 192

  2. On your computer, type: ssh pi@<your private IP> 'python <your script path, example: /home/pi/Desktop/script.py>'

    You may need to change pi to the username that you use on your Raspberry Pi.

  3. The script will run on your Raspberry Pi, and you will get the JSON output on your computer! You can easily automate this using subprocess and json.loads . This way is also secure, because SSH uses encryption.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
FastDeveloper
  • 367
  • 5
  • 15