1

I want to detect change in gpio input of raspberry pi and set handler using signal module of python. I am new to signal module and I can't understand how to use it. I am using this code now:

import RPi.GPIO as GPIO
import time
from datetime import datetime
import picamera
i=0
j=0
camera= picamera.PiCamera()

camera.resolution = (640, 480)

# handle the button event
def buttonEventHandler (pin):
    global j
    j+=1
    #camera.close()
    print "handling button event"
    print("pressed",str(datetime.now()))
    time.sleep(4)
    camera.capture( 'clicked%02d.jpg' %j )
    #camera.close()

def main():

      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(2,GPIO.IN,pull_up_down=GPIO.PUD_UP)    
      GPIO.add_event_detect(2,GPIO.FALLING)

      GPIO.add_event_callback(2,buttonEventHandler) 
     # RPIO.add_interrupt_callback(2,buttonEventHandler,falling,RPIO.PUD_UP,False,None)

      while True:
          global i
          print "Hello world! {0}".format(i)
          i=i+1
          time.sleep(5)

  #  if(GPIO.input(2)==GPIO.LOW):

     # GPIO.cleanup()

if __name__=="__main__":
    main()
slavoo
  • 5,798
  • 64
  • 37
  • 39
Himanshu mittal
  • 155
  • 5
  • 16
  • 1
    Why you are not suing GPIO module https://pypi.python.org/pypi/RPi.GPIO – Rahul K P Sep 12 '16 at 06:48
  • i am using GPIO module but i calls the handler function two time in one single interrupt..i think signal module works better..thats why i am thinking of using it... – Himanshu mittal Sep 12 '16 at 08:04

1 Answers1

0

I just changed code in a different manner tough you are free to implement same using SIGNAL module.You can start new thread and poll or register call back event their, by using following code and write whatever your functional logic in it's run() method.

import threading
import RPi.GPIO as GPIO
import time
import time
from datetime import datetime
import picamera
i=0
j=0
camera= picamera.PiCamera()

camera.resolution = (640, 480)
PIN = 2

class GPIOThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)              

    def run(self):
        while True:
            if GPIO.input(PIN) == False: # adjust this statement as per your pin status i.e HIGH/LOW
                global j
                j+=1
                #camera.close()
                print "handling button event"
                print("pressed",str(datetime.now()))
                time.sleep(4)
                camera.capture( 'clicked%02d.jpg' %j )

def main():

      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(PIN,GPIO.IN,pull_up_down=GPIO.PUD_UP)    
      GPIO.add_event_detect(PIN,GPIO.FALLING)

      gpio_thread = GPIOThread()
      gpio_thread.start() 

      while True:
          global i
          print "Hello world! {0}".format(i)
          i=i+1
          time.sleep(5)


if __name__=="__main__":
    main()

The above code will iterate until PIN input goes high, so once PIN goes high the condition in while loop inside run method breaks and picture is captured.

So, in order to call above thread do this.

gpio_thread = GPIOThread()
gpio_thread.start() 

this will call the thread constructor init and will initialize the variable inside constructor if any, and execute the run method.

You can also call join() method , to wait until thread completes it's execution.

gpio_thread.join()  

This always works for me, so Cheers!!

U.Swap
  • 1,921
  • 4
  • 23
  • 41