Hi,
Some time ago I discovered that my programme randomly stopped receiving interrupt from GPIOs. After investigation, it turns out that a login (user pi) under raspbian resets the GPIO devices directory and RPi.GPIO's "epoll" function doesn't cope well with this situation.
example:
import time
import RPi.GPIO as GPIO
def event_handler(pin):
print('Event on PIN', pin)
GPIO.setmode(GPIO.BCM)
PIN = 9
GPIO.setup(PIN, GPIO.IN)
GPIO.add_event_detect(PIN, GPIO.BOTH, event_handler)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
works well until one tries to ssh to the Raspberry Pi or executes a
sudo su -
su - pi
At this point the handler won't be called ever again...
The only workaround I found so far is something along the lines of:
import os
import time
import RPi.GPIO as GPIO
GPIO_DEV = '/sys/devices/virtual/gpio/gpio%u'
def event_handler(pin):
print('Event on PIN', pin)
GPIO.setmode(GPIO.BCM)
PIN = 9
GPIO.setup(PIN, GPIO.IN)
GPIO.add_event_detect(PIN, GPIO.BOTH, event_handler)
pin_export_time = time.ctime(os.path.getmtime(GPIO_DEV % PIN))
try:
while True:
if pin_export_time != time.ctime(os.path.getmtime(GPIO_DEV % PIN)):
print('Reset !!!')
GPIO.remove_event_detect(PIN)
GPIO.add_event_detect(PIN, GPIO.BOTH, event_handler)
try:
pin_export_time = time.ctime(os.path.getmtime(GPIO_DEV % PIN))
except Exception:
pin_export_time = -1
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
Not that this behaviour doesn't seem to happen on ArchLinux.
1) What makes the login under "pi" touching the /sys/devices/virtual/gpio/ ?
(couldn't find anything under /etc/profiles, .bashrc, etc...)
2) Is there a possibility to include the detection & reset directly in the RPi.GPIO code?
Regards,
François GUILLIER
Possibly linked to issue 60 - sending task to the background using Ctrl-Z then bg has the same symptoms.
Possibly related to this:
https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=123305&sid=1cd26b5c521932440c0f31fdab9c266b#p829698