Cheap solution for automatic shutdown PiDashCam
For the automatic shutdown I have used a 12V programmable timer in combination with an optocoupler.
I have used a CNY17-3 type because I had it lying around but I think other types will also work.
The timers you can buy very cheap on eBay or Banggood, 2 examples are given:-
The timer is getting a power supply that is always there from somewhere in the car. The trigger
signal comes from a switched 12V power supply, I used the power supply from the car radio.
The input for the optocoupler is also taken from the car radio power supply.
When the car is powered the the timer gets triggered and is starting the power supply for the Raspberry pi,
Dashcam pi will get started and will do its job. When the car is turned off the optocoupler triggers the
GPIO pin of the raspberry and starts a shutdown. After 15 seconds the timer is shutting down
and the raspberry pi is powerless. You can chose the length of time when the timer shuts down
the power supply. I have used 15 seconds and that is enough to shut down the raspberry.
I have written a script for the DashCamPi so it can properly shut down called pwr-dashcam.py
Create it with the following contents:-
import RPi.GPIO as GPIO
# GPIO25 (pin 22) set up as input. It is pulled up to stop false signals
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# wait for the pin to be sorted with GND and, if so, halt the system
subprocess.call(['shutdown -h -P now "System halted by GPIO action"'], shell=True)
# clean up GPIO on normal exit
(You can use other pins if you want to)
Then I have place the script file in the same place as the otherscript files from the dashcam in the map /root/scripts/
Then you have to make the scriptfile executable
# chmod +x /root/scripts/pwr-dashcam.py
Then create a service file so that the script to shutdown the raspberry pi is automatically started.
# nano /etc/systemd/system/pwr-dashcam.service
Then add the following text to the service file:
Description=Service to start the power detection for Dashcam
To start the service file use the following commands.
# systemctl daemon-reload
# systemctl start pwr-dashcam.service
To let the service start automaticly after a reboot execute the following command:
# systemctl enable pwr-dashcam.service
Now everything should be working. In my car it is.