Ligne 41 : |
Ligne 41 : |
| | | |
| [[Fichier:PiButton_bb.jpg|800px]] | | [[Fichier:PiButton_bb.jpg|800px]] |
| + | |
| + | == Le script Python == |
| + | Pour créer facilement ce script sur votre python, nous vous proposons une méthode à base de copier/coller à l'aide des étapes suivantes à réaliser dans un terminal (ou une connexion SSH). |
| + | |
| + | === Copier/coller le code === |
| + | Voici une méthode Copier/Coller super simple<br /> |
| + | |
| + | On commence par demander la création du fichier |
| + | <nowiki>cat <<! > raspi-button.py</nowiki> |
| + | |
| + | On fait ensuite le copier/coller du code |
| + | |
| + | <nowiki>#!/usr/bin/env python |
| + | # -*- coding: latin-1 -*- |
| + | |
| + | import RPi.GPIO as GPIO, time |
| + | |
| + | GPIO.setmode(GPIO.BCM) |
| + | GREEN_LED = 18 |
| + | RED_LED = 23 |
| + | BUTTON1 = 24 |
| + | |
| + | GPIO.setup(GREEN_LED, GPIO.OUT) |
| + | GPIO.setup(RED_LED, GPIO.OUT) |
| + | GPIO.setup(BUTTON1, GPIO.IN) |
| + | |
| + | while True: |
| + | # Si le bouton est pressé, la broche GPIO est raccordée |
| + | # à la masse. Le GPIO est donc à LOW (bas). |
| + | # Bouton pressé -> Input = LOW = False |
| + | if( GPIO.input( BUTTON1 ) == False ): |
| + | GPIO.output(GREEN_LED, True) |
| + | GPIO.output(RED_LED, False) |
| + | else: |
| + | GPIO.output(GREEN_LED, False) |
| + | GPIO.output(RED_LED, True) |
| + | </nowiki> |
| + | |
| + | On termine l'opération de saisie simplement avec: |
| + | <nowiki>!</nowiki> |
| + | |
| + | === Rendre le fichier exécutable === |
| + | Par défaut, les fichier sont considéré comme des fichiers texte non exécutable... même s'il contiennent des scripts. |
| + | |
| + | Il faut donc indiquer au système d'exploitation qu'il peut autoriser l'exécution de notre raspi-blink.py |
| + | <nowiki>chmod +x raspi-button.py</nowiki> |
| + | |
| + | === Executer le programme === |
| + | Taper simplement la commande suivante: |
| + | <nowiki>sudo ./raspi-button.py</nowiki> |