Due to high demand, expect some shipping delays at this time - orders may not ship for up to 2-3 business days.
0

Adafruit GPIO Halt - doesn't work for me if running as servi
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Please be positive and constructive with your questions and comments.

Adafruit GPIO Halt - doesn't work for me if running as servi

by moscardo on Fri Dec 10, 2021 3:21 pm

Hi guys,
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
The source code files are almost identical, the only differ in line 264 where I've used different commands:
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?

moscardo
 
Posts: 29
Joined: Sun Nov 06, 2016 6:12 am

Re: Adafruit GPIO Halt - doesn't work for me if running as s

by moscardo on Mon Jan 24, 2022 9:44 am

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:

Code: Select all | TOGGLE FULL SIZE
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 cas it can be useful for others:

dtoverlay_experiments.jpg
dtoverlay_experiments.jpg (75.88 KiB) Viewed 36 times


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

moscardo
 
Posts: 29
Joined: Sun Nov 06, 2016 6:12 am

Please be positive and constructive with your questions and comments.