Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Graceful delayed shutdown with timer and 1 relay
#11
Is there any advantage to a hardware solution to this vs a software solution? Personally, and only from my own experience, I've really struggled with hardware solutions but found that using Bluetooth detection (or USB) is so much easier. No hardware to install, nothing to buy.
Reply
#12
(10-24-2019, 08:35 PM)andyj Wrote: Is there any advantage to a hardware solution to this vs a software solution?  Personally, and only from my own experience, I've really struggled with hardware solutions but found that using Bluetooth detection (or USB) is so much easier.  No hardware to install, nothing to buy.
the rpi draws a fair bit of power in standby mode.  were you to leave your car in a garage for several days you'd come back to a flat battery.  the purpose of hw solution is to completely remove power from the pi to draw as little power as possible when actually shut off.
Reply
#13
(10-24-2019, 11:23 PM)Hisma Wrote:
(10-24-2019, 08:35 PM)andyj Wrote: Is there any advantage to a hardware solution to this vs a software solution?  Personally, and only from my own experience, I've really struggled with hardware solutions but found that using Bluetooth detection (or USB) is so much easier.  No hardware to install, nothing to buy.
the rpi draws a fair bit of power in standby mode.  were you to leave your car in a garage for several days you'd come back to a flat battery.  the purpose of hw solution is to completely remove power from the pi to draw as little power as possible when actually shut off.

What about a sudo poweroff instead of sudo shutdown -h now?  I've read that the Pi only consumes about 80mA that way.  In my case, even a couple of hundred mA isn't going to kill the battery between starts.  But 80mA would be about 12 hours per amp, 2 amps a day.  It would take quite a while to kill the battery with that.
Reply
#14
(10-25-2019, 05:01 PM)andyj Wrote:
(10-24-2019, 11:23 PM)Hisma Wrote:
(10-24-2019, 08:35 PM)andyj Wrote: Is there any advantage to a hardware solution to this vs a software solution?  Personally, and only from my own experience, I've really struggled with hardware solutions but found that using Bluetooth detection (or USB) is so much easier.  No hardware to install, nothing to buy.
the rpi draws a fair bit of power in standby mode.  were you to leave your car in a garage for several days you'd come back to a flat battery.  the purpose of hw solution is to completely remove power from the pi to draw as little power as possible when actually shut off.

What about a sudo poweroff instead of sudo shutdown -h now?  I've read that the Pi only consumes about 80mA that way.  In my case, even a couple of hundred mA isn't going to kill the battery between starts.  But 80mA would be about 12 hours per amp, 2 amps a day.  It would take quite a while to kill the battery with that.

you add it to everything else that draws power in your car and it makes a difference.  you don't just factor the pi alone.  also bluewave measured the output when powered off and it was over 100mA (don't remember the exact value).  my timer relay draws only 4mA.  It wasn't hard to implement and never gave me any problems.  But yeah if you want to go with a 100% software solution it's your choice.
Reply
#15
For me what is key about the hardware solution is the minimal current draw (from the timer) when the RPi is off. This is important as my car can sit for some weeks without being used.

The 10 amp version of the timer apparently draws no current at all when it's idle.

Also having the RPi power on with the ignition and not having to push another button I think is an advantage.

The down side of a cold start (as opposed to a return from standby) is that it takes around 30 seconds to boot.
Reply
#16
(09-14-2019, 07:19 PM)765GHF Wrote: After my Mausberry power supply failed (a common scenario apparently) I needed to find an alternate. 

My solution is based on this video - https://www.youtube.com/watch?v=rknBevexOcU on the Automate Everything channel and utilises a single SPST NC 12V relay and a programmable 12V timer. 


Overview

In short the timer provides power to the RPi and in my case peripherals (e.g. powered USB hub) when the IGN is on. When the IGN is switched off the timer continues to provide power to the RPi for a configurable amount of time before switching off (e.g. 15 mins). In tandem with this the relay closes when the IGN is switched off and sets a monitored GPIO pin low. A script on the RPi monitors the status of the GPIO pin and ensures that the RPi shuts down before the timer cuts power to the RPi (e.g. 14 minutes).

The advantages of this solution is that it is relatively simple, is straightforward to wire / connect and doesn't take up too much space. The disadvantage is that the timer is relatively expensive at €16 plus shipping (other suitable, less expensive timers may be out there - I didn't search). Also there is a very small chance that the script fails to shut down the RPi before the timer switches off. While idle (IGN off) the timer does consume 0.3 ma.



Setup diagram



Parts list:

Timer - https://www.amazon.com/gp/product/B015Z2...ct_title_1
12v - 5v buck converter - https://www.ebay.com/itm/24V-12V-to-5V-5...3349930955
Powered USB hub (optional) - https://www.amazon.fr/gp/product/B074VZD...le_o05_s01



I would recommend configuring and testing on the bench before installing in the car. This makes troubleshooting (IMO) a lot easier.



1. Detail

When IGN is on the timer switches on it's output which powers the 12V - 5V converter (which in turn powers the RPi and in my case an external USB hub - used for BT, sound card, reverse cam and charging phone etc.). 

