Shutdown Raspberry via GPIO
Hi,
I'd like to shut down my Raspberry Pi 3 via a Button connected to a GPIO.
Therefore i connected a button and wrote a script like in the link below (i used the while loop as in listing 2).
This works perfectly as long as i don't connect the printer (megatronic board) to the USB-port. When the printer is connected, the pi shuts down randomly after 0 to 60 sec just like as if the button is pressed. When i stop the python script the printer works fine.
I tried everything i could imagine to fix this. I used different GPIO pins, tried internal and external pullups and i checked the wiring often.
So the button works fine and the printer works fine but i can't use them at the same time.
Is the connection to the printer messing with the GPIOs?
I'm using 0.80.3 and the latest raspbian jessie by the way.
Greetings Andi
Comments
import time
import os
from subprocess import call
GPIO.setmode(GPIO.BCM)
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,GPIO.HIGH)
def Interrupt(channel):
print "Button Pressed"
call('halt', shell=False)
GPIO.add_event_detect(17, GPIO.RISING, callback = Interrupt, bouncetime = 250)
try:
while True:
# nix Sinnvolles tun
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
print "\nBye"
import time
import os
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,GPIO.HIGH)
while True:
input_state = GPIO.input(17)
if input_state == False:
print('Button Pressed')
os.system("sudo shutdown -h now")
time.sleep(2)
except KeyboardInterrupt:
GPIO.cleanup()
print "\nBye"
https://forum.repetier.com/discussion/4192/external-server-command-on-rpi-not-working
<span></span>