Quantcast
Channel: PirateBox Forum - Raspberry Pi(rate)Box
Viewing all articles
Browse latest Browse all 1863

Simplest way to add an on-off button

$
0
0
You ll need a 2 pin header to connect to pins 5-6. You can use any button for this purpose

you need to download a package with pacman. In new installations of 1.1.3 you first need to do
sudo rm /etc/resolv.conf
sudo sh -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf '


Then you download the actual neede package:

sudo pacman -S wiringpi

Create your script file:

sudo nano /home/alarm/buttononoff

paste the following code:
#!/bin/bash
pin=9
gpio mode $pin in
gpio mode $pin up
while true; do
 var=$(gpio read $pin)
 if [ "$var" -eq "0" ] ; then
  echo shutdown
  sudo shutdown -h now
 fi
sleep 0.1
done

ctrl+x to exit
y to save with desired name

give execution bit to script:
sudo chmod +x buttononoff

create service for script so it runs after every boot:

sudo nano /lib/systemd/system/button.service

paste the following code:

[Unit]
Description=Button Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/home/alarm/buttononoff
StandardOutput=null

[Install]
WantedBy=multi-user.target
Alias=button.service

give rights and enable the service:

sudo chmod 644 /lib/systemd/system/button.service
sudo systemctl daemon-reload
sudo systemctl enable button.service

finally reboot!

When the pi is up and running, by pressing the button, safe shutdown is initiated.
When the pi is powered off, by pressing the button, the pi boots without having to unplug and plug the usb cord again

Viewing all articles
Browse latest Browse all 1863

Trending Articles