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

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)
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