I'm sort of new with web app programming so sorry if it is obvious for you guys! I appreciate the time you take for me !

I tried to explain my problem the best I could in the title! :p I am building a drone, and i need to launch a script from my computer beeing connected to the PI in wifi. I am trying to make a web app for that purpose.

I couldn't find anything on the subject so here I am, is there a way to launch my SSH connection to the raspberryPI 3 and then execute my script from the web app on my computer?

(for now i am using putty and i need to establish the connection by hand every time)

  • Have you considered hosting your web app on your raspberry pi then connecting to it from your controlling computer? that would make it very easy. let me know if you want me to explain how this would work @RobinMariaccia – scitronboy Apr 2 '18 at 19:33
  • Thank you for your help! I would really love to hear about how this would work! @scitronboy – Robin Mariaccia Apr 3 '18 at 22:03

I have no idea how you could ssh into something with a web app, but if you hosted the web app on your rpi, you wouldn't even need to do that. when you say web app, I assume you mean an app built with HTML and javascript that runs in your browser. if you host(serve) that web app from your rpi, then your controlling computer would connect to the rpi to access the web app, removing the need for an ssh connection. this would be how it works: you have a simple python script running on your raspberry pi which hosts your web app on your lan(local area network). you access the web app on your computer by typing your pi's ip address into the address bar(for example http://192.168.*.*/, or whatever your pi's local ip is. you can find the pi's local ip by typing ifconfig wlan0 into the command line and looking at the result that says inet addr:) Then, there is a button on the web app that you press to launch your script. since the pi is hosting the web app, it knows when you press that button, and launches the script! This might sound complicated, but it's not really...

If you would like to use this approach, I would recommend using the Flask python framework to host your web app. Here's a great tutorial for building some internet-controlled leds, which you could easily change to control a script instead:

Here would be your script that you'd run on the pi:

contents of /home/pi/webApp/droneServerScript.py:

from flask import Flask, render_template, request
from subprocess import Popen
app = Flask(__name__)

def main():
    return renderTemplate('main.html')

def runscript():
    x = Popen(["put your launch command here. for example, if you launch it with 'python script.py', replace this message with that."])
    return redirect("/")

if __name__ == "__main__":
   app.run(host='', port=80, debug=True)

then , in the same directory, put a html file called main.html

the contents of /home/pi/webApp/main.html:

<a href="/runscript">run the script</a>

then, type sudo nano /etc/rc.local, and add

sudo python /home/pi/webApp/droneServerScript.py

to the bottom. This will make it so that your server starts up automatically every time your pi boots!

hope this helps and feel free to ask for clarification!

  • Robin, I haven't actually tested this script, so if you get any errors, just let me know and I'll fix them for you! – scitronboy Apr 4 '18 at 16:23

If I understand correctly, you have a computer running a web browser and a Raspberry Pi running some script.

+--------------+            +-------------+
|              |            |             |
|  computer    | <= WiFi => |     Pi      |
| (w/ browser) |            | (w/ script) |
+--------------+            +-------------+

Since you do not mention a server, I'm going to assume that the web browser page is either statically loaded from a local file on the computer or that it has no further interaction with a server after the page is loaded.

If that's correct and you wish to control the Pi from the browser, one way to do that is to use the WebSocket interface. On the browser side, it's fairly straightforward to open a connection to the Pi:

const socket = new WebSocket('ws://raspberrypi.local.net:5000');

This assumes that your Pi is connected to the network and is reachable at raspberrypi.local.net, but you could also use either an IPv4 or IPv6 address instead. In this example, it's using port 5000 on the Pi.

On the Pi, it's a bit more complex because you need to implement a server capable of interpreting WebSocket calls. If I were writing it, I might use Python or C++ libraries for that. From there, one could run a script.

On safety

It's not stated exactly what you're doing with the drone, or whether you anticipate having the Pi aboard the drone, but I think it's worth noting that safety should be a consideration. Specifically, the drone should be able to land safely if its network connection is interrupted for any reason (and even more critical if you're trying to control the drone that way). That would likely be a slow, controlled descent so that any animals or people beneath it would be able to avoid injury. It also helps prevent damage to the drone.

Not the answer you're looking for? Browse other questions tagged or ask your own question.