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

  • No, the server has no GPIO pin code on it's own as it is not for any special device and works on all.
    I know some pins are also used for serial console by default so they might not be used. Not sure which one and I also see no dependency on a serial connection being connected as long as this connection does not mirror to some pins, but we haven't written anything to do so if it happens it comes from linux directly and nor from server. 
  • I've done some more investigation:

    There seems to be a problem with the python script i used.

    This one didn't work with the printer connected:

    import RPi.GPIO as GPIO
    import time
    import os
    from subprocess import call

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(27,GPIO.OUT)
    GPIO.output(27,GPIO.HIGH)
    # Callback-Funktion
    def Interrupt(channel):
      print "Button Pressed"
      call('halt', shell=False)
    # Interrupt-Event hinzufuegen, steigende Flanke
    GPIO.add_event_detect(17, GPIO.RISING, callback = Interrupt, bouncetime = 250) 
    # Endlosschleife, bis Strg-C gedrueckt wird
    try:
      while True:
        # nix Sinnvolles tun
        time.sleep(1)
    except KeyboardInterrupt:
      GPIO.cleanup()
      print "\nBye"

    However this one works with the printer connected:

    import RPi.GPIO as GPIO
    import time
    import os
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(27,GPIO.OUT)
    GPIO.output(27,GPIO.HIGH)
    try:
     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"

    The first one uses an interrupt and a subprocess while the second one is just a regular if script.

    Does this make sense to anyone?
  • I got same  problem as you, random stop as soon as I touch something near the GPIO pins. It looks to be something with hardware, may be  bad ground isolation..   I tried to replace that code with the pigpio library, it allows to better filter when interrupt is coming.
Sign In or Register to comment.