When IGN goes off the timer starts it's countdown timer (timer is configurable - e.g. 15 mins), when countdown is complete the timer switches off the output which powers off the 12V - 5V converter which in turn powers off the RPi and USB hub. If the IGN is switched back on before the countdown finishes then the timer resets / cancels the countdown timer.

When the timer is off it draws a very small current (0.3 ma in low power mode (the default) when idle). The 10 amp version of the timer apparently draws zero current when idle but is more expensive at 19€.

The timer requires programming before use - see below and / or refer to https://www.youtube.com/watch?v=rknBevexOcU (Automate Everything)

To enable a graceful shutdown of the RPi (i.e. for the RPi to shutdown before the timer) I use a python script (running as a service on the RPi) which monitors the state of a GPIO pin (e.g. GPIO 17). A standard 12V SPST NC relay connected to the ignition controls the state of the monitored GPIO pin.

When the ignition is switched off the GPIO pin is set low by the relay which connects / shorts GND to GPIO 17. The scripts detects this and after a configurable delay initiates a graceful shutdown. To ensure that the RPi shuts down prior to the timer the configurable shutdown delay is less than the shutdown delay configured for the timer (e.g. 14 mins). If the ignition is turned back on the script detects this and no shutdown occurs.

The script and service definition file along with installation instructions are below.



2. Timer programming

Before use the timer must be programmed so that it powers off it's load x (e.g. 15) minutes after power (IGN) is lost.

This involves 

- setting the required shutdown delay (e.g. 15 minutes)
- setting function to #12
- setting trigger to #2.

Refer to example 12 in the timer cook book / programming guide - http://timers.shop/Timer-Cook-Book_ep_43-1.html#example12 for more detail on how to program the timer (or to the Automate Everything video - https://www.youtube.com/watch?v=rknBevexOcU)

I highly recommend purchasing (or making your own) timer configuration button board to save frustration when programming - http://timers.shop/Timer-configuration-b..._p_18.html.



3. Relay wiring

Pin 86 of the relay is connected to the ignition and pin 85 to ground. 
Pin 30 (COM) is connected to GPIO GND and pin 87A (NC) to the monitored GPIO pin (e.g. GPIO 17). 


The script and service definition file along with installation instructions are below.



4. Timer wiring

POWER (red wire) - connected to an always live +12V
TRIGGER (blue wire) connected to the IGN line 
LOAD (yellow) - the output of the timer - this is connected to the 12V - 5V buck converter (which in turns powers the RPi and USB hub and peripherals)
GROUND (black) - vehicle ground

The green and white programming wires should be shielded / insulated.



Disclaimer: I am neither a developer or automotive electrician - YMMV



5. Python script / service installation

Script

