RPi.GPIO Segmentation Fault (segfault) if GPIO.remove_event_detect is used in callback
If using python GPIO module, you’ll get Segfaults when calling the GPIO.remove_event_detect in the callback.
To avoid this you can use this workaround:
import time
from time import mktime, time, sleep
import RPi.GPIO as GPIO
import requests
def button_press_isr1(channel):
#Do not use >> SEGFAULT
#GPIO.remove_event_detect(18)
#Instead use a variable to set the callback on ignore
if ignoreCalls==1:
return
ignoreCalls=1
cnt = 0
edge_start1 = time()
while (time() - edge_start1) <= 0.035 :
if GPIO.input(18) == 0 :
cnt += 1
else:
cnt = 0
if cnt == 7 :
break
if cnt == 7 :
response = requests.post(url="http://192.168.1.5:8080")
#and wherever you would activate the callback again, just remove the ignoreCalls switch
ignoreCalls=0
#GPIO.add_event_detect(18, GPIO.FALLING, callback=button_press_isr1)
def init():
#variable used as workaround for the SEGFAULT Problem
global ignoreCalls
ignoreCalls=0
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN)
GPIO.add_event_detect(18, GPIO.FALLING, callback=button_press_isr1)
def main():
try:
while True:
sleep(0.03)
except KeyboardInterrupt:
# exit with CTRL+C
print("CTRL+C used to end Program")
finally:
GPIO.cleanup()
if __name__ == "__main__":
init()
main()