Here is scenario:
pushbutton is connected to Raspberry Pi GPIO pin. Whet button is pressed, R.pi calls remote URL.
This can be useful for many simple applications, like detecting and timestamping passes at certain location.
The R.pi should be up and running. Connect the pushbutton to free GPIO. In our example we connected the button to pin 11. The pin is configured with the pullup, which means the button must pull the GPIO pin to ground. Pin 12 on r.Pi model 2B (40 pin P1) is GND:
Now prepare some simple script on your fancy web browser, which will be called when someone presses the button. Here is example, which simply adds one line with time and date at the end of the file “log.txt” every time when file is loaded:
<?php $date = date("d.m.Y;H:i:s"); file_put_contents('log.txt', "Ftic:".$date.$data.PHP_EOL, FILE_APPEND); ?>
Here is a concept of described principle:
On R.Pi create and copy following python code to file button.py:
# External modules import RPi.GPIO as GPIO import time import os # Key pin butPin = 17 # Broadcom pin 17 (Connector P1, pin 11) # URL which is called at keypress url = "http://pavlin.si/test/test.php" # Pin Setup: GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up print("Call url:" + url + "at keypress.") try: while 1: if GPIO.input(butPin): # button is released time.sleep(0.1) else: # button is pressed: print("Pressed") os.system("wget -O/dev/null " + url + " &> /dev/null") while not GPIO.input(butPin): time.sleep(0.1) except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly: GPIO.cleanup() # cleanup all GPIO
now it’s time to run the script above with:
>>> python button.py
short pins 11 and 12 on P1 and watch the file log.txt on server. For fancy printout use this in file index.php:
<!DOCTYPE html> <html> <head> </head> <body> <?php echo "<pre>".file_get_contents( "log.txt" )."</pre>"; ?> </body> </html>