Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Safe shutdown using three relays
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.
Reply
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:

Code:
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:
Code:
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.
Code:
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.
Code:
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

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

Code:
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
Code:
# 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
Code:
# 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/documentatio...ge/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
Code:
[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:
Code:
#!/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/qu...an-stretch
https://www.raspberrypi.org/forums/viewt...p?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!
Reply
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.
Reply
(08-11-2019, 04:15 PM)skrimo Wrote: 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.


Hey mate,

I just want to say thanks for sharing your solution with us, as well as monitoring this thread afterwards to help people out.  My setup has been working great as per your diagram  and I really appreciate the effort you have put in for us.  Legend.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)