-2

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!

AndreiF
  • 21
  • 5

1 Answers1

2

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.

Manuel Fedele
  • 1,390
  • 13
  • 25
  • This actually sounds really promising! I'll try and set it up now so far this is the closest thing to what I was looking for and I should be able to work with it. Thank you!! – AndreiF Mar 15 '19 at 16:36
  • I don't understand why my answare is downvoted :) I is online with op request. – Manuel Fedele Mar 15 '19 at 16:49
  • I don't understand either my question was downvoted as well. I guess its some internet troll I tried to upvote it but since I am fairly new to this site the rules say it won't show up :( – AndreiF Mar 15 '19 at 17:03
  • so I actually can't get the code to work it gives me this error: requests.exceptions.ConnectionError: HTTPConnectionPool(host='%3c192.168.x.x%3e', port=5000): Max retries exceeded with url: /function_one (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 11001] getaddrinfo failed',)) – AndreiF Mar 15 '19 at 19:32
  • Does this happen even if you hit that url in your browser or just if you hit it from the gui? BTW I suggest you to open another question for that, with flask tag – Manuel Fedele Mar 15 '19 at 19:38
  • I get this error only when I run it in the python IDE with the exact code provided above but with the appropriate IP address. When I type it in the browser it just doesn't work. I am typing http://<192.168.12.1>:5000/function_one in the browser and the python script but it doesn't work. – AndreiF Mar 15 '19 at 19:46
  • Ouch. < and > are placeholders for the url. Use http://192.168.x.x:5000/function_one without < and > – Manuel Fedele Mar 15 '19 at 19:48
  • ...Welp that worked. Thanks for all your help! Sorry I'm very new to all this haha – AndreiF Mar 15 '19 at 19:49