andies
Posts: 141
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Interrupt programming with python

Tue Oct 14, 2014 11:22 am

Sorry for the stupid question but reading all those posts I was not able to answer my question. I have installed a GPIO solution where the door bell is connected (wired) with the GPIO of my raspberry. If someone rings the bell my raspberry has a UP-state in GPIO 17. So far, so good.

I want to use this signal so that my raspberry can send emails etc pp. To this end I also wrote a python program using interrupts. Now my question: This program uses

Code: Select all

GPIO.add_event_detect(17, GPIO.RISING, callback=klingel_callback, bouncetime=2000)
and I do not know how I can "activate" this python program. If I am running it from terminal, is it necessary that I include a

Code: Select all

try:  
	while True:
  		time.sleep(2)
so the program will run forever? Also, since I do not want to start it manually forever: Can I put the program in rc.local (I need sudo for the actions that come from the callback)? Any hints are appreciated.
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

DirkS
Posts: 10365
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Interrupt programming with python

Tue Oct 14, 2014 2:56 pm

Yes, you need to keep the program alive yourself. So adding something like

Code: Select all

while True:
      sleep(3600)
will keep it alive.
Putting it in rc.local (before the line 'exit 0') is one way of starting it automatically

Gr.
Dirk.

Return to “Python”