0

I have a program that turns my pi on and off at a push of a button. The program works and i know that it runs on boot because it prints text and I can see it in the terminal. But what happens is when i press the button, nothing happens. I have tried 2 different ways of auto running the program.

my code

import RPi.GPIO as GPIO
import time
import os


GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def shutdown(channel):
    os.system("sudo shutdown -h now")

    GPIO.add_event_detect(3, GPIO.FALLING, callback = shutdown, bouncetime = 2000)

and i have tried to auto run it by putting "python powerbutton.py" at the bottom of the file /etc/rc.local and .bashrc

Ghanima
  • 15,958
  • 17
  • 65
  • 125

1 Answers1

1

Your question is incomplete, but from the look of your (apparently) incomplete program it is not running.

For a working solution see https://raspberrypi.stackexchange.com/a/42945/8697

This would also be more robust, and less prone to unwanted shutdown due to spikes.

Milliways
  • 62,573
  • 32
  • 113
  • 225