forked from NeonHorizon/lipopi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlipopi.py
executable file
·143 lines (96 loc) · 3.86 KB
/
lipopi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# lipopi.py
# Example python script that uses the LiPoPi circuitry to handle the safe shutdown of
# a Raspberry Pi in response to user request or low battery
# See the README file for details on how to set this up with systemd
# this script is called by /etc/systemd/system/lipopi.service
# This version uses the GPIO event trigger machinery
# 2016 - Robert Jones - Freely distributed under the MIT license
# based on Daniel Bull's LiPoPi project - https://github.com/NeonHorizon/lipopi
import os
import RPi.GPIO as GPIO
import time
from picamera import PiCamera
from datetime import datetime
from signal import pause
# Configure the GPIO pins
now = ''
def lipopi_setup():
global lipopi
global camera
global now
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# setup the pin to check the shutdown switch - use the internal pull down resistor
GPIO.setup(lipopi['shutdown_pin'], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# setup the low battery check pin
#GPIO.setup(lipopi['low_battery_pin'], GPIO.IN)
now = "/home/pi/Desktop/%s.mp4" % datetime
# start video recording
camera.start_recording(now,format="h264")
# create a trigger for the shutdown switch and low battery pins
GPIO.add_event_detect(lipopi['shutdown_pin'], GPIO.RISING, callback=lipopi_user_shutdown, bouncetime=300)
#GPIO.add_event_detect(lipopi['low_battery_pin'], GPIO.FALLING, callback=lipopi_low_battery_shutdown, bouncetime=300)
# open log file in append mode
lipopi['logfile_pointer'] = open(lipopi['logfile'], 'a+')
# Detect when the switch is pressed - wait shutdown_wait seconds - then shutdown
def lipopi_user_shutdown(channel):
global lipopi
global camera
global now
camera.stop_recording()
camera.close()
cmd = "cp /home/pi/Desktop/%s /boot/recordings/" % now
os.system(cmd)
cmd = "rm /home/pi/Desktop/*.mp4"
os.system(cmd)
cmd = "sudo wall 'System shutting down in %d seconds'" % lipopi['shutdown_wait']
os.system(cmd)
time.sleep(lipopi['shutdown_wait'])
msg = time.strftime("User Request - Shutting down at %a, %d %b %Y %H:%M:%S +0000\n", time.gmtime())
lipopi['logfile_pointer'].write(msg)
lipopi['logfile_pointer'].close()
GPIO.cleanup()
os.system("sudo shutdown now")
# Respond to a low battery signal from the PowerBoost and shutdown
# Pin goes low on low battery
def lipopi_low_battery_shutdown(channel):
global lipopi
global camera
camera.stop_recording()
camera.close()
cmd = "sudo wall 'System shutting down in %d seconds'" % lipopi['shutdown_wait']
os.system(cmd)
time.sleep(lipopi['shutdown_wait'])
msg = time.strftime("Low Battery - Shutting down at %a, %d %b %Y %H:%M:%S +0000\n", time.gmtime())
lipopi['logfile_pointer'].write(msg)
lipopi['logfile_pointer'].close()
GPIO.cleanup()
os.system("sudo shutdown now")
# Close the log file, reset the GPIO pins
def lipopi_cleanup():
global lipopi
global camera
lipopi['logfile_pointer'].close()
GPIO.cleanup()
# Main --------------------------------------------
# Get current datetime to be used as filename
datetime = datetime.now().isoformat()
# Setup camera
camera = PiCamera()
# Setup LiPoPi global variable array
lipopi = {}
# Specify which GPIO pins to use
lipopi['low_battery_pin'] = 15
lipopi['shutdown_pin'] = 18
lipopi['logfile'] = '/home/pi/lipopi.log' # FULL path to the log file
# or do this relative to the location of this script?
lipopi['shutdown_wait'] = 2 # seconds - how long to wait before actual shutdown - can be 0 if you want
# setup the GPIO pins and event triggers
lipopi_setup()
# Although the shutdown is triggered by an interrupt, we still need a loop
# to keep the script from exiting - just do a very long sleep
while True:
time.sleep(6000)
# clean up if the script exits without machine shutdown
lipopi_cleanup()