Systemd-based monitoring of a GPIO pin (for Raspberry Pi).
The basic idea here is to monitor an input pin at the end of the Pi's main GPIO header for a short to the GND pin next to it and initiate an orderly shutdown. This is useful to shut down a headless Raspberry Pi without having to log in through the serial interface or ssh: just touch a paper clip to the pins at the end of the GPIO header. NOT THE 3V3 / 5V END!!
I had originally done this with a small python script but wanted to achieve it with minimal resources. Hence using only systemd and no other shell or script-based polling of the pin's value.
It works by setting up an input pin with internal pullup, assigning a falling edge detection interrupt and monitoring the pin's value through sysfs using a systemd.path unit.
This can easily be adpated to perform other tasks when a GPIO pin change is detected.
Dependencies:
Since I couldn't find a way to set an internal pullup directly through sysfs, I've reverted to using gpio-admin
, a program from quick2wire.
Usage:
-
Copy all systemd unit files to /etc/systemd/system.
-
Enable and start monitoring:
- 40 pin header:
# systemctl enable --now [email protected]
- 26 pin header:
# systemctl enable --now [email protected]
You may want to first test the action without actually shutting down:
- Edit
[email protected]
to echo the poweroff command. - Monitor the activation service:
- 40 pin header:
# journalctl -f -u [email protected]
- 26 pin header:
# journalctl -f -u [email protected]
Improvements are certainly welcome.