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 :(