0

I need my raspberry Pi to turn OFF automatically when I press a button, so I've forked the Adafruit-GPIO-halt project and created a slight modified version of it here.

I've modified the C code and the makefile in order to have two different binaries:

  • first one for the shutdown;
  • Second one to reboot the pi

The source code files are almost identical, the only differ in line 264 where I've used different commands:

(void)system("shutdown -h now");

(void)system("shutdown -r now");

respectively.

I've also created two .service files where I call the binaries with two different GPIO (21 for shutdown, 16 for reboot) and also I add the second argument to have a delay. This are the services files.

[Unit]
Description=GPIO shutdown (pin 21 to ground)
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/local/sbin/gpio-shutdown 21 10000

[Install]
WantedBy=multi-user.target
[Unit]
Description=GPIO reboot (pin 16 to ground)
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/local/sbin/gpio-reboot 16 10000

[Install]
WantedBy=multi-user.target

I've enabled the services in order to have then working after a reboot. Indeed, after a manual reboot I'm able to see the services up and running:

pi@raspberrypi:~/Documents/repos/Adafruit-GPIO-Halt $ systemctl status gpio-shutdown.service
● gpio-shutdown.service - GPIO shutdown (pin 21 to ground)
     Loaded: loaded (/etc/systemd/system/gpio-shutdown.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-12-10 13:44:05 GMT; 5min ago
   Main PID: 1254 (gpio-shutdown)
      Tasks: 1 (limit: 3720)
        CPU: 6ms
     CGroup: /system.slice/gpio-shutdown.service
             └─1254 /usr/local/sbin/gpio-shutdown 21 10000

Dec 10 13:44:05 raspberrypi systemd[1]: Started GPIO shutdown (pin 21 to ground).
pi@raspberrypi:~/Documents/repos/Adafruit-GPIO-Halt $ systemctl status gpio-reboot.service
● gpio-reboot.service - GPIO reboot (pin 16 to ground)
     Loaded: loaded (/etc/systemd/system/gpio-reboot.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-12-10 13:44:12 GMT; 3min 27s ago
   Main PID: 1265 (gpio-reboot)
      Tasks: 1 (limit: 3720)
        CPU: 6ms
     CGroup: /system.slice/gpio-reboot.service
             └─1265 /usr/local/sbin/gpio-reboot 16 10000

Dec 10 13:44:12 raspberrypi systemd[1]: Started GPIO reboot (pin 16 to ground).

and

pi@raspberrypi:~/Documents/repos/Adafruit-GPIO-Halt $ ps -A | grep gpio
 1095 ?        00:00:00 gpio-reboot
 1096 ?        00:00:00 gpio-shutdown

The strange thing to me is that they don't work. Shorting pin 21 or 16 to GND and waiting 10 secs (and more) does nothing.

I've eventually noticed that running a python GPIO debug script and then shorting the pin to GND is the only way to "bring the services to life again" and make them work as expected (please find it inside the repo "gpio_test" folder, code below however).

#!/usr/bin/env python3

import RPi.GPIO as GPIO
import time

PINS = [21, 16]

GPIO.setmode(GPIO.BCM)

for pin in PINS:
   GPIO.setup( pin, GPIO.IN, pull_up_down=GPIO.PUD_UP )

try:
   while(1):
      #read pins
      values = [GPIO.input(pin) for pin in PINS]
      print( values )
      time.sleep(0.25)
except:
   print("cleanup")
   GPIO.cleanup()

Why? What am I doing wrong? Any ideas on how to make these script both run at startup and make them work as expected?


Edit: 24/01/2022

I was eventually able to do the thing I needed using the overlay method. In particular I've added the following line inside the /boot/config.txt file:

dtoverlay=gpio-shutdown,gpio_pin=21,debounce=3000

This way I'm able to use two GPIO, one for the shutdown (GPIO 21) and another one for turning on the PI (GPIO 3).

I want to share some observation I've made, experimenting with different dtoverlay configurations in case it can be useful for others:

note how in the blue circles the disconnection of GPIO 3 from GND has, in the two cases, opposite results; that is, the raspberry power on, following a previous power off, can be obtained:

  • connecting GPIO 3 to GND (if originally GPIO 3 was floating);
  • disconnecting GPIO 3 from GND (if originally GPIO 3 was connected to GND);

On the other hand, connection of GPIO 21 to GND has always the same effect: Raspberry shut-down.

Even if I was eventually able to find a solution to my problem, I'm still not able to understand the strange behaviour of services/python script described above :(

1 Answer 1

0

I might actually be able to help with this one, as I'm doing something similar enough that I have a snippet you could use.

I found that when your raspberry pi is off, if you connect the SCL pin, to ground, it will power back on. This requires no code on your half. See the below image for SCL:

It will need code however to turn it off once it's on. Coding is confusing to some, so let me know and I can simplify it if needed.

https://pastebin.com/zwKa73vc

Using the same gpio pin (and so, same button) we used to boot it up, this should let you press it for < 0.5 seconds to make it reboot. If you are still holding it after 0.5 seconds, it will shut down.

I've also left in the "volume" buttons, in case your project is similar to mine. I wanted a volume bar to display when pressing a volume up/down button, and although it's a very basic one, it does the job. I can remove these if needed. If you'd like to keep them, in your command terminal, you should just have to run this command: pip install pyalsaaudio

If you need this to run at startup, here's a link I've followed for this: https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/ Method 1, is probably the easiest. However, not the quickest. Assuming you're okay with it running once the pi is completely booted, I would follow this.

So, Save the code I put in pastebin, I recommend the desktop, as "shutdownController.py" then open the command terminal, and type sudo nano /etc/rc.local This will open the rc.local file in an editor that's given administrator privileges, and will allow you to edit the file.

Using the keyboard to navigate, right above "Exit 0" Paste in "sudo python /home/pi/Desktop/shutdownController.py &" by right clicking and pasting (not CTRL + V)

make sure to leave the trailing ampersand in the quote above.

2
  • 1
    please insert the code into your answer ... do not use a link
    – jsotola
    Jan 23 at 7:31
  • Thank you so much @Evan S four your reply!
    – orestino
    Jan 24 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.