Backwards

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 -*-
token="TOKEN"
testonoti="The button is pressed."
pin="18"

import RPi.GPIO as GPIO
import time
import requests


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


while True:
    inputValue = GPIO.input(pin)
    if (inputValue == False):
        requests.get('https://api.rpinotify.it/existencerequest/{}/text/{}'.format(token, testonoti))
    time.sleep(0.3)

						

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.

https://api.rpinotify.it/notification/$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 buttonpressed.py in crontab.



Dove buttonpressed.py 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 buttonpressed.py

RpiNotify is an Italian project supported by RaspberryItaly

The first Italian RaspberryPi community.