Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

power management, crash = reboot, power fail = shutdown, power on = boot, script = Python

Wed Nov 15, 2017 5:38 pm

hi all, ive had a couple instances where after a few days of running my raspberry has crashed (possibly my script was the issue but no error just an unresponsive raspberry often 48hour+ of running, i know an update may also resolve it), also the girlfriend likes to unplug things for her phone charger, hair straighteners etc and as i have some scripts storing temperature readings to a file i was worried a save at the same time as an unplug could cause an sd card corruption etc.

so i decided to put a circuit and script together that would allow for an auto reboot of the raspberry should it crash, and shutdown if the power is lost, but it must turn back on if the power is regained(this could be an issue if the power is turned off then back on while a shutdown of the raspberry is commencing, it may just never turn back on)
to turn the raspberry on ill be using pin5 connected to ground which will boot a shutdown raspberry but pin5 is predefined for I2C

what im wondering is there a better way to do things than using the multiple parts that i am currently using (other than changing relays and transistors for relay modules)

the circuit
Image

RPI1 = raspberry 40pin header
USB1 = raspberry power input
V1 = house electrical supply
PWR1 = 5v wall plug
Q1, Q2 = NPN transistor
RL1, RL2 = 5v relay
R1 = 220 ohm resistor
R2, R3 = 500 ohm resistor
D1, D2, D3, D4, D5 = simple diodes 1n4001 etc
C1 = 4700uf electrolytic capacitor
C2 = 4.5F 5.2v Super Capacitor
Off Delay Timer, such as https://www.schrack.com/shop/timer-sing ... r0011.html
B1 resets the timer, delaying the power off for another 10 minutes

crash protection.
the circuit uses an off delay timer like for a bathroom fan, set to disconnect the raspberry after 10minutes, a relay RL2 to B1 of the off delay resets its timer every 5 minutes using GPIO16, if the raspberry fails/crashes the signal should fail to send allowing for the power off timer to reach its 10 minute counter. the off delay will then disconnect the power on T15/T16 allowing for the raspberry to switch off, upon doing so C1 holds a small charge keeping RL1 open (uses NC contacts) for a short time allowing the RPi to completely power down, once C1 is drained, RL1 switches Normally Closed reconnecting B1, resetting the timer and Pin5 to ground which switches back on the raspberry.

power fail shutdown
power fails and PWR1 is no longer sending out 5v, C2 takes over (C2 is a super capacitor 5v+, 2.5F+ should be good) as this is grounded by diode D2, GPIO21 which is pulled up sees a loss in ground and engages a shutdown procedure.

auto boot
in the event that the raspberry crashed or power failed then come back, C1 would deplete its charge (or maybe C2 if running 5v equipemnt etc) allowing RL1 to go NC, when closed Pin 5 is connected to ground and the raspberry boots, once our script is running pin5 is disconnected from ground by sending pin32/GPIO12 high, switching RL1 open, allowing for pin5 to be used as an I2C channel.

the script

Code: Select all

import RPi.GPIO as GPIO
import time
import os

switch_off = 21  # gpio pin 40
reset_timer = 16  # gpio pin 36
on_switch = 12  # gpio pin 32

GPIO.setmode(GPIO.BCM)
GPIO.setup(switch_off, GPIO.IN, GPIO.PUD_UP)  # Switch off/Restart pin 40 to power supply ground
GPIO.setup(reset_timer, GPIO.OUT, initial=GPIO.LOW)  # pin 36 reset off delay timer
GPIO.setup(on_switch, GPIO.OUT, initial=GPIO.LOW)  # use pin 32 to disconnect pin 5 from ground so it can be used as I2C

count = 0

while 1:
    count += 1
    if GPIO.input(switch_off):
        print('shutdown')
        # stop any active process, stop save files etc
        GPIO.cleanup()
        os.system('shutdown now -h')
    if count == 300:
        GPIO.output(reset_timer, GPIO.HIGH)
        time.sleep(1.2)
        GPIO.output(reset_timer, GPIO.LOW)
        count = 0
    time.sleep(1)
