Skip to main content

Use systemd to configure commands to run when a GPIO button is pressed on a Raspberry Pi

Project description

systemd-gpio

Use systemd to configure commands to run when a GPIO button is pressed on a Raspberry Pi.

This project is inspired by https://github.com/ali1234/systemd-gpio. The main difference is that we are using gpiozero rather than WiringPi, because WiringPi is no longer maintained.

Usage

To configure a GPIO pin, create a file called /etc/gpio/N, where N is an integer pin number following gpiozero's convention. For example, to configure actions for pin GPIO23, create the file /etc/gpio/23.

The file should use the systemd EnvironmentFile syntax. All variables are optional.

The values for GPIO_PULL_UP, GPIO_ACTIVE_STATE, GPIO_BOUNCE_TIME, GPIO_HOLD_TIME, and GPIO_HOLD_REPEAT are passed to the Python gpiozero.Button class.

The values of GPIO_WHEN_HELD, GPIO_WHEN_PRESSED, and GPIO_WHEN_RELEASED control the commands to be executed when the button is held, pressed, or released, respectively. See the corresponding methods gpiozero.Button.when_held, gpiozero.Button.when_pressed, and gpiozero.Button.when_released, respectively.

Once you have populated the configuration file, activate the button by running the following command, replacing N with the pin number:

sudo systemctl enable --now gpio@N.service

Examples

# /etc/gpio/23
GPIO_HOLD_TIME=5
GPIO_WHEN_PRESSED="wall 'Hold the power button for 5 seconds to power off.'"
GPIO_WHEN_HELD=poweroff

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

systemd-gpio-0.0.1.tar.gz (7.7 kB view hashes)

Uploaded Source

Built Distribution

systemd_gpio-0.0.1-py3-none-any.whl (7.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page