0

I am trying to use php to execute a python script, and then retrieve the output of that script for later use. I am running a LAMP server on a Raspberry Pi 4 (set up according to this article https://randomnerdtutorials.com/raspberry-pi-apache-mysql-php-lamp-server/) and I am trying to display information about the GPIO to the web server, and then later move it into the SQL database. I would like to use the gpiozero library for reading inputs from the GPIO on the Raspberry Pi because it is an easy and well-documented library, and I have had issues trying to use any of the other ones that I've seen online. The problem is that whenever I use the exec() or shell_exec() functions to try and execute the python script that I have to read the GPIO, nothing is returned to the php webpage.

This is my code from the php file:

<?php
    // Check raspberry pi GPIO
    $command = escapeshellcmd('/bin/python /var/www/html/check.py');
    echo $output = shell_exec($command);
?>

and this is my code from the py file called 'check.py':

from gpiozero import Button 

button = Button(4)

if button.is_pressed:
    print("Pressed")
print("Hello World")

I have been able to run this python script from the terminal, and it executes properly and returns 'Hello World' because there is no input present, but the php page on the web server remains blank. However, when I change the py script to just print out 'Hello World' like this:

print("Hello World")

The text is successfully returned by the php script and displayed on the web server by the echo function. This leads me to believe that the issues lies with using an object from a library, but I cannot be sure.

I tried to switch over to using only the command line and removing python from the equation, but that did not seem to work. I saw some other people from years ago were able to run commands like

$ gpio all
$ gpio read 1
$ gpio help

but none of these commands worked for me and I would receive a response from the terminal saying "bash: gpio: command not found". I looked around for some other people who had this same issue (again from older stack overflow posts) and I tried uninstalling and then reinstalling this library called wiringpi, but there was nothing to uninstall and when I went to reinstall it there was nothing found. I tried installing some other things that I saw, but that didn't change anything for me either. So then I switched routes and went back to python again, but then I tried using a different library and I tried using the RPi GPIO library, but that gave me the same issues as the gpiozero library. I started to think that maybe it might have to do with the permissions of the folder holding the python libraries, so I looked into changing the permissions of those folders, but since I didn't know too much about how to do that and the chmod command, I decided to not do anything hastily.

EDIT: So I did just set up the php debugger, and I was able to narrow my issues down a little bit more. The code that I am now running in my php file called 'check.php' is this:

<?php
// Check raspberry pi GPIO
$command = escapeshellcmd('/bin/python /var/www/html/check.py');
$output = null;
$result = null;

exec($command, $output, $result);

$element = null;
foreach( $output as $element ) {
    echo $element . '<br>';
}

$isnull = is_null($output[1]);
echo $isnull . '<br>';

$serial = serialize($output[1]);
echo $serial;
echo $result . '<br>';
?>

The python script called 'check.py' now says:

from gpiozero import Button 

print ("Hello World 1")

button = Button(4)

if button.is_pressed:
    print("Pressed")
print ("Hello World 2")

This code works perfectly when I step through it using the debugger: Hello World 1 and Hello World 2 display. However, when I pull up the check.php page in a web browser through the server, only the "Hello World 1" displays and it says that the second value of the array is actually null. So basically, in the debugger the second value in the array is 'Hello World 2' and in the browser it is null. The problem still exists because I don't want it to be null in the browser, I want it to give me the same results that I am getting through the vscode IDE using the php debugger.

EDIT 2: I found that I was also able to run the php script from terminal with no issues, so I began to believe that it was definitely a permissions issue. After a long time of searching, I finally found a case that looked pretty similar to mine and the solution that user found ended up working for me as well. I'm not sure if it is the best practice, but I accessed visudo from the command line and added

www-data ALL=(ALL)    NOPASSWD: ALL

to the very bottom of the file. I also changed my $command variable to include 'sudo' at the front to run it with root user privileges. I get the feeling that this allows for security issues, but I am only using the python file to return information about the Raspberry Pi's GPIO. There is no human input entering that script, so it might be alright. If anyone has any suggestions please let me know. I guess I would need to somehow give my webserver access to the python gpiozero library files in this case, but I am not completely sure how I would do that. I've looked into chown and chmod, but I'm not sure if that would work or how I would do it. Would I just need to do it for the gpiozero library folders? or would I have to change things for each folder above that as well?

feddockh
  • 1
  • 2
  • 1
    AFAIK, Python modules must be downloaded for the specific user if not globally installed - this is worth checking that `www-data` can use them modules. Also, "it remains blank" is not descriptive. [Read the return types in the documentation](https://www.php.net/manual/en/function.shell-exec.php#refsect1-function.shell-exec-returnvalues) and do some debug on what errors you're getting. You can capture them if you read the docs and further debug. – Jaquarh Oct 25 '22 at 21:40
  • @Jaquarh I looked into that and made an edit to my original question, thank you for your feedback. – feddockh Oct 26 '22 at 15:25
  • Yeah, I would probably use `venv` to create a Python environment rather than allow `www-data` sudo access – Jaquarh Oct 27 '22 at 20:23

0 Answers0