if your aware of a better way of doing things please let me know

one issue i can see is if the raspberry crashes while RL2 is connected / GPIO16/pin36 is live then the raspberry would never reset. can anyone see a workaround to this without using a 2nd raspberry?

or perhaps 2x Raspberry Pis would be the way to go?

i know a crash in most cases is unlikely but if a raspberry is controlling the environment for plants/animals then id rather have an auto reboot just in case

BudBennett
Posts: 83
Joined: Fri May 17, 2013 2:45 pm
Location: Westcliffe, Colorado, USA

Re: power management, crash = reboot, power fail = shutdown, power on = boot, script = Python

Sun Nov 19, 2017 2:44 pm

A better way:
https://hackaday.io/project/25107-singl ... spberry-pi

Another better way:
https://hackaday.io/project/25116-singl ... spberry-pi

A Google search for "Raspberry Pi UPS" should turn up a bunch of other offerings.

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: power management, crash = reboot, power fail = shutdown, power on = boot, script = Python

Sun Nov 19, 2017 4:01 pm

thanks for the reply, im aware of uninterruptible power supplies and use an old APC BX 1400 for my PC but non of these offer an auto reboot should the RPi crash (though some circuitry and a script could offer this).
i did try to send myself an email when the power fails but the 4.5F cap didnt have enough power to successfully send the mail(was sending over GSM using a sim card HAT also powered by the cap) the first example is interesting and i may borrow some of his circuitry to increase the output of mine.

i get around 45seconds of power time from the 4.5F cap when just running an RPi and around 20secs when running a HAT/ancillaries, the RPi3 can remain powered up until the voltage depletes from 5v to around 2.4v but shutdown is initiated within 1 second of power loss and full shutdown takes just 6seconds

my main issue is the possibility of a crash while the off delay reset is live, using a UPS would only replace the power circuit and is something i did consider prior to building my own power management circuit

as im currently looking at options for a reptile habitat my next project must have a reset for in case of a crash, ill also be adding crash logging of some kind perhaps through an electrical counter or i may just use 2 RPi and do away with the delay timer and counter

ive also looked at using a 555 timer instead of the delay timer, its certainly possible but i cant see any benefit to this, low cost but increased build time.

BudBennett
Posts: 83
Joined: Fri May 17, 2013 2:45 pm
Location: Westcliffe, Colorado, USA

Re: power management, crash = reboot, power fail = shutdown, power on = boot, script = Python

Sun Nov 19, 2017 5:16 pm

Then check this post about the watchdog.

viewtopic.php?f=29&t=147501

This functionality seems to change as the OS gets updated, but I think that it would still work for what you are trying to accomplish. You can also use systemd to keep track of whether code is running or not and restart it automatically.

I found that the newer RPi's can shutdown pretty quickly, but the slower processors on the older versions and the Zero will take up to 20 seconds and you don't want it to shutdown while it is writing to the SD card.

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: power management, crash = reboot, power fail = shutdown, power on = boot, script = Python

Mon Nov 20, 2017 10:21 am

i had presumed when the raspberry crashed that would be the whole unit paused in its current process, the watchdog option seems like a much better idea than an hardwired interface and makes running with a UPS possible for my scenario without use of extra parts.
Thank you for your input on this, you have raised some good points which are certainly worth exploring further.

melaw
Posts: 2
Joined: Mon Feb 05, 2018 12:12 pm

Re: power management, crash = reboot, power fail = shutdown, power on = boot, script = Python

Mon Feb 05, 2018 4:49 pm

Davies wrote:
Sun Nov 19, 2017 4:01 pm
i get around 45seconds of power time from the 4.5F cap when just running an RPi and around 20secs when running a HAT/ancillaries, the RPi3 can remain powered up until the voltage depletes from 5v to around 2.4v but shutdown is initiated within 1 second of power loss and full shutdown takes just 6seconds
Did you try using a cheap DC/DC step up converter to get more joules out of the supercap?

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: simon1974 and 6 guests