Cyroq
Posts: 58
Joined: Sat Dec 14, 2013 2:40 pm

PHP executes Python but not GPIO

Fri Jan 27, 2017 9:40 am

Hi all,

For an automation project I'm hooking my catfeeder-system up to a webserver. I've got the server running and by using shell_exec() in the php file, I can execute .py scripts.
However, as soon as the script needs to control GPIO functions, the script stops working (or at least, I don't see any new output and the connected LEDs aren't doing anything. Yes, my catfeeder-system needs LEDs :D ).

Code: Select all

def fadeInRed():
	print("fadeInRed().")
	GPIO.setmode(GPIO.BOARD)
	GPIO.setup(12, GPIO.OUT)

	p = GPIO.PWM(12, 50)  # channel=12 frequency=50Hz
	p.start(0)

	for dc in range(0, 101, 8):
    		p.ChangeDutyCycle(dc)
	        time.sleep(0.3)
	time.sleep(1)
	p.ChangeDutyCycle(0)
	p.stop()
	GPIO.cleanup()
The print is executed, but the next lines are not.
Does GPIO need special permissions perhaps? The script works fine when executed by ssh or by fysical button.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: PHP executes Python but not GPIO

Fri Jan 27, 2017 12:59 pm

Your webserver runs as user www-data, group www-data. That doesn't have permission.

sudo usermod -a -G gpio www-data
sudo systemctl restart apache2
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Cyroq
Posts: 58
Joined: Sat Dec 14, 2013 2:40 pm

Re: PHP executes Python but not GPIO

Thu Feb 02, 2017 11:27 am

That was an easy fix. Thanks!

Return to “Automation, sensing and robotics”