Simplest way to add an on-off button

Posted by m3fisto 
Simplest way to add an on-off button
July 21, 2017 10:42AM
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
Re: Simplest way to add an on-off button
July 25, 2017 03:34PM
Hi,
thank you for that interesting howto. I am thinking of adding this simple package & script per default to the image.
What do you think about this?

Does this really work in non interactive way? In the current sudo configuration, the sudo command will ask for user & password. Ah, wait. The service file is not running as user alarm, but as root.

Best regards
Matthias
Re: Simplest way to add an on-off button
July 31, 2017 09:16AM
Yes it runs in a non interactive way since the systemd service is executed as root. I ve tried it on both a RPi 1B and a RPI 3B and it works nice on both of them.

Of cource feel free to include it as a default script. This kind of on-off button is being used in many other applications on RPis since it requries any simple button to work and nothing more.

#!/bin/bash
pin=9
gpio mode $pin in
gpio mode $pin up
counter=0
while true; do
 var=$(gpio read $pin)
 if [ "$var" -eq "0" ] ; then
  echo Button has been pressed..
  echo ""
  ((counter = counter + 1))
   if [ "$counter" -gt "10" ] ; then
      echo Shutting Down..
      sudo shutdown -h now
   fi
      if [ "$counter" -gt "3" ] && [ "$counter" -lt "10" ]; then
        echo Rebooting..
        sudo shutdown -r now
      fi 
 fi
sleep 0.1
done


Hi , i'm sure there is neater ways than this.. but here's my attempt at making the the On-Off Button dual action.. Press for ~3 sec's and we reboot... ~10 secs and it powers off.

Also , I added LED status using this method..
https://howchoo.com/g/ytzjyzy4m2e/build-a-simple-raspberry-pi-led-power-status-indicator
Re: Simplest way to add an on-off button
October 12, 2017 02:31AM
Oops:

Yeah , ok.. Flawed logic.. my first script still needed some work as it still thought a 10 second push was a reboot request , due to Reboot test going true @ 3 second's in..

try this...

#!/bin/bash
pin=9
gpio mode $pin in
gpio mode $pin up
counter=0
while true; do
 var=$(gpio read $pin)
 if [ "$var" -eq "0" ] ; then
		echo Button has been pressed..
		echo ""
		sleep 1
		((counter = counter + 1))
		if [ "$counter" -gt "10" ] ; then
		  echo Shutting Down..
		  sudo shutdown -h now
		fi
 else
		if [ "$counter" -gt "3" ] ; then
			echo Rebooting..
			sudo shutdown -r now
		fi 
		counter=0
 fi
sleep 0.1
done



Edited 3 time(s). Last edit at 10/12/2017 03:43AM by GarFin.
Re: Simplest way to add an on-off button
October 23, 2017 02:04PM
@GarFin

I tried your latest script and ran into the same issue. It still reboots at 10 seconds instead of shutting down. I ended up removing your reboot portion and only keep the shutdown. Thank you @m3fisto and @GarFin for posting this code \ guide.
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically.
 **    **  **    **  **     **   ******    **     ** 
  **  **   ***   **   **   **   **    **   ***   *** 
   ****    ****  **    ** **    **         **** **** 
    **     ** ** **     ***     **   ****  ** *** ** 
    **     **  ****    ** **    **    **   **     ** 
    **     **   ***   **   **   **    **   **     ** 
    **     **    **  **     **   ******    **     ** 
Message: