Key pressed

Miniguida - 03/01/2017

An example script notifying the user when a key outside the device is pressed.

RPi.GPIO e RaspberryPi are requested.

PYTHON script

#!/usr/bin/env python
# -*- coding: utf-8 -*-
testonoti="The button is pressed."

import RPi.GPIO as GPIO
import time
import requests

GPIO.setup(pin, GPIO.IN,pull_up_down=GPIO.PUD_UP)

while True:
    inputValue = GPIO.input(pin)
    if (inputValue == False):
        requests.get('{}/text/{}'.format(token, testonoti))


It works in a very simple way. Every time the script detects that the key is pressed a notification is sent.
Python was chosen to make things easier for RaspberryPy users

The variables of the script:

  • token Personal token assigned during the registration process.
  • testonoti Notification text
  • pin BCM pin number (qui per info).

The HTTP GET call structure is very simple.$token/text/$testonoti

Variable name Description Format
$token Personal token. Alphanumeric string.
$testonoti Notification text. Text without spaces (use %20)

Execute the script on startup

After the Raspberry initial boot, every 0.3 seconds the script checks the key status.

You can use cron, a very popular tool used to plan operations on linux systems.

For the execution of the script on startup insert the line @reboot python in crontab.

Dove is the position of the script in the device.

For the execution of the script authorizations are necessary. Authorizations can be given by entering chmod 755

RpiNotify is an Italian project supported by RaspberryItaly

The first Italian RaspberryPi community.