I have spent two days on this and have tried nearly everything I could find. I am using an apache server (2.4.10).
I have two files in a folder called testPythonPHP
that is located in the root folder of my server (/var/www/html
). The files are callPythonFromPHP.php
and pythonReturn.py
. I call the python script from the PHP script using the exec command. Everything works fine before I try to use the RPi.GPIO
module in the python script. Before using the RPi.GPIO
module, I get the desired output.
Here is the PHP code:
<?php
exec(" python /var/www/html/testPythonPHP/pythonReturn.py" , $status,
$return );
echo( $status[0] ." is status and ".$return." is return and user is ");
echo exec( "whoami");
?>
Here is my python code:
import RPi.GPIO as GPIO
import time
butPin = 25
ledPin = 24
GPIO.setmode (GPIO.BCM)
#GPIO.setup (ledPin, GPIO.OUT)
#GPIO.setup (butPin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
#GPIO.output (ledPin, GPIO.LOW)
def bullspit():
cat = "3"
dog = "4"
catDog = cat + dog
GPIO.cleanup()
return catDog
if __name__ == '__main__':
print bullspit()
Here is the output to the browser:
34 is status and 0 is return and user is www-data
Once I un-comment any of the code in the python script, my output becomes:
is status and 1 is return and user is www-data
I no longer get the returned value from the python script and I now get the 1 return status (indicating something went wrong). I have tried changing permissions and using different commands (not that I can guarantee those were done correctly).
I feel like I am on the right track but cannot get this to work. Does anyone know what my mistake is or if there is another GPIO module I can use that will work with python and read analog values?
My goal is to use python to read in an analog value from a MCP3008 chip and return the value to the PHP script where I will dictate different GPIO pins to write high or low depending on the returned value. I tested a variation of the python file in the terminal and was able to print different return values based on an input button being high or low.
I would settle for reading in analog values from the PHP script, but it still seems like a permission issue and I will likely run into the same problem.
php
at all? Why not write all of it in python?python pythonReturn.py
)34
. I am usingphp
because I am modifying this instructable form TheFreeElectron: [instructables.com/id/…. It usesphp
and javascript to update picture on the browser. However, instead of changing the picture, and toggling the state of the GPIO pin, each time the picture is clicked, I want to change the picture, and GPIO state, based on the analog value I read in. I also tried running python code directly (without path) and it ran just the same.php
is helpful except I do not want to use the PWM, although I will put that in my back pocket for later. I simply want to read in the analog value and set other pins high or low based on a value threshold. I planned on tackling this route next with the help of this: [packagist.org/packages/tgeindre/php-gpio]. However, I really want to get the call fromphp
to python to work properly as this could prove useful on other projects down the road too. Which one is the "better" approach is something I am unsure of.