sorry for bothering you here: I didn't know where to in the forum ask this question.
let me know if this is not the correct place: I will move my question elsewere in case.
I need my raspberry Pi tu 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
- Code: Select all | TOGGLE FULL SIZE
(void)system("shutdown -h now");
- Code: Select all | TOGGLE FULL SIZE
(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.
- Code: Select all | TOGGLE FULL SIZE
[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
- Code: Select all | TOGGLE FULL SIZE
[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:
- Code: Select all | TOGGLE FULL SIZE
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).
- Code: Select all | TOGGLE FULL SIZE
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
- Code: Select all | TOGGLE FULL SIZE
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).
- Code: Select all | TOGGLE FULL SIZE
#!/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?