Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I send python script outputs to a Raspberry Pi over SSH/WiFi?

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.

like image 748
derpynodes Avatar asked Oct 20 '25 13:10

derpynodes


1 Answers

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.

like image 187
Shraft Avatar answered Oct 22 '25 04:10

Shraft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!