I am trying to turn on a LED (using the RPi.GPIO python library) if the player enters the correct number on a webpage. I'm serving up a php file using apache (that bit is ok).
snippet from '/var/www/html/index.php'
echo exec("python3 /home/pi/myStuff/number.py $player_name $player_guess");
I tried running the number.py from terminal:
python3 number.py Paul 12345
and this works fine.
When I do it via the PHP then it exits the code and the PHP is returned "PRE SETUP" (see number.py below). The problem lies with the line "GPIO.setup(7,GPIO.OUT)". As far as can work out the required permissions to set the GPIO pins are incorrect, but I've tried chmod, chown and chgrp and all the combinations I have used so far haven't worked. (chown to www-data for apache, chgrp to root, chmod to 755, and a fair few more).
import sys import time import RPi.GPIO as GPIO number = sys.argv if number == "12345": print("PRE SET MODE") # added to help me debug GPIO.setmode(GPIO.BOARD) print("PRE SETUP") # debugging GPIO.setup(7,GPIO.OUT) print("SETUP OK") # debugging GPIO.output(7,1) print('You guessed correctly %s' % name) # desired output to PHP time.sleep(1) GPIO.output(7,0) GPIO.cleanup() else: print("Better luck next time") # output if you guess wrong
Can anyone point me in the right direction. I've tried using bash scripts (gpio mode 7 out, gpio write 7 1, ...) but this has the same problem. Any suggestions?