Executing a .py on the startup to trigger a GPIO button
Matthew Harrington
I am quite a n00b, and this task is not easy as I thought at a first glance. I am on a raspberryPi4 with Raspberry Pi OS Desktop with recommended software installed (really similar to every ubuntu, but the GPIO works from the first boot).
With success, I have written a .py program (named button_tab.py) that changes my chromium tab when a physical button, connected via GPIO pins of the raspberry, is pressed.
I need to start this .py script at the startup of the system
#name of the file: button_tab.py
import RPi.GPIO as GPIO
import subprocess
import time
GPIO.setmode(GPIO.BOARD)
buttonPin = 36
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True: buttonState = GPIO.input(buttonPin) if buttonState == False: subprocess.call(["xdotool", "key", "ctrl+Tab"]) time.sleep(0.5) This program works great when I execute it via:
python3 button_tab.pyEvery time I push the button, it changes the tab in the selected chromium window as if I push the keyboard keys ctrl+tab, and it has a cooldown of 0.5 sec in order to not trigger it multiple time at once.
I have even created a listen_for_button.sh script in order to execute it via bash.
#! /bin/sh
echo "Starting listen-for-button.py"
python3 /usr/bin/button_tab.pyWhen I start it manually, it works too
./listen_for_button.shNow the problem:
I need to start this script on the startup, so I Installed all the pip3 modules for the sudo
(like sudo -H pip3 install subprocess.run) and started to try different solutions:
- to run it via crontab:
@reboot /home/pi/listen_for_button.sh(nope) - to move listen_for_button.sh in
/etc/init.d/and to abilitate it viasudo rcconf(after installing it viasudo aptitute install rcconf sysv-rc-conf) (nope) - thanks to @Ra, I've edited with
sudo nano /etc/profilethe ~/bashrc configuration file adding/bin/bash /home/pi/listen_for_button.shat the bottom of the file (something moves, but the login stuck to my process probably, because no desktop is shown, only a black screen) - to build /lib/systemd/system/button_tab.service (spoiler... nope)
#the button_tab.service file:
[Unit]
Description=Button Tab Service
After=multi-user.target
Conflicts=
DefaultDependencies=false
[Service]
Type=simple
Environment="PATH=/home/{{ user }}/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/usr/bin/python3 /usr/bin/button_tab.py
Restart=on-abort
User=pi
[Install]
WantedBy=multi-user.targetand to enable it via:
sudo systemctl daemon-reload
sudo systemctl enable button_tab.service
sudo systemctl start button_tab.serviceThe process works (green light when asking for the status via sudo systemctl status button_tab.service) but the button doesn't work even if the process is running. I have tried different construction of the .service file: this is the last version. All the changes to the .service file have deal to the same result: nope
The only way that I have found to make the button works is to launch manually or the button_tab.py script or the listen_for_button.sh script.
None of the automated ways works.
Probably I am missing something basic, like the behaviour of the xdotool... I really don't know!
Please, I invoke the sudo help
Thanks
1 Answer
SOLVED! Thanks to the @Ray comments, was enough to do this:
sudo nano /etc/profileand add to the bottom of the file:
nohup /bin/bash /home/pi/listen_for_button.shDoing this, the ~/bashrc execute the .sh file in the background, with no output at all. So the login process can continue smoothly and the .sh file act like if it is executed by the user <3
I hope that this can help someone else. All the best
1