james3126
Posts: 5
Joined: Wed Oct 25, 2017 2:05 pm

GPIO control lost (without error) when running script from PHP.

Sun Mar 04, 2018 9:27 pm

I have been creating a little program that will allow me to control the GPIO pins through Python. This all went smoothly. The problem happens when I try calling this python script from PHP. It seems to loose functionality to control the GPIO pins, and crashes silently without any error.

The following files exist:
RRR.py - The main file thats loosing perms
configCreator.py - Generates a config.ini file that the RRR.py file makes use of
config.ini - The config that has been generated
install.py - Installs apache2 and php and generates a webpage that links to the RRR.py file.
index.php - A simple page with only a form
control.php - A file uses GET to retrieve variables and builds a command for execution.

When running an example command of

Code: Select all

python3 RRR.py -c ON -r 5
, it properly takes control of the 5th devices mapped pins.

I can get the same command by placing the following URL -> '[ip]/control.php?com=ON&dev=5'
A PHP echo shows me the $command var holds the following

Code: Select all

python3 RRR.py -c ON -r 5
I use PHP to execute this command using the following ->

Code: Select all

$output = shell_exec($command);
and then view the output by using ->

Code: Select all

echo "<pre>$output</pre>"
I get the following output:

Code: Select all

Sending command (ON) to device (5)
Performing operation. This will take 1 second(s)...
Please wait...
This is when the GPIO pins are means to be controlled from the following lines in the RRR.py file:

Code: Select all

 GPIO.setup(devPin, GPIO.OUT)
 GPIO.output(devPin, GPIO.LOW)
 time.sleep(sleepTime)
 GPIO.output(devPin, GPIO.HIGH)
Where devPin is a GPIO pin number that has been worked out from the config.ini. Dont worry about this part, it works fine. The pin numbers are still correct when being ran from PHP. The GPIO commands simply don't work.

This leads me to believe that the problem lies fully with the very first line of the file, however, no error is generated. The first line being:

Code: Select all

import RPi.GPIO as GPIO
Maybe PHP's shell_exec doesn't have the correct perms to use the GPi.GPIO python module? Anyone know how I would fix this?

tl;dr : Everything works FINE from the command line, but totally breaks when the exact same commands are called from PHP.
Coding mad.

james3126
Posts: 5
Joined: Wed Oct 25, 2017 2:05 pm

Re: GPIO control lost (without error) when running script from PHP.

Sun Mar 04, 2018 10:03 pm

No worries! I have the answer now.

For anyone else struggling, someone wrote this answer to me:

"This doesn't work, because the user that runs your python script (www-data) is not allowed to write to this device:

To write there you need to be root or in the gpio group.
The user www-data is neither root nor in the gpio group.
In order to access gpio ports, you need to put the www-data user in the correct group:

Code: Select all

sudo usermod -aG gpio www-data
Don't forget to restart your http server (and php-fpm if applicable)"
Coding mad.

Return to “Other programming languages”

Who is online

Users browsing this forum: No registered users and 1 guest