Solved by converting py script to bash
Steps:
sudo nano /opt/SafeShutdown.sh
Code:
#!/bin/bash
# GPIO pin definitions (BCM pin numbers)
powerPin=3 # BCM pin 3, physical pin 5
ledPin=14 # BCM pin 14, TXD
resetPin=2 # BCM pin 2, physical pin 13
powerenPin=4 # BCM pin 4, physical pin 5
# Initialize GPIO pins using pigpio
init_gpio() {
pigs m $powerPin r # Set powerPin to input (read)
pigs m $resetPin r # Set resetPin to input (read)
pigs m $ledPin w # Set ledPin to output (write)
pigs w $ledPin 1 # Turn LED on
pigs m $powerenPin w # Set powerenPin to output (write)
pigs w $powerenPin 1 # Enable power
}
# Wait for the power button to be pressed and hold for 1 second, then power off
poweroff() {
while true; do
if [ $(pigs r $powerPin) -eq 0 ]; then
sleep 1 # Wait for 1 second to confirm button is held down
if [ $(pigs r $powerPin) -eq 0 ]; then
sleep 5
sudo shutdown -r now
fi
fi
sleep 0.1
done
}
# Blink the LED while the power button is pressed
led_blink() {
while true; do
if [ $(pigs r $powerPin) -eq 0 ]; then
while [ $(pigs r $powerPin) -eq 0 ]; do
pigs w $ledPin 0 # LED OFF
sleep 0.2
pigs w $ledPin 1 # LED ON
sleep 0.2
done
fi
sleep 0.1
done
}
# Reset the Raspberry Pi when the reset button is pressed
reset_pi() {
while true; do
if [ $(pigs r $resetPin) -eq 0 ]; then
sleep 5
sudo shutdown -r now
fi
sleep 0.1
done
}
# Initialize GPIO and start the processes
init_gpio
# Run each function in the background
poweroff &
led_blink &
reset_pi &
# Wait for processes to complete (they never will in this loop)
wait
sudo apt-get update
sudo apt-get install pigpio python3-pigpio
sudo systemctl enable pigpiod
sudo systemctl start pigpiod
chmod +x /opt/SafeShutdown.sh
sudo nano /etc/systemd/system/SafeShutdown.service
Code:
[Unit]
Description=SafeShutdown
After=network.target
[Service]
ExecStart=/opt/SafeShutdown.sh
WorkingDirectory=/opt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=root
[Install]
WantedBy=multi-user.target
sudo systemctl enable SafeShutdown.service
sudo systemctl start SafeShutdown.service
sudo reboot - h now
done