简体   繁体   中英

Executing Python script to control GPIOs from a Webserver

I am working on a project that has a webserver(thats currently running from my laptop) and a RaspberryPi. I made a simple website with HTML, PHP and Javascript and i have a working python programm on my RaspberryPi. What i want to do is, to start the python programm using php currently i am using this command.

exec('sudo python /var/www/LED-Ring/python/examples/LED-Ring.py');

When i run the command directly in the terminal

sudo python /var/www/LED-Ring/python/examples/LED-Ring.py 

it works just fine.

I have already added the following

www-data ALL=NOPASSWD: ALL

into sudo visudo (i know that this is a security risk but i dont care right now i just want to get it to work)

what do i need on the Pi even though the webserver is running on the laptop?

the only thing the raspi needs to be able to do is to open the browser and go on the website.(which it already does) and then whenever the webserver send the command, the python script should start

the problem is probably related to permissions because i can run the python script with

"sudo python /var/www/LED-Ring/python/examples/LED-Ring.py" 

but i cant run it with

"sudo -u www-data python /var/www/LED-Ring/python/examples/LED-Ring.py"

when i use that command i get

Failed to create mailbox device : Operation not permitted

File "/var/www/LED-Ring/python/examples/LED-Ring.py", line 52, in module strip.begin

File "build/bdist.linux-armv7l/egg/neopixel.py", line 106 in begin

RuntimeError: ws2811:init failed with code -9 (failed to create mailbox device)

Don't use exec pleaze it's unsafe .Just use subprocess and the you can pass info between them wiht intermidiate file or via prints on shell

import subprocess
import time

list_subproc = []
list_to_pass = []
list_to_pass.append(myvar1)
list_to_pass.append(myvar2)
list_to_pass.append(myvar3)
list_to_pass.append(myvar4)

p = subprocess.Popen(['python','dialog29.py',list_to_pass],stdout=subprocess.PIPE,stderr=subprocess.STDOUT) # Call subprocess
list_subproc.append(p.pid)

time.sleep(0.2)
line = p.stdout.readline().lstrip().rstrip()
line = line.decode()
print(line)

For your othe problem because i cant recreate the problem probably you provide a wrong path.Provide a full path and check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM