BlueWave Studio forum
Safe shutdown using three relays - Printable Version

+- BlueWave Studio forum (https://bluewavestudio.io/community)
+-- Forum: Hardware for head-unit (https://bluewavestudio.io/community/forum-87.html)
+--- Forum: Power Supply and Safe Shutdown (https://bluewavestudio.io/community/forum-91.html)
+--- Thread: Safe shutdown using three relays (/thread-1147.html)

Pages: 1 2


Safe shutdown using three relays - Grabber - 08-09-2019

I have cleaned up the instructions based on the updated wiring diagram and video skrimo posted. I did this because I was confused after reading the instructions. But after seeing the video I came to the conclusion that "before" and "after" were being used instead of the words "splice" or "split".  Hey, its all good skrimo. I think I got it figured out and will be putting it together today on the bench.

1) ACC wire
- Splice ACC wire - connect one lead to 12v Relay #1 at pin 85 and connect second lead to 12v Relay #2 at pin 85

2) 12v Constant BAT+
- Splice 12v Constant BAT+ - connect one lead to 12v Relay #1 at pin 87 and connect second lead to 5v Delay Relay COM

3) 5V Delay Relay NO
- Splice lead from 5V Delay Relay NO pin - connect one lead to 12v Relay #1 at pin 30 and connect second lead to 12v to 5v Converter at 12v IN

4) Main Ground (GND)
- Connect 12v Relay #1 pin 86, 12v Relay #2 pin 86, and 12v to 5v Converter to Main Ground GND

5) Raspberry Pi to 5V Delay Relay
- Connect GPIO 5v pin 2 or 4 to 5V Delay Relay V+
- GPIO GND pin (6,9,14,20,25,30,34, or 39) I will use GPIO GND pin 6. Connect GPIO GND pin 6 to 5V Delay Relay GND
- GPIO pin (x) connect to 5V Delay Relay IN

6) Rasberry Pi to 12v Relay #2
- Connect GPIO 3.3v pin 17 to 12v Relay #2 at pin 30
- Conncet GPIO pin 23 to 12v Relay #2 at pin 87a

7) Wire up power connections from 12v to 5v Converter out to Raspberry Pi and Monitor as desired.

If you use different GPIO pins to connect to the 5v Delay Relay or 12v Relay #2 you need to modify the scripts to reflect the pins you used.


RE: Safe shutdown using three relays - tremers - 08-11-2019

Thanks skrimo and Grabber for this post on the 3 relay solution.

I was able to do the build with only the normal confusion in regard to GPIO numbers and pin outs, however this is all sorted - just check that you follow the diagram and Grabber's detailed notes.

Just a number of observations.
There was confusion over the use of the 2 scripts mentioned in this forum.

1. relay.py
  This python script has one function and that is to arm the delay relay so that when the PI shuts down the relay will start its timer and then turn power off.

2. switch.sh
  This bash script has four functions:
  a. it arms the delay relay - hence if you use this script you don't need to run the relay.py script also. 
      i.e. the channel in relay.py is the equivalent of GPIOpin2 in switch.sh
  b. to control KODI playback and blank the screen when the Accessories switch is turned off.
  c. to shutdown the pi when the the accessories has been off for a number of minutes (given by the shutdown delay)
  d. to resume playback in KODI and turn the screen back on.



Debugging Hardware IO

Lets say that you have used GPIOpin1=23 (Pin16) and GPIOpin2=24 (Pin18) for connections to the 12V and 5V relays.

In switch.sh GPIOPin1 is setup as an input with the following 2 commands:

echo "23" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio23/direction
Notice that I substituted the number 23 where the script uses that variable $GPIOpin1

It is possible to debug using simple commands to see what is going on.

For example to see if the pi is receiving the correct signal that the ACC is on or off then even if your script is running you can enter into a terminal the following:
cat /sys/class/gpio/gpio23/value
This command will return 0 or 1, 
0 means the ACC is on and 
1 means it's off. If you get an error then run the 2 commands above to set the GPIO as input.

Similarly you can check if relay.ph or switch.sh actually ran at startup and has armed the delay relay.
cat /sys/class/gpio/gpio24/value

if this command return "0" then the delay relay is not armed. This could mean that the script did not start or the GPIO was not configured etc.

You can also manually arm/disarm the switch from the terminal and observe the LED's on the delay relay.
echo "24" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio24/direction
The above 2 commands only needs to be done once after a reboot

echo "1" > /sys/class/gpio/gpio24/value
Will arm the relay (LED should light up and stay lit)

echo "0" > /sys/class/gpio/gpio24/value
Will start the relay countdown (LED should flash) and then the LED will go out after the timeout delay trigger has expired.

You can do these tests without needing to turn on/off the accessories, however so the pi does not experience a power off keep Accessories on!



