2

I want my RaspberryPi to perform some actions when the light in a room is switched on or off. To do so, I connected a photocell to the GPIO pins.

Previously, I used some python 3 program that queried the GPIO pin in a loop. The code looks similar to this:

import wiringpi2 as wiringpi
import time
import subprocess

subprocess.call(["gpio", "export", "24", "in"])
wiringpi.wiringPiSetupSys()

last_status = -1

while True:
    current_status = wiringpi.digitalRead(24)
    if last_status != current_status:
        last_status = current_status
        print(time.strftime("%H:%M:%S"), " -- ", current_status)
    time.sleep(1)

This works somehow, but it has some drawbacks, as the code is neither very precise nor resource-friendly.

I think using an interrupt instead of a loop might be superior in every respect. So I tried something like this:

import wiringpi2 as wiringpi
import time

wiringpi.wiringPiSetupSys()

def print_status():
    current_status = wiringpi.digitalRead(24)
    print(time.strftime("%H:%M:%S"), " -- ", current_status)

def start_interrupt():
    wiringpi.wiringPiISR(24, 2,  print_status()) # also tried "wiringpi.INT_EDGE_BOTH" instead of "2" but got the error AttributeError: 'module' object has no attribute 'INT_EDGE_BOTH'

x = wiringpi.piThreadCreate(start_interrupt())
if x != 0:
    print("it didn't start")

However, this doesn't work. I tried to find some examples on how to use WiringPi's interrupt-functionality but I failed - I only found examples for C not for Python (3).

Can you help me to get it right?

I have two questions:

  • What is the right way to "translate" the loop-solution into an interrupt solution in Python (3)?
  • Is there a way to use INT_EDGE_BOTH instead of some integer in Python? This would make the code more readable.
4

Interrupts are not supported in the python version of wiringPi2 because it's a wrapper of the original wiringPi C library. To quote the publisher of the module in issue 8 of it's github:

... I will have to look at implementing Python-wrapped interrupts in a different manner. At the moment your only choice is to set up a polling loop. Sorry!

Try using RPI.GPIO if you want a python module with interrupt support. I think it comes with the standard Raspbian image as well, here's the Inputs page that has the interrupt function.

If you still want to use wiringPi the solution would be as you have already done, a while loop that pools constantly the desired pin then breaks out when it changes.

wiringPi-python doesn't define the constants like INPUT, OUTPUT and INT_EDGE_BOTH, you will need to define them beforehand referring to them. Those constants are defined in the C library.

Good luck!

  • that's bad news... however another solution could be to code this part of the program in C... – speendo Jun 28 '15 at 10:01
  • Can you use RPI.GPIO for interrupts alongside using wiringPi2 for other functions at the same time or will they fight? – AutomatedMike Nov 4 '15 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.