I used the code in this thread but removed things I didn't need. Here is the code I use
Code: Select all
#!/usr/bin/python
import time, commands, os
from time import sleep
import RPi.GPIO as GPIO
from subprocess import Popen
GPIO.setmode(GPIO.BOARD)
gpio_button = 5
GPIO.setup(gpio_button,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(gpio_button,GPIO.FALLING)
GPIO.setwarnings(False)
def triplePress():
print ("triple press")
def doublePress():
print ("double press")
devnull = open(os.devnull, 'wb')
Popen(['nohup', '/home/pi/actionB.sh'], stdout=devnull, stderr=devnull)
def singlePress():
print ("single press")
devnull = open(os.devnull, 'wb')
Popen(['nohup', '/home/pi/actionA.sh'], stdout=devnull, stderr=devnull)
def longPress():
print ("long press")
while True:
if GPIO.event_detected(gpio_button):
GPIO.remove_event_detect(gpio_button)
now = time.time()
count = 1
GPIO.add_event_detect(gpio_button,GPIO.RISING)
while time.time() < now + 1:
if GPIO.event_detected(gpio_button):
count +=1
time.sleep(.25) # debounce time
if count == 1:
longPress()
elif count == 2:
singlePress()
elif count == 3:
doublePress()
elif count == 4:
triplePress()
GPIO.remove_event_detect(gpio_button)
GPIO.add_event_detect(gpio_button,GPIO.FALLING)
But there is one problem: when I turn the flourescent ceiling light in the next room on and off the actions are also triggered! That ceiling light is not wired to the Pi. There must be some sort of interference going on. If I shut down the python script that controls the buttons everything else on the Pi runs fine without any interference from the ceiling lamp. So there is something with this specific script and wiring that causes the issue.
I tried to post yesterday but my first post never went through. Since then I've googled and read a lot of posts about using long wires to buttons in raspi. Many say that when the switch is open long wires can become a kind of antenna that pick up interference and make the input pin go back and forth between 1 and 0 (the term is that it "floats"). That would explain why starting the lamp triggers the script actions.
From searching I've seen these suggested solutions: add a resistor before the switch; add an external "pull up" resistor between input and 3V; I switched from another gpio pin to pin 5 since some said it differs by having a forced pull up resistor (but others say any gpio pin should work when the pin's internal resistor is set to pull up like I do in the code above); add a capacitor between input pin and 3V; and some much more complex suggestions.
I have tried these wirings (I hope the illustration is understandable):
1) input pin5 --- (2 meter wire) --- switch --- (2 meter wire) --- pi ground
2) input pin5 --- 10K resistor --- switch --- ground
3) input pin5 --- 1K resistor --- switch --- ground
4) input pin5 --- branch1: switch --- ground
--- branch2: 10K resistor --- 3V
5) input pin5 --- branch1: switch --- ground
--- branch2: 1K resistor --- 3V
In test 1, 3, 4 and 5 I have the same problem. The button works but turning on the lamp sometimes also triggers the script.
In case 2 nothing happened, neither on button press nor when the lamp is switched on.
I don't have any capacitors so haven't tried that yet. I've spend over 4 hours searching and testing. Please help me solve this problem. If capacitor is the solution please describe in detail what capacitor to use and where and in what direction to add it.