i. Copy and paste script (belowto /home/pi/power-monitor.py
ii. Create directory /opt/power-monitor - >sudo mkdir /opt/power-monitor
iii. Copy script to /opt/power-monitor/power-monitor.py >sudo cp /home/pi/power-monitor.py /opt/power-monitor/
iv. Set owner - >sudo chown root:root /opt/power-monitor/power-monitor.py
v. Add execute attribute to script - >sudo chmod +x /opt/power-monitor/power-monitor.py
vi. Set required GPIO pin (PORT) and shutdown delay (SHUTDOWN_DELAY) in script ->sudo nano /opt/power-monitor/power-monitor.py


Service

i. Copy and paste service definition file (below) to /home/pi/power-monitor.service
ii. Copy service definition file to /etc/systemd/system/power-monitor.service ->sudo cp /home/pi/power-monitor.service /etc/systemd/system/
iii.Set owner - >sudo chown root:root /etc/systemd/system/power-monitor.service
iv. Install service - >sudo systemctl enable power-monitor.service
v. Start service - >sudo systemctl start power-monitor.service


Testing / debugging 

i. In the script set the DEBUG variable to 1 - >sudo nano /opt/power-monitor/power-monitor.py
ii. Restart the service - >sudo systemctl restart power-monitor.service
iii. Debug output is directed to /var/log/daemon - view with >tail -f /var/log/daemon
iv. When finished set the DEBUG variable in the script back to 0 >sudo nano /opt/power-monitor/power-monitor.py


Service definition file - power-monitor.service

Code:
[Unit]
Description=Script to monitor ignition status
ConditionPathExists=/opt/power-monitor/power-monitor.py
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 -u /opt/power-monitor/power-monitor.py
Restart=on-failure
StartLimitInterval=60
RestartSec=5
StartLimitBurst=3

[Install]
WantedBy=multi-user.target


Script - power-monitor.py

Code:
#!/usr/bin/python3 -u

import RPi.GPIO as GPIO
import time
import subprocess

DEBUG = 0

# GPIO / BCM 17 - physical pin 11
PORT = 17

# Shutdown delay in seconds
# 14.5 minutes
SHUTDOWN_DELAY = 870

# No. of seconds after ignition returns to cancel shutdown
CANCEL_SHUTDOWN = 5

IGN_STATUS = 1
SHUTDOWN = 0
IGN_OFF_TIME = 0
IGN_OFF_LAST_SEEN = 0
NOW = 0


# specify pin numbering format
GPIO.setmode(GPIO.BCM)

# set pin to input, set pull up resistor so it reads high
GPIO.setup((PORT), GPIO.IN, pull_up_down=GPIO.PUD_UP)


while True:
    
    NOW = time.time()

    # Get the status of the ignition
    IGN_UP = GPIO.input(PORT)

    # Ignition switched off? then set shutdown flag and igntion off time 
    if not IGN_UP and not SHUTDOWN:
        SHUTDOWN = 1
        IGN_OFF_TIME = NOW
        print("Ignition switched off, shutdown flag set!")

    # Increment this counter while ignition is off
    if not IGN_UP and SHUTDOWN:
        IGN_OFF_LAST_SEEN = NOW

    # if igntion is off check if shutdown delay time reached
    if (SHUTDOWN and not IGN_UP and ((NOW - IGN_OFF_TIME) > SHUTDOWN_DELAY)):
        print("Shutdown delay of", SHUTDOWN_DELAY, "seconds reached, shutting down!")
        try:
            STATUS = subprocess.check_output(["shutdown", "-h", "now", "--no-wall"])
        except subprocess.CalledProcessError as shutdowncmd:
            print("Shutdown failed with error code: ", shutdowncmd.returncode)
            break    
    
    # Cancel shutdown if power has been back for CANCEL_SHUTDOWN seconds
    if ((NOW - IGN_OFF_LAST_SEEN) >= CANCEL_SHUTDOWN ) and SHUTDOWN:
        print("Ignition has been back for : ", CANCEL_SHUTDOWN, " seconds - shutdown flag reset")
        SHUTDOWN = 0
        IGN_OFF_TIME = 0
        IGN_OFF_LAST_SEEN = 0


    if DEBUG:
        STATUS = GPIO.input(PORT)
        print("Pin status is: ", STATUS)
        print("IGN_UP is set to: ", IGN_UP)
        print("SHUTDOWN is set to: ", SHUTDOWN)
        if SHUTDOWN:
            print("Igntion off time is: ", IGN_OFF_TIME)
            print("Igntion off last seen time is: ", IGN_OFF_LAST_SEEN)
            print("Current time is : ", NOW)
            print("Elapsed time is : ", (NOW - IGN_OFF_TIME))


    time.sleep(1)

if DEBUG:
    print("Exiting")

GPIO.cleanup()


Hi I really like your solution Congratulations, great work !!! and I would like to use it, I apologize immediately and I thank you in advance, because I have no practice in these things and would like an explanation.
Which relay did you use? 4 or 5 pin standard 12V SPST NC ?.
  you wrote: Pin 86 of the relay is connected to the ignition and pin 85 to ground.
Pin 30 (COM) is connected to GPIO GND and pin 87A (NC) to the monitored GPIO pin (e.g. GPIO 17).
If you use a 5-pin relay, where do you connect 87? the diagram does not appear!
Then please how did you connect the 12V - 5V buck converter?
only (yellow) - the output of the timer - this is connected to the 12V - 5V buck converter?
The positive and negative must be connected to the buck converter.
Help please
Reply
#17
On a 5 pin relay, 87 (NO) isn't connected to anything.
Reply
#18
Thanks for the feedback (although my post borrows heavily from the solution posted on the Automate Everything youtube channel).

I used a 5 pin relay as I had a spare one lying around - as such pin 87 (NO) wasn't connected. Otherwise I would have purchased a 12V 4 pin SPST NO relay.

Regarding the buck converter - think of the yellow (LOAD) wire from the timer as the positive input for the converter - so as such it is connected to the positive input on the converter. The negative input on the converter can be connected to any good ground /earth source in the car - I used an existing ground / negative feed to my stereo as my negative / ground source. As negative / ground is used as return (i.e. to complete the circuit) it does not need to be switched like the positive input.

Have a look at the diagram in the oriiginal post - the fork symbols represent a connection to earth / negative.



Cheers
Reply
#19
(11-02-2019, 01:26 PM)765GHF Wrote: Thanks for the feedback (although my post borrows heavily from the solution posted on the Automate Everything youtube channel).

I used a 5 pin relay as I had a spare one lying around - as such pin 87 (NO) wasn't connected. Otherwise I would have purchased a 12V 4 pin SPST NO relay.

Regarding the buck converter - think of the yellow (LOAD) wire from the timer as the positive input for the converter - so as such it is connected to the positive input on the converter. The negative input on the converter can be connected to any good ground /earth source in the car - I used an existing ground / negative feed to my stereo as my negative / ground source. As negative / ground is used as return (i.e. to complete the circuit) it does not need to be switched like the positive input.

Have a look at the diagram in the oriiginal post - the fork symbols represent a connection to earth / negative.



Cheers
Smile Thanks very kind, above all clear, I have almost all I need is the timer that I don't know if it can be sent here in Italy!, Done, now Ordered! about 13 euros shipping
Reply
#20
"Otherwise I would have purchased a 12V 4 pin SPST NO relay."

Of course I meant a NC relay not a NO relay above.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)