With a little Python script I'm able to detect a button state that is connected in my Raspberry board. This is the script:
#!/usr/bin/env python
import os
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
try:
while True:
if(GPIO.input(12) == 1):
print 'Btn on'
os.system('date')
time.sleep(5)
else:
os.system('clear')
print 'Waiting'
time.sleep(2)
except KeyboardInterrupt:
GPIO.cleanup()
In this way, using PuTTY, or directly in the Raspberry terminal, I can easily detect the state of this button.
Now I would create the same thing but in PHP. I need to create a PHP web page that will be in the var/www/html
directory of my Raspberry, and should just show me the same thing. When I press the button, I will get a simple echo "Btn on" with the timestamp, and if not pressed just another echo with "Waiting".. Is it possible? I tried directly exec
the Python script, in PHP, in this way:
$command = escapeshellcmd('sudo -u www-data python btn.py');
$output = shell_exec($command);
echo $output;
but it's not working. Any idea?