Controlling the Screen
Switch.sh has the ability to control the screen, however I discovered that depending on the hardware screen the provided default actions do not work.

I have discovered 3 Ways to turn screen off/on
# Offical PI screen
echo 1 > /sys/class/backlight/rpi_backlight/bl_power #turns off screen back light 

--or--
# Another type of screen
vcgencmd display_power 0

#--my screen--
xset s activate   # uses the screen saver to display a blank screen 

Ways to turn screen on
# Offical PI screen
echo 0 > /sys/class/backlight/rpi_backlight/bl_power #turns on screen back light 

--or--
# Another type of screen
vcgencmd display_power 1

#--my screen--
xset s reset

Simply edit your switch.sh file to match your hardware.

But there's more, I discovered that depending on the your hardware the way you start switch.sh can make a difference.

For example: If you have an Official PI screen or the vcgencmd compatible one, you can use rc.local (as documented on this forum) to start the script at boot up.
An alternate is to use crontab (See https://www.raspberrypi.org/documentation/linux/usage/cron.md).

However for my hardware I had to use xset to control the screen which did not support disabling the backlight.
(note: I installed xscreensaver but I'm not sure if its required.  sudo apt-get install xscreensaver)

When I tested the script it worked fine, however my attempts at using rc.local or crontab to start it during boot up did not.

Using xset requires that the desktop be present and the script must be run in the users display context.

I solved this by:
  1. start relay.ph using rc.local so that the delay relay is armed as earlier as possible, and then 
  2. start script.sh via desktop auto start.
While I could have skipped relay.sh, the time taken to startup to the desktop takes longer before the delay relay is armed.

Desktop Auto start
To start a process during desktop startup, using the terminal create a file in the directory ".config/autostart" called .desktop

This it myfile: switch.desktop
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=Switch Autostart
Comment=
NoDisplay=true
Exec=sh -c '/home/pi/relay/switchstart.sh'
StartupNotify=false
NotShowIn=GNOME;KDE;XFCE;

The start file switchstart.sh:
#!/bin/bash

sudo /home/pi/relay/switch.sh &

I was not sure how to use sudo start in the auto start file so used this simple bootstrap method. Sudo is requried to get the correct permissions to set the input port.

References:
https://raspberrypi.stackexchange.com/questions/84189/how-to-control-screensaver-in-raspbian-stretch
https://www.raspberrypi.org/forums/viewtopic.php?p=931067

Hope that the above information assists those that have taken similar steps to use this 3 relay solution to power our PI's.

I am happy to say that my installation is in place and working a treat!

Cheers and good luck!


RE: Safe shutdown using three relays - skrimo - 08-11-2019

I'm glad you found my solution usable and that you documented what you needed to change to make it work for you.

I'm by no means an expert when it comes to raspberry pi, python coding, scripting,... I had a chance to do it, and I purely did it with Google help.
And I did it the way I needed it to work for the hardware I had. (hence the HAD) just sold my car, so no carpi for now.


Safe shutdown using three relays - how to - Daniel_BlueWave - 09-23-2019

Author of this solution is @skrimo

Only post regarding solution improvement will be accepted here (and whatever posts from @skrimo)

All others, not related post, should be placed here:
https://bluewavestudio.io/community/showthread.php?tid=797&pid=4739#pid4739

Otherwise they will be removed.

Thank you for understanding.

Starting point:
https://bluewavestudio.io/community/showthread.php?tid=797


RE: Safe shutdown using three relays - how to - only - stevens_albert - 10-18-2019

how do i get the scripts?


RE: Safe shutdown using three relays - how to - Robazz - 10-18-2019

(09-23-2019, 08:03 PM)Daniel_BlueWave Wrote: Author of this solution is @skrimo

Only post regarding solution improvement will be accepted here (and whatever posts from @skrimo)

All others, not related post, should be placed here:
https://bluewavestudio.io/community/showthread.php?tid=797&pid=4739#pid4739

Otherwise they will be removed.

Thank you for understanding.

Starting point:
https://bluewavestudio.io/community/showthread.php?tid=797

What has happened to all of the instructions and links that skrimo had previously posted, the links above go nowhere?

Thanks


RE: Safe shutdown using three relays - how to - only - skrimo - 10-18-2019

relay.py script
import RPi.GPIO as GPIO
import time

channel = 26

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)


def motor_on(pin):
    GPIO.output(pin, GPIO.HIGH)  # Turn fan on


def motor_off(pin):
    GPIO.output(pin, GPIO.LOW)  # Turn fan off


if __name__ == '__main__':
    try:
        motor_on(channel)
    except KeyboardInterrupt:
        GPIO.cleanup()


switch.sh script
#!/bin/bash

#this is the GPIO pin used for monitoring ACC state
GPIOpin1=23

#this has NOTHING to do with 3 relay system
GPIOpin2=24

#Enter the shutdown delay in minutes
delay=58

echo "$GPIOpin1" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
echo "$GPIOpin2" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
let minute=$delay*60
SD=0
SS=0
SS2=0
while [ 1 = 1 ]; do
power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
uptime=$(</proc/uptime)
uptime=${uptime%%.*}
current=$uptime
if [ $power = 1 ] && [ $SD = 0 ]
then
SD=1 && kodi-send --action="pause"
	sleep 10
	echo 1 > /sys/class/backlight/rpi_backlight/bl_power
SS=${uptime%%.*}
fi

if [ $power = 1 ] && [ $SD = 1 ]
then
SS2=${uptime%%.*}
fi

if [ "$((uptime - SS))" -gt "$minute" ] && [ $SD = 1 ] && [ $power = 1 ]
then
poweroff
SD=3
fi

if [ "$((uptime - SS2))" -gt 1 ] && [ $SD = 1 ]
then
SD=0 && echo 0 > /sys/class/backlight/rpi_backlight/bl_power
	kodi-send --action="play"
fi

sleep 1
done


this works great for my setup:
- Raspberry pi3b+ before and Raspberry pi4b now
- Official 7" touchscreen


RE: Safe shutdown using three relays - how to - only - stevens_albert - 10-18-2019

(10-18-2019, 12:04 PM)skrimo Wrote: relay.py script
import RPi.GPIO as GPIO
import time

channel = 26

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)


