Page 1 of 1

Start sh script from GPIO button

Posted: Sat Apr 18, 2015 5:34 pm
by davidemol
Hi,
I'm new in this community.
There is anybody can help me how to start sh script when a GPIO circuit is close (or push switch button connected to GPIO).
Thanks!
Davide

Re: Start sh script from GPIO button

Posted: Sat Apr 18, 2015 9:17 pm
by MarkHaysHarris777
hi Davide, there are a couple of ways to run a routine (script, whatever) from a sensor or push button event. You always need to decide in your design whether you want 'push' or 'pull'.... do you want to poll the device to 'see' if the button is pushed, or do you want an interrupt callback to handle the event of pushing the button without you having to check for it deliberately every so many seconds... poll vs interrupt.

You can attach an interrupt to a gpio pin which you set to detect on a rising or falling edge (can be either or both, high or low) and then you set an interrupt handler (its sometimes called a callback) to run when the event triggers the interrupt. Otherwise, you will need to 'read' the gpio pin you are interested in every so often (every thirty seconds, or whatever) and then 'do something' if the switch is closed (whatever that means) I usually 'pullup' my gpio pins with a resistor (they are normally high) and I check to see if they are LOW; the push botton grounds the input usually on my designs.

I am a big fan of interrupts (but they are an advanced topic, and they are tricky). Typically interrupts do not work well from a push button because of contact bounce. if you wan the switch to trigger the interrupt you will need to condition the switch (there are many ways to do this) so that the contact bounce is eliminated. You might use a resistor cap combo with a diode, or you might use a 555 timer one-shot , or you might use a flip-flop debouncer... its up to you; READ about it on-line and try to decide yourself.

Re: Start sh script from GPIO button

Posted: Sun Apr 19, 2015 9:11 am
by craigmidlands
This is similar to what i'm trying to do and wondered if a callback function was the correct way to handle it.

However, to complicate things, i'm doing it wirelessly using a ciseco WIK.

What i'm attempting to do is in two parts:
1. poll the xino board periodically to see if it is still there, this part pretty simple. Write to serial port and read response if any.

2. If a button (alarm state) is pressed on the xino pin D02 or D03 which are interrupts, the rpi needs to execute a set of commands (print is ok for now.)

if i try to set up an IF statement it just seems to hang there and doesn't read any other code in the script. From this, pretty much as you say, handling an interrupt is not that simple.

In summary, how do you suggest i best scan for the interrupt on the xino via the rpi using python?

Re: Start sh script from GPIO button

Posted: Mon Apr 20, 2015 5:23 pm
by davidemol
Ciao Marcus,
thanks for your very clear reply.

I try to explain well my easy home project...

I installed a micro USB camera in my home entryphone.
I intercepted the ring button and brought the USB cable and and ring cable to raspberry.
I make a script to shoot a picture and send it by mail.
I miss the last step now: I need to start script when the ring button will be pressed, that is when the DC circuit is close.

Thanks again!
Davide

Re: Start sh script from GPIO button

Posted: Mon Apr 20, 2015 8:28 pm
by joan
davidemol wrote:Ciao Marcus,
thanks for your very clear reply.

I try to explain well my easy home project...

I installed a micro USB camera in my home entryphone.
I intercepted the ring button and brought the USB cable and and ring cable to raspberry.
I make a script to shoot a picture and send it by mail.
I miss the last step now: I need to start script when the ring button will be pressed, that is when the DC circuit is close.

Thanks again!
Davide
There are dozens of ways of doing what you want. Search for switch on the forum.

Have you connected the switch? Two ways.
  • One end of the switch connected to Pi ground through a 1k resistor. The other end of the switch to a Pi gpio. Enable the Pi's internal pull-up to 3V3. Then the gpio will read 1 when the switch is open and will read 0 when the switch is closed.
  • One end of the switch connected to Pi 3V3 through a 1k resistor. The other end of the switch to a Pi gpio. Enable the Pi's internal pull-down to ground. Then the gpio will read 0 when the switch is open and will read 1 when the switch is closed.
There are plenty of examples of using Python to read and respond to switches. Just search the forum.