1

I want to run python scripts on raspberry pi and control this process with my android phone. Now, I can run python scripts on rpi3 from my phone via ssh. I have 2 questions:

  1. If a python script on the pi runs how could I stop running the script with my phone?

  2. How can I send back a message to my phone that the script-running has ended?

Thanks

johnny cigar
  • 119
  • 10

1 Answers1

3

The easiest way would be to use Flask. Flask is a Python library that basically runs a website server for you. Then you can create a website with Flask in which you have some buttons or switches. You can add actions to buttons (they are basically HTML buttons). So if you want to for example run a python script or stop it, you can just set the button's action to that. I advise you to read documentation for more.

  1. As for how to execute commands, I found you similar post to yours on Stack Overflow
  2. Then when the function you executed terminates, you can just redirect back to homepage (of course don't forget to implement that website will include data about running scripts or something so you know which are running and when you kill one script, the redirect will get you back to homepage and now there won't be any sign of that script anymore).

Now mind, that Flask is really lightweight, so if you plan to be extending your project significantly, you may want to consider using Nginx or Apache as a website back-end server, because they are more powerfull and Raspberry Pi can handle both easily (I am currently running Nginx on mine, was previously running Apache 2). In this case you might need to know a bit of JavaScript or PHP to be able to execute commands or if you don't mind learning, you can get yourself into Django, a pure-python front-end webserver which you combine with Nginx or Apache. I am running Nginx + Django for all dynamic-content sites I have/develop. There are lots of tutorials on YouTube snd documentation is among the best out there. Everything explained in great detail (some things aren't though, but great majority is) with lots of examples.

campovski
  • 2,979
  • 19
  • 38
  • And if something more extensive is needed django might be a good choice - combined with apache or nginx, like you said or using the standalone server. – Ch4t4r Sep 06 '17 at 15:22
  • Indeed, I am running django myself for all dynamic websites – campovski Sep 06 '17 at 15:27
  • Thanks to everyone for the quick answers! – johnny cigar Sep 06 '17 at 15:34
  • No problemo, amigo! If you are interested in something else, just ask. But better, **explore** from what we provided ;) – campovski Sep 06 '17 at 15:36
  • I explored it and a couple of questions come up. I installed flask and created a little website, now I'm getting to know with it, but there is one thing I'm still not sure how to do (I'm new to this client-server-networking stuff) Is it possible to send for example a text file from my flask webserver to my android phone (client)? If yes, what would be the steps? It'd be a huge help, thanks in advance! – johnny cigar Sep 07 '17 at 11:31
  • @johnnycigar send in what way? You mean that client (phone) is able to download a file from server or that server actually sends the file automatically to phone? – campovski Sep 07 '17 at 11:38
  • I would be interested in both but If I could implement the second option that would be the best, – johnny cigar Sep 07 '17 at 13:48
  • Could you help in either way? It would be a huge help! – johnny cigar Sep 08 '17 at 16:20
  • Sorry for not responding. Take a look at `send_file`. It allows client to download a file. Sending it automatically is a hard one – campovski Sep 08 '17 at 16:22