theshark
Posts: 3
Joined: Sun Nov 01, 2015 3:26 pm

Rilevare contatto aperto-chiuso

Sun Nov 01, 2015 3:51 pm

Ciao a tutti, è da poco che sto cercando di capire come usare le GPIO del Raspberry. Vorrei collegare direttamente al raspberry un contatto NA-NC (switch a due fili per intenderci) e da python, dovrebbe risultare che alla pressione del tasto mi restituisce il messaggio Tasto Premuto, mentre al suo rilascio mi dica Pulsante Rilasciato. Ci sto sbattendo la testa :evil: ma non riesco a capire come fare, spero di essere stato chiaro nel descrivere il problema. Un saluto a tutti

morris1974
Posts: 25
Joined: Wed Jun 10, 2015 8:13 am

Re: Rilevare contatto aperto-chiuso

Sat Nov 07, 2015 10:11 am

Prova a dare un occhiata al thread precedente.
Sennò qui il link per quello che chiedi.
http://raspi.tv/2013/rpi-gpio-basics-4- ... and-inputs

theshark
Posts: 3
Joined: Sun Nov 01, 2015 3:26 pm

Re: Rilevare contatto aperto-chiuso

Sun Nov 08, 2015 3:00 pm

ho trovato e applicato questo codice, ma ho un problema: funziona se premo il pulsante... ma quando lo rilascio non succede nulla. Vorrei che quando rilascio si attiva un altro subprocess che lanci un altro programma sempre in python...

Code: Select all

    #-*- coding: UTF-8 -*

    import os
    import sys
    import time
    import datetime
    import subprocess


    import RPi.GPIO as GPIO


    def snap(channel):
     print "pulsante premuto"
     subprocess.call("python /home/registra.py", shell=True)


    GPIO.setmode(GPIO.BCM)

    button = 7

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

     
    '''
    button_last = GPIO.input(button)
    while True: 
      button_now = GPIO.input(button)
      if button_now != button_last and button_now == GPIO.LOW:
        snap()
      button_last = button_now
      time.sleep(0.01)

    '''

    GPIO.add_event_detect(button, GPIO.FALLING, callback=snap, bouncetime=300)

    while True:
      try:
        time.sleep(1.0)
      except KeyboardInterrupt:
        print("except..")
        break

      
    GPIO.remove_event_detect(button)

    GPIO.cleanup()

    print('end of script.')

Return to “Italiano”