Call URL with keypress on Raspberry Pi

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:

Ultra simple schematic for this example

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:

  $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


# 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 = ""
# 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.")
  while 1:
    if GPIO.input(butPin): # button is released
    else: # button is pressed:
      os.system("wget -O/dev/null " + url + " &> /dev/null")
      while not GPIO.input(butPin):
  GPIO.cleanup() # cleanup all GPIO


now it’s time to run the script above with:

>>> python

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>
echo "<pre>".file_get_contents( "log.txt" )."</pre>";

One Comment

  1. Branez says:

    Thank You! I will use for another project.

Leave a Reply