Search code examples
pythonsshraspberry-piwirelessvnc

How to remotely control Raspberry Pi WITHOUT VNC


So to go into more detail, I'm basically looking to construct a program (that will eventually become a gui but I don't need to worry about this now) using python that will be able to interact with the raspberry pi. To elaborate, I need this program to run on a windows machine that a raspberry pi can connect to via wifi. I say wifi because this situation will require that eventually many different raspberry pi's are able to connect to this same machine and take commands and spit back outputs.

I don't want to use VNC because I don't want to use the raspberry pi's gui to be able to run programs remotely on the pi. This is why I looked into SSH into more detail but once again I have run into a problem. Being able to command the pi wirelessly to do things via the CMD is nice and all but I need a user friendly method for when this project is done so others can do it.

Essentially, is there a way to write a python script on my windows PC that will have methods that can automatically interact with the pi as if it were through SSH? Like if I have a method called runProgram1() in the script on the PC it will automatically run program1 on the raspberry pi wirelessly? I can always convert this to a gui later but the issue is getting the python script in the first place.

A quick system requirement outline would be for the windows pc to be able to have methods that can do things like automatically turn on/off the pi, run a program already on the pi, take data that the pi collects and spit it out to the windows machine, and add more pi's to the system. And all this needs to be done wireless as the pi's will all be out of reach when they are in their final place.

So far I haven't found anything quite like this online, so I was hoping the gurus of stackoverflow could help. I know its quite a lot to ask but just being pointed in the right direction would help loads. Thanks in advance!


Solution

  • I suggest you to build a very simple flask web server on your raspberry pi and send requests from your Windows gui.

    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/function_one")
    def function_one():
        # Do something on rpi or start a subprocess
        return "Some message"
    
    app.run(host='0.0.0.0')
    

    This should be running on your rpi. Then, from the Windows gui you can just send a get with requests library to that endpoint, maybe onclick or when you prefer.

    import requests
    requests.get('http://<yout_rpi_local_ip>:5000/function_one')
    

    This will trigger the function on your RPI from Windows. You can even secure it with authentication.