def motor_on(pin):
    GPIO.output(pin, GPIO.HIGH)  # Turn fan on


def motor_off(pin):
    GPIO.output(pin, GPIO.LOW)  # Turn fan off


if __name__ == '__main__':
    try:
        motor_on(channel)
    except KeyboardInterrupt:
        GPIO.cleanup()


switch.sh script
#!/bin/bash

#this is the GPIO pin used for monitoring ACC state
GPIOpin1=23

#this has NOTHING to do with 3 relay system
GPIOpin2=24

#Enter the shutdown delay in minutes
delay=58

echo "$GPIOpin1" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
echo "$GPIOpin2" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
let minute=$delay*60
SD=0
SS=0
SS2=0
while [ 1 = 1 ]; do
power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
uptime=$(</proc/uptime)
uptime=${uptime%%.*}
current=$uptime
if [ $power = 1 ] && [ $SD = 0 ]
then
SD=1 && kodi-send --action="pause"
	sleep 10
	echo 1 > /sys/class/backlight/rpi_backlight/bl_power
SS=${uptime%%.*}
fi

if [ $power = 1 ] && [ $SD = 1 ]
then
SS2=${uptime%%.*}
fi

if [ "$((uptime - SS))" -gt "$minute" ] && [ $SD = 1 ] && [ $power = 1 ]
then
poweroff
SD=3
fi

if [ "$((uptime - SS2))" -gt 1 ] && [ $SD = 1 ]
then
SD=0 && echo 0 > /sys/class/backlight/rpi_backlight/bl_power
	kodi-send --action="play"
fi

sleep 1
done


this works great for my setup:
- Raspberry pi3b+ before and Raspberry pi4b now
- Official 7" touchscreen


Thanks Skirmo, I really preciate it. I am pretty new to RBP projects, could you guide me a little on how to install the scripts? Im sorry for bothering you so much recently. :/



RE: Safe shutdown using three relays - how to - only - skrimo - 10-18-2019

No problem

Install OAP, then exit oap and open terminal.

First type these two lines
sudo nano relay.py (and paste in relay code)
sudo nano switch.sh (and paste in switch code)

Then
sudo chmod +x /home/pi/relay.py
sudo chmod +x /home/pi/switch.sh

Then
sudo nano /etc/rc.local

And write these two linea before exit0
python /home/pi/relay.py &
sudo /home/pi/switch.sh &

Finally
sudo reboot


Now it should all work if your wirring is corrent


RE: Safe shutdown using three relays - how to - only - stevens_albert - 10-18-2019

(10-18-2019, 05:57 PM)skrimo Wrote: No problem

Install OAP, then exit oap and open terminal.

First type these two lines
sudo nano relay.py (and paste in relay code)
sudo nano switch.sh (and paste in switch code)

Then
sudo chmod +x /home/pi/relay.py
sudo chmod +x /home/pi/switch.sh

Then
sudo nano /etc/rc.local

And write these two linea before exit0
python /home/pi/relay.py &
sudo /home/pi/switch.sh &

Finally
sudo reboot


Now it should all work if your wirring is corrent

Thank you! I was able to shutdown the pi with the delayed relay. The only problem I have now is that after I turn the ingition back on, the pi does not boot back up. I hve to manually unplug the usb and plug it back in. I am not sure if this is a problem with my wiring or my 5v relay. Any thoughts on how I can troubleshoot this? Thanks again!