0

I made a Webserver using my Laptop and a few PHP-HTML-Javascript programms. Right now i am able to connect to these pages and everything works just fine. My problem is that i want to run a Python script on a RaspberryPi (using GPIOs to control neopixel LEDs) when a certain event occurs on the webpage. I am already succesfully "noticing" the event and i am able to start alerts if it happens.

My Question is: What code can i use in the Javascript on the webpage that starts the Python script on the RaspberryPi? Where does the Python script need to be saved? On the Pi or on the server?

The Python script is also working if i execute it from the terminal and it doesnt need any input data nor does it create output data, it should just controll the GPIOs

My RaspberryPi is not the Webserver, it is only connecting to it via browser.

Update: Got it to work thanks to the help in the answer, but now I am facing the next issue. I use an AJAX request to start my Python script controlling the LEDs, the routine takes 15 seconds until it is done. I want to be able to stop the LED-routine by pressing a button that is on the same webpage. I already tried

    abort()

but it doesn't work in this case(or at least not how I am using it).

I also tried making a second webpage from the server on the Raspberry Pi and then access it with another AJAX request. The second page starts a script that turns off the LEDs. Currently I am not able to access the second page until the first one is done with the routine.

  • Turning something on or off by changing GPIO pins should take a small fraction of a second, not tens of seconds. Have a close look at your code; perhaps put some timers in the Python and run it manually to locate the bottleneck. (If you are running an animation that takes several seconds to complete, you will have to plan a way to interrupt it.) – Bob Brown Mar 16 at 15:18
  • it doesnt take a few seconds to turn on the LEDs I didn't explain that very well. The animation takes a few seconds to complete. Yeah i am planning to interrupt it but i dont know how i should get an interruption-signal that is created on the other webserver to the webserver that is executing the python script and then into the python script to stop the animation – Hannes Ensbacher Mar 16 at 15:34
  • You will need a variable that can be tested by the code doing the animation to tell whether to keep running. You'll need to be able to "turn off" that variable from the code that signals "stop." You might use a "lockfile," but it's looking more and more like a single Python program with a GUI is the right tool for the job. – Bob Brown Mar 16 at 15:49
1

Web browsers work very hard to keep what happens within the browser from changing anything else on the computer running the browser. This is essential to maintaining security from malicious web sites.

One way of doing what you want is to run a small server on the Raspberry Pi and have the web page, also running in a browser on the Raspberry Pi, send a request to the server. There's an answer on Stack Overflow that shows how to do that using Flask. It looks like Flask for Python 2 is already installed with Raspbian Buster. (I haven't tried to run it.)

That will take care of running Python. You still have to send something from the web page's JavaScript to the Flask server. The easiest, although not the most lightweight, way to do that is with jQuery. You could send a jQuery post request to the port on which Flask is listening. According to the answer linked above, that's http://localhost:5000

Warning: If your Raspberry Pi is exposed to the Internet, others will start "sending stuff" to that port in short order, attempting to break in to your Pi. If you're on a home network that uses reserved addresses, you'll be OK. If not, you'll need to be careful to harden your Flask installation.

Depending on the complexity of your web page, it might be a lot simpler just to make a GUI interface for the Python program controlling the GPIO pins. You could use something like tkinter.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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