PiSupply / Pi-Supply-Switch

Pi Supply Switch On/Off Switch for Raspberry Pi
https://www.pi-supply.com/product/pi-supply-raspberry-pi-power-switch/
Other
58 stars 17 forks source link

Softshut Not working #17

Open LDDill opened 4 years ago

LDDill commented 4 years ago

Hi I followed all of the instructions step-by-step. I am using a raspberry pi 3B+ running raspbian stretch. The ON and Off buttons work, but not the Softshut Off button. Shown below is what I get when I run "systemctl status piswitch.service". Any suggestions?

pi@raspberrypi:~ $ systemctl status piswitch.service ● piswitch.service - Starts softshut for Pi Switch Loaded: loaded (/etc/systemd/system/piswitch.service; enabled; vendor preset: Active: failed (Result: exit-code) since Fri 2020-01-31 16:57:53 CST; 13min a Process: 348 ExecStart=/opt/piswitch/softshut.py (code=exited, status=1/FAILUR Main PID: 348 (code=exited, status=1/FAILURE)

Jan 31 16:57:52 raspberrypi systemd[1]: Started Starts softshut for Pi Switch. Jan 31 16:57:53 raspberrypi softshut.py[348]: Traceback (most recent call last): Jan 31 16:57:53 raspberrypi softshut.py[348]: File "/opt/piswitch/softshut.py" Jan 31 16:57:53 raspberrypi softshut.py[348]: GPIO.wait_for_edge(PinSeven, G Jan 31 16:57:53 raspberrypi softshut.py[348]: RuntimeError: Error waiting for ed Jan 31 16:57:53 raspberrypi systemd[1]: piswitch.service: Main process exited, c Jan 31 16:57:53 raspberrypi systemd[1]: piswitch.service: Unit entered failed st Jan 31 16:57:53 raspberrypi systemd[1]: piswitch.service: Failed with result 'ex lines 1-14/14 (END)

This is what my softshut.py file looks like.

!/usr/bin/env python

Import the modules to send commands to the system and access GPIO pins from subprocess import call import RPi.GPIO as GPIO from time import sleep

Map pin seven and eight on the Pi Switch PCB to chosen pins on the Raspberry Pi header The PCB numbering is a legacy with the original design of the board PinSeven = 7 PinEight = 11 GPIO.setmode(GPIO.BOARD) # Set pin numbering to board numbering GPIO.setup(PinSeven, GPIO.IN) # Set up PinSeven as an input GPIO.setup(PinEight, GPIO.OUT, initial=1) # Setup PinEight as output

while (GPIO.input(PinSeven) == False): # While button not pressed GPIO.wait_for_edge(PinSeven, GPIO.RISING) # Wait for a rising edge on PinSeven sleep(0.1); # Sleep 100ms to avoid triggering a shutdown when a spike occured

sleep(2); # Sleep 2s to distinguish a long press from a short press

if (GPIO.input(PinSeven) == False): GPIO.output(PinEight,0) # Bring down PinEight so that the capacitor can discharge and remove power to the Pi call('poweroff', shell=False) # Initiate OS Poweroff else: call('reboot', shell=False) # Initiate OS Reboot

Thanks!

shawaj commented 4 years ago

Do you have the board hooked up to the Pi as per the guide linked below?

The options as shown in the two pictures here are pins 2 and 6 or pins 7 and 11 - https://learn.pi-supply.com/pi-supply-switch-v1-1-assembly-instructions/

But with the Pin 7 / 11 you need the software running on the pi for it to work correctly

LDDill commented 4 years ago

Hi @shawaj thanks for reaching out. I have physically hooked up the power switch to my Pi in this order: USB to micro USB from power supply to power switch, USB to micro USB from power switch to Pi, Pin 7 on the power switch to Pin 7 on the Pi, and Pin 8 on the power switch to Pin 11 on the Pi. Because of the error I get when running "systemctl status piswitch.service", I believe my issue is with the software. The error is listed below:

pi@raspberrypi:~ $ systemctl status piswitch.service ● piswitch.service - Starts softshut for Pi Switch Loaded: loaded (/etc/systemd/system/piswitch.service; enabled; vendor preset: Active: failed (Result: exit-code) since Fri 2020-01-31 16:57:53 CST; 13min a Process: 348 ExecStart=/opt/piswitch/softshut.py (code=exited, status=1/FAILUR Main PID: 348 (code=exited, status=1/FAILURE)

Jan 31 16:57:52 raspberrypi systemd[1]: Started Starts softshut for Pi Switch. Jan 31 16:57:53 raspberrypi softshut.py[348]: Traceback (most recent call last): Jan 31 16:57:53 raspberrypi softshut.py[348]: File "/opt/piswitch/softshut.py" Jan 31 16:57:53 raspberrypi softshut.py[348]: GPIO.wait_for_edge(PinSeven, G Jan 31 16:57:53 raspberrypi softshut.py[348]: RuntimeError: Error waiting for ed Jan 31 16:57:53 raspberrypi systemd[1]: piswitch.service: Main process exited, c Jan 31 16:57:53 raspberrypi systemd[1]: piswitch.service: Unit entered failed st Jan 31 16:57:53 raspberrypi systemd[1]: piswitch.service: Failed with result 'ex lines 1-14/14 (END)

Any suggestions?

Thanks!

LDDill commented 4 years ago

I have found a solution to the error I was receiving, "RuntimeError: Error waiting for edge". There appears to be an issue in the RPi.GPIO directory when calling "GPIO.wait_for_edge(PinSeven, GPIO.RISING)". The code runs perfectly if that line is removed (as shown below).

!/usr/bin/env python

Import the modules to send commands to the system and access GPIO pins from subprocess import call import RPi.GPIO as GPIO from time import sleep

Map pin seven and eight on the Pi Switch PCB to chosen pins on the Raspberry Pi header The PCB numbering is a legacy with the original design of the board PinSeven = 7 PinEight = 11 GPIO.setmode(GPIO.BOARD) # Set pin numbering to board numbering GPIO.setup(PinSeven, GPIO.IN) # Set up PinSeven as an input GPIO.setup(PinEight, GPIO.OUT, initial=1) # Setup PinEight as output

while (GPIO.input(PinSeven) == False): # While button not pressed

sleep(0.1); # Sleep 100ms to avoid triggering a shutdown when a spike occured

sleep(2); # Sleep 2s to distinguish a long press from a short press

if (GPIO.input(PinSeven) == False): GPIO.output(PinEight,0) # Bring down PinEight so that the capacitor can discharge and remove power to the Pi call('poweroff', shell=False) # Initiate OS Poweroff else: call('reboot', shell=False) # Initiate OS Reboot

shawaj commented 4 years ago

@LDDill is this an error in our code? Or something you added?

LDDill commented 4 years ago

@shawaj The original code had "GPIO.wait_for_edge(PinSeven, GPIO.RISING)" on line 17. However, the code would give an error when ran. After removing that line the code works correctly. With a press initiating shut down and a hold initiating reboot.