JustSumDad
Posts: 5
Joined: Thu Apr 02, 2020 8:28 pm

shutdown script

Thu Apr 02, 2020 8:40 pm

Hello all, first post here :)

I am working on a shutdown/start up system for a car application.

I have all the power distribution and conversion working and indeed everything works, but I have an issue.
I am using a relay to act as a power button, connected to GPIO pins 5/6, the relay is held open when the engine is running and when the key goes off the relay closes the circuit to pins 5/6 thus initiating the shut down via script.
When the engine starts back up, the relay is charged and the circuit is opened and the PI wakes up.
All good.

I used this guide to create my shutdown system:
https://howchoo.com/g/mwnlytk3zmm/how-t ... spberry-pi

I modified one part of the code to make a 45 min delay in the shutdown, this is so I can access the PI via wifi and collect files from it or whatever when I get home.

First off, I am lousy at scripting, I just don't do enough to keep it active in my mind so I forget what I learned a few weeks later when I run into any need of it again.
So please bear with me and please if you can help :) Thank you!!

So here is my problem, when I go to the store, I shut down the engine, the auto shut down begins, I come back 20 min later and start the engine.
The shut down is still running, 25 min later the RPI shuts down, I am still driving...

How can I modify the script to cancel the shut down if it detects pins 5/6 are restored to the open circuit state?

I am fairly certain I should be able to add a few lines to this script:
#!/usr/bin/env python

import RPi.GPIO as GPIO
import subprocess


GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)

subprocess.call(['shutdown', '-h', '+45'], shell=False)

But with my limited knowledge, I am hoping someone can save me many hours of wasted time and effort :)

Thank you in advance!

klricks
Posts: 7154
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: shutdown script

Thu Apr 02, 2020 11:04 pm

JustSumDad wrote:
Thu Apr 02, 2020 8:40 pm
subprocess.call(['shutdown', '-h', 'now'], shell=False)
Instead of shutdown -h now, specify the minutes to shutdown like this:

Code: Select all

shutdown -h +45 
When ignition power on is sensed then issue this to cancel any pending shutdowns:

Code: Select all

shutdown -c
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

JustSumDad
Posts: 5
Joined: Thu Apr 02, 2020 8:28 pm

Re: shutdown script

Fri Apr 03, 2020 5:04 am

klricks wrote:
Thu Apr 02, 2020 11:04 pm
JustSumDad wrote:
Thu Apr 02, 2020 8:40 pm
subprocess.call(['shutdown', '-h', 'now'], shell=False)
Instead of shutdown -h now, specify the minutes to shutdown like this:

Code: Select all

shutdown -h +45 
When ignition power on is sensed then issue this to cancel any pending shutdowns:

Code: Select all

shutdown -c
Yes I did the +45 in the script so the auto shut down already and that does work.

The manual abort the shut down will not work for me tho, as I have no way to get access to the system, it has no screen, I have nothing to ssh in while I am on the road.

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: shutdown script

Fri Apr 03, 2020 6:26 am

My Python skills are a bit pants, but the way I would do it is to have a counter that counts down to zero, then do the shutdown. If the GPIO changes state, then reset the counter to stop the countdown.

This code is probably wrong (and completely untested), but should give you the general idea.

Code: Select all

#!/usr/bin/env python3

inport RPi.GPIO as GPIO
import subprocess

timeout = -1

GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN)

while timeout != 0:
    time.sleep(1)
    if GPIO.in(3) = 0:
        if timeout = -1:
            timeout = 2700 # start 45 minute countdown
        else:
            timeout = timeout - 1 # continue countdown
    else:
        timeout = -1 # stop the countdown

# Countdown reached zero without being stopped. Shutdown!
subprocess.call(['shutdown', '-h', 'now']
There's no need for a internal pull-up on GPIO3 (pin 5) as it has an on-board pull-up resistor anyway.

I've used python3 instead of just python, as that is probably a good idea.

I haven't used "wait for edge" as the timing isn't that critical. Just checking the current state of the GPIO will do. The relay is going to stay in one state or another for some time.

I welcome any criticism/corrections of the code from those who do Python. :)

With a little more code and hardware, you could have a status LED. LED is on while car is running, flashing one a second during the countdown and obviously off when shutdown starts.

With a little more work you could have a small LCD display with the current status on.
Unreadable squiggle

klricks
Posts: 7154
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: shutdown script

Fri Apr 03, 2020 5:12 pm

JustSumDad wrote:
Fri Apr 03, 2020 5:04 am
klricks wrote:
Thu Apr 02, 2020 11:04 pm
JustSumDad wrote:
Thu Apr 02, 2020 8:40 pm
subprocess.call(['shutdown', '-h', 'now'], shell=False)
Instead of shutdown -h now, specify the minutes to shutdown like this:

Code: Select all

shutdown -h +45 
When ignition power on is sensed then issue this to cancel any pending shutdowns:

Code: Select all

shutdown -c
Yes I did the +45 in the script so the auto shut down already and that does work.

The manual abort the shut down will not work for me tho, as I have no way to get access to the system, it has no screen, I have nothing to ssh in while I am on the road.
I did not suggest any manual intervention.....
I am saying...... modify your code so that when the GPIO pin senses ignition switch OFF then issue the shutdown -h +45 command. When the same GPIO senses ignition switch is ON then issue the shutdown -c to cancel the pending shutdown command.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

JustSumDad
Posts: 5
Joined: Thu Apr 02, 2020 8:28 pm

Re: shutdown script

Fri Apr 03, 2020 6:17 pm

klricks wrote:
Fri Apr 03, 2020 5:12 pm
JustSumDad wrote:
Fri Apr 03, 2020 5:04 am
klricks wrote:
Thu Apr 02, 2020 11:04 pm

Instead of shutdown -h now, specify the minutes to shutdown like this:

Code: Select all

shutdown -h +45 
When ignition power on is sensed then issue this to cancel any pending shutdowns:

Code: Select all

shutdown -c
Yes I did the +45 in the script so the auto shut down already and that does work.

The manual abort the shut down will not work for me tho, as I have no way to get access to the system, it has no screen, I have nothing to ssh in while I am on the road.
I did not suggest any manual intervention.....
I am saying...... modify your code so that when the GPIO pin senses ignition switch OFF then issue the shutdown -h +45 command. When the same GPIO senses ignition switch is ON then issue the shutdown -c to cancel the pending shutdown command.
Yes that is exactly what I am asking for help with. Since its not my code and I don't know how to code and I have little use to code.

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: shutdown script

Fri Apr 03, 2020 7:35 pm

This is how I would do it based on your existing code.

Code: Select all

import RPi.GPIO as GPIO
import subprocess
import time


GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:

    GPIO.wait_for_edge(3, GPIO.FALLING)    
    subprocess.call(['shutdown', '-h', '+45'], shell=False)
    time.sleep (5)
    
    GPIO.wait_for_edge(3, GPIO.RISING)
    subprocess.call(['shutdown', '-c'], shell=False)
    time.sleep (5)
    
So you wait as you did before , when a falling edge is detected you start the 45 min countdown in the shutdown .

after 5 seconds, allows for relay contact bounce and stops program looping to quickly,

we start waiting for a rising edge , if one never occurs the pi will shutdown after allotted time,
however if rising edge does occur then the shutdown cancel is called , the code then loops after 5 seconds and goes back to waiting for a falling edge. this has not been tested I leave that up to you.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Beginners”