loading

When I use my Raspberry Pi without screen and keyboard, I have a lan or something else. But if I can't see my Raspberry Pi in lan, or if I can't see my Raspberry Pi on my screen, or if my keyboards not work because the batteries are died, I use my secret shutdown switch.

Do you want a Raspberry Pi 3? or a switch

Step 1: Write the Code

All the secret is in the code. Go to home folder.

cd /~

Create a new file with:

sudo nano shutdown.py

now write inside this:

import RPi.GPIO as GPIO import time import os

buttonPin = 21

GPIO.setmode(GPIO.BCM)

GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

last_state = True

input_state = True

while True:

input_state = GPIO.input(buttonPin)

if (not input_state):

print("Shutdown")

os.system('sudo shutdown -h now')

time.sleep(0.05) GPIO.cleanup()

Step 2: First the Code

When you start your Raspberry Pi, you can run the previous code by modifying the rc.local file.
Write this in terminal:

sudo nano /etc/rc.local

Then modify the file by adding this line before "exit 0"

python /home/pi/shutdown.py &

Step 3: Now Try to Shutdown by Wire

Now it's the time of truth.
Reboot you Raspberry Pi, and try to connect the last two GPIO pin like in figure. The last two pin in Raspberry Pi Zero are Ground and GPIO 21.

Ta taaa your Raspberry Pi shutdown without screen, keyboard or others kind of solutions. All you need is a cable...or a switch. See the image. My Raspberry Pi zero shutdown only when turn it upside down. I have used a tilt switch.

Do you want a tilt switch ?

About This Instructable

1,727views

27favorites

License:

More by masteruan:Meteo Node IoT Smart Meteo Station O-Zone: DIY Bluetooth Battery Lamp Omega Onion2 OLED Screen Show IP and LAN Name 
Add instructable to: