devnull
Posts: 65
Joined: Sat Dec 24, 2011 7:46 am

Interrupts

Sun Jan 06, 2013 2:00 am

Hi all;

I am trying to get the wiringPi interrupts to work using gadgetoid's port of wiring pi(git clone https://github.com/Gadgetoid/WiringPi-Python.git), the function is: waitForInterrupt:

Code: Select all

>>> dir(wiringpi)
['GPIO', 'HIGH', 'INPUT', 'LOW', 'LSBFIRST', 'MODE_GPIO', 'MODE_PINS', 'MODE_SYS', 'MSBFIRST', 'NES', 'OUTPUT', 'PUD_DOWN', 'PUD_OFF', 'PUD_UP', 'PWM_OUTPUT', 'Pin', 'Serial', 'Shift', 'WPI_MODE_GPIO', 'WPI_MODE_PINS', 'WPI_MODE_SYS', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_newclass', '_object', '_swig_getattr', '_swig_property', '_swig_repr', '_swig_setattr', '_swig_setattr_nondynamic', '_wiringpi', 'delay', 'delayMicroseconds', 'digitalRead', 'digitalWrite', 'gertboardAnalogRead', 'gertboardAnalogWrite', 'gertboardSPISetup', 'millis', 'pinMode', 'pullUpDnControl', 'pwmSetClock', 'pwmSetMode', 'pwmSetRange', 'pwmWrite', 'readNesJoystick', 'serialClose', 'serialDataAvail', 'serialGetchar', 'serialOpen', 'serialPrintf', 'serialPutchar', 'serialPuts', 'setupNesJoystick', 'shiftIn', 'shiftOut', 'softPwmCreate', 'softPwmWrite', 'waitForInterrupt', 'wiringPiSPIDataRW', 'wiringPiSPIGetFd', 'wiringPiSPISetup', 'wiringPiSetup', 'wiringPiSetupGpio', 'wiringPiSetupSys']
I have a microswitch attached to pin 12 (GPIO18) and using digitalRead I can read the port:

Code: Select all

wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(18,0)
>>> wiringpi.digitalRead(18)
1
However waitForInterrupt ALWAYS returns 0 and never appears to detect the rising edge when the microswitch is closed:

Code: Select all

wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(18,0)
>>> wiringpi.waitForInterrupt (1,5000)

0
>>> 
I would expect to be able to set the interrupt edge something like this:

Code: Select all

wiringpi.pinEdge(0,falling)
however I cannot find any way to do that with the current implementation and suspect that this may be the problem.

Any suggestions ???
> /dev/null 2>&1

texy
Forum Moderator
Forum Moderator
Posts: 5092
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Interrupts

Sun Jan 06, 2013 10:07 am

You got an extra space before the (1,5000) ?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4247
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Interrupts

Sun Jan 06, 2013 11:02 am

Python doesn't mind that extra space; it isn't the problem.

User avatar
jojopi
Posts: 3041
Joined: Tue Oct 11, 2011 8:38 pm

Re: Interrupts

Sun Jan 06, 2013 11:36 am

devnull wrote:wiringpi.pinEdge(0,falling)
however I cannot find any way to do that with the current implementation and suspect that this may be the problem.
That is one problem. And another is that when you call wiringPiSetupGpio() rather than wiringPiSetupSys(), the /sys/class/gpio/gpioN/value files are never opened, the file descriptors are left uninitialised, and waitForInterrupt() then mistakenly blocks on stdin.

As a nasty workaround, setting the edge detection via /sys and then calling both setup functions appears to work:

Code: Select all

import os
import wiringpi

os.system("echo 7 >/sys/class/gpio/export");
os.system("echo rising >/sys/class/gpio/gpio7/edge");

wiringpi.wiringPiSetupSys()
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(7,0)
while True:
    print wiringpi.waitForInterrupt(7,5000)
That is using BCM2835 GPIO7.

User avatar
jojopi
Posts: 3041
Joined: Tue Oct 11, 2011 8:38 pm

Re: Interrupts wiringPi

Mon Jan 07, 2013 12:16 pm

jojopi wrote:and then calling both setup functions
I was confused here.

wiringPiSetupSys() is the correct, single, function to call if you want to use Broadcom GPIO numbers and the kernel /sys interface, which is required for interrupts.

It is not necessary to call wiringPiSetupGpio() as well, unless you specifically want to use the faster register access for everything except interrupts. From python the difference is not likely to be noticeable, and since the Gpio() /dev/mem interface requires much higher privileges, Sys() should probably always be preferred.

Nevertheless it is a bug that the library tries to use the /sys interface for interrupts in Gpio() mode, but it has not opened the files.

wiringPiSetup() is the function that uses nutty pin numbering and must be avoided.

devnull
Posts: 65
Joined: Sat Dec 24, 2011 7:46 am

Re: Interrupts

Wed Jan 30, 2013 3:46 am

@jojopi - Thanks and my apologies for the delay, I thought that I had subscribed to email alerts when a thread was updated but it appears not and I thought nobody had responded.

I will try this out later.

Many thanks

Pete
> /dev/null 2>&1

User avatar
moallen
Posts: 36
Joined: Fri Dec 28, 2012 6:37 pm
Contact: Website

Re: Interrupts

Mon Feb 04, 2013 4:17 pm

I too have been trying to figure out how to use interrupts in wiringPi with Python, but don't understand C well enough to utilize Gordon's examples to use in Python.

Anyway in trying jojopi's script above, I am getting this error:
Traceback (most recent call last):
File "wiringpi-motion-test-3.py", line 11, in <module>
print wiringpi.waitForInterrupt(24,5000)
AttributeError: 'module' object has no attribute 'waitForInterrupt

It's probably something simple I'm not doing, but any suggestions? wiringPi should be up to date on my RPi.

My code:

import os
import wiringpi

os.system("echo 7 >/sys/class/gpio/export");
os.system("echo rising >/sys/class/gpio/gpio7/edge");

wiringpi.wiringPiSetupSys()
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(24,0)
while True:
print wiringpi.waitForInterrupt(24,5000)
http://allenmlab.blogspot.com/

User avatar
moallen
Posts: 36
Joined: Fri Dec 28, 2012 6:37 pm
Contact: Website

Re: Interrupts

Mon Feb 04, 2013 8:56 pm

Still no luck getting interrupts to work with wiringPI in Python. I did find the following example referenced in the Nov. MagPi, and it does work for me. It doesn’t use wiringPi or RPi.GPIO. It sets up a polling object to watch for interrupts. It runs under Python3. I'm using a Parallax motion sensor connected to GPIO24 to trigger an interrupt. I do need to understand this polling concept better in order to incorporate it into my own code, though.
http://ryniker.ods.org/raspberrypi/MagP ... _test23.py
http://allenmlab.blogspot.com/

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: Interrupts

Wed Feb 06, 2013 2:14 pm

I think you might have more luck with a simpler Python module, as WiringPi does a lot of things and is quite complex. I've just written a really slim module to handle interrupts with epoll for my own needs: https://github.com/metachris/raspberrypi-gpio-utils.

There is also an extremely simple example of the low-level functionality in https://github.com/metachris/raspberryp ... ernal_doc/. It should definitely work; and I'd be glad to get some feedback!
pythonhosted.org/RPIO

devnull
Posts: 65
Joined: Sat Dec 24, 2011 7:46 am

Re: Interrupts

Sat Feb 23, 2013 12:11 pm

moallen wrote:Anyway in trying jojopi's script above, I am getting this error:
Traceback (most recent call last):
File "wiringpi-motion-test-3.py", line 11, in <module>
print wiringpi.waitForInterrupt(24,5000)
AttributeError: 'module' object has no attribute 'waitForInterrupt
You need to install it from here: https://github.com/Gadgetoid/WiringPi-Python.git

After going through the normal instal method, run python and execute the following:

dir(wiringpi)

and you should see the waitForInterrupt() function listed.

Cheers

PeterC
> /dev/null 2>&1

devnull
Posts: 65
Joined: Sat Dec 24, 2011 7:46 am

Re: Interrupts

Sat Feb 23, 2013 12:13 pm

OK, I have the interrupt working, however I have discovered that if you have more than one interrupt setup, they all appear to fire when any one of them fires.

Can someone verify this ??

Thanks

PeterC
> /dev/null 2>&1

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: Interrupts

Sat Feb 23, 2013 4:08 pm

Have you tried it with RPIO, my extension of RPi.GPIO with interrupt handling? (its now at https://github.com/metachris/RPIO), and the previous link won't work anymore.) In my tests with RPIO I can wait for interrupts on any number of GPIOs and they work independently. All the necessary code is this:

Code: Select all

import RPIO

def do_something(gpio_id, value):
    logging.info("New value for GPIO %s: %s" % (gpio_id, value))

RPIO.add_interrupt_callback(7, do_something, edge='rising')
RPIO.add_interrupt_callback(8, do_something, edge='falling')
RPIO.add_interrupt_callback(9, do_something, edge='both')
RPIO.wait_for_interrupts()
pythonhosted.org/RPIO

devnull
Posts: 65
Joined: Sat Dec 24, 2011 7:46 am

Re: Interrupts

Sun Feb 24, 2013 10:07 am

Have you tried it with RPIO, my extension of RPi.GPIO with interrupt handling?
Chris, I have installed your RPIO extensions and they work great, thanks.

Any plans to add more functions ?? - bit banging serial on any port would be a nice one, pwm is also useful.

PeterC
> /dev/null 2>&1

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: Interrupts

Sun Feb 24, 2013 11:24 am

I'm glad RPIO works well for what you want to do! And yeah, there are more features coming -- the module is under very active development and I'm open to suggestions and feature ideas. What I've just finished is software pullup and -down resistors for interrupts (part of the next update) and am taking a look into DMA driven PWM too (might take a bit of time). Thanks for the feedback.
pythonhosted.org/RPIO

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: Interrupts

Tue Feb 26, 2013 4:39 pm

Hello Chris,
just had a look at your package and I think it is just what I need. I have a question related to interrupt handling, but before that let me briefly explain what I am doing. My Raspi is a mixture of home automation and alarm system, which I control from my smartphone via TCP packets. What I need is to wait for TCP packets as well as for interrupts on certain GPIO pins, which are connected to various detectors (motion, smoke, sound, ...).
Your library would allow me to receive interrupts from GPIO pins. How would I integrate that with waiting for TCP packets ? For TCP, I am using epoll as well, but my (current) understanding is, that with your library, I only can wait for interrupts from GPIO pins. Is my understanding correct ?

Thank you !
Josh

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: Interrupts

Tue Feb 26, 2013 4:46 pm

Yes this is correct - you can't wait for socket packets with RPIO. But you can use epoll at many places concurrently; for instance you could run "RPIO.wait_for_interrupts()" in a Thread and wait for the socket packets with another epoll call. Or extend RPIO.py with generic socket support; should be easy.
pythonhosted.org/RPIO

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: Interrupts

Tue Feb 26, 2013 5:27 pm

Chris,
thank you for your fast response. I am relatively new to epoll, so waiting at various places with epoll is at the moment beyond me :-(
But I will have a look at RPIO.py, perhaps I can understand it a bit and include socket handling - I'll give it a try.

Thank you
Josh

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: Interrupts

Wed Feb 27, 2013 2:18 am

It was just too straight-forward not to do it... RPIO now supports TCP socket interrupts! :) It's very easy; one new command:

Code: Select all

RPIO.add_tcp_callback(port, callback[, threaded_callback=False])
You can find a full example and more information at http://pythonhosted.org/RPIO/#ref-rpio-py-tcpserver. I'll put it on pypi soon, for now you need to get the package from Github:

Code: Select all

$ cd /tmp
$ curl -L https://github.com/metachris/RPIO/archive/master.tar.gz | tar -xz
$ cd RPIO-master
$ sudo python setup.py install
I'd be glad to get feedback!
pythonhosted.org/RPIO

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: Interrupts

Wed Feb 27, 2013 6:51 am

:D :D :D
Thank you so much, Chris !!!!
Definitely I will check and try it and give a feedback !
I guess I can now throw parts of my pretty lame python code away :)

Again, thank you for your very valueable help !

Have a nice day
Josh

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: Interrupts

Wed Feb 27, 2013 2:09 pm

Chris,
may I ask a (possibly stupid) question: what is the difference in behaviour of threaded_callback=false and threaded_callback=true ?
My knowledge of interrupt processing is based on programming micro-controllers, where you have direct access to and influence on everything.
I believe that for my purposes (just waiting for something to happen, then deal with it, then wait again) requires threaded_callback=false, but what it really means is beyond me.

Sorry for my silly questions. I googled threaded callback, but the results did not enlighten me ...

Thank you
Josh

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: Interrupts

Wed Feb 27, 2013 2:48 pm

Not a stupid question at all. If you set the threaded_callback parameter to True, your callback will be started inside a Thread. Thread is the keyword you are looking for. Each program has one main thread, and all other threads run concurrently, with the operating system scheduling the timeslots for all of them.

In short, if you using a callback without a thread and do something which blocks the system (eg. time.sleep()), no other interrupts will be received until your callback finishes. If your callback is run in a Thread, both work at the same time.
pythonhosted.org/RPIO

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: Interrupts

Wed Feb 27, 2013 7:10 pm

Thank you for trying to shed some light in my darkness, but now you have lost me completely. Up to now, I thought that a process that calls time.sleep() behaves well and busy-wait ist the bad guy who blocks the system ...
Anyway, my main issue was to be able to have a central point to wait for a number of different events and then deal with them when the occur - sequentially, not in parallel. I think that your work is providing me with such a possibility.
I will try that and be back to you - either with success or more silly questions.
Thank you
Josh

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: Interrupts

Wed Feb 27, 2013 9:04 pm

Blocking (eg. with sleep()) is not necessarily bad; it just blocks everything else in the same thread. A callback inside an extra thread can do whatever it wants while RPIO can return to waiting for interrupts.
pythonhosted.org/RPIO

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: Interrupts

Wed Feb 27, 2013 9:10 pm

Chris,
slowly but steadily ;) I beginn to understand ...
Now I am quite sure that I can scrap probavly about one third of my current code (not working) by using your RPIO.
Will need some time for testing, but I am very optimistic now.

Thanks a lot
Josh

Gadjetnut
Posts: 17
Joined: Fri Apr 05, 2013 9:13 pm

Re: Interrupts

Fri Apr 05, 2013 10:42 pm

If you are building an alarm system, please check out this website:

http://www.projects.privateeyepi.com/home

They have full instructions on how to create an alarm system with email alerts, web based dashboard to view and control you alarm. No fees.

http://www.privateeyepi.com/image/dashboard.jpg

Return to “Python”

Who is online

Users browsing this forum: No registered users and 9 guests