-
Notifications
You must be signed in to change notification settings - Fork 6
/
amdsv2.py
155 lines (146 loc) · 5.33 KB
/
amdsv2.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
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python
#https://github.com/sakearzoo
#https://github.com/abhisheksarkar30
#By Sheikh Nawab Arzoo & Abhishek Sarkar
import RPi.GPIO as GPIO
import time
import picamera
import datetime
import subprocess
import dropbox
import os
import sms
import logging
import signal
import sys
import MFRC522
LOG_FORMAT = "%(asctime)s - %(levelname)s: %(message)s"
logging.basicConfig(filename='amdsv2.log', format=LOG_FORMAT, level=logging.INFO)
# Get your app key and secret from the Dropbox developer website
app_key = '*******' // enter your app here from dropbox
app_secret = '*******' // enter your secret key from dropbox
logging.info("Automated System started!")
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
print "Ctrl+C captured, ending read."
logging.warning("Automated System terminated!")
GPIO.cleanup();
sys.exit(1);
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
def dropboxAuth():
accessTokenFileOverwrite = open("accessToken.txt", "w+")
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key,app_secret)
authorize_url = flow.start()
# Have the user sign in and authorize this token
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code'
code = raw_input("Enter the authorization code here: ").strip()
try:
# This will fail if the user enters an invalid authorization code
access_token, user_id = flow.finish(code)
accessTokenFileOverwrite.write(access_token)
except:
print "failed authorization, restart"
accessTokenFileOverwrite.close()
os.remove("accessToken.txt")
accessTokenFileOverwrite.close()
def dropboxUpload(fileToUpload):
if not os.path.isfile("accessToken.txt"):
dropboxAuth()
#get access token from file
accessTokenFileRead = open("accessToken.txt", "r")
access_token = accessTokenFileRead.read()
accessTokenFileRead.close()
# make client
client = dropbox.client.DropboxClient(access_token)
#upload file
fileToUploadObject = open(fileToUpload, "rb")
response = client.put_file(fileToUpload, fileToUploadObject)
fileToUploadObject.close()
sensorPin = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
prevState = False
currState = False
cam = picamera.PiCamera()
num1='*********' //enter the 1st number here
num2='*********' //enter the 2nd number here
msg_warn='Motion Detected!!! Get video @ https://www.dropbox.com/home/Apps/ \nFrom PI'
msg_prob='Problem occurred in AMDSv2! Check Log for details!\nFrom PI'
db=[[0x01,0x02],["Abhishek","Arzoo"]]
count=0
while True:
time.sleep(0.1)
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
(status,uid) = MIFAREReader.MFRC522_Anticoll()
read=[]
if status == MIFAREReader.MI_OK:
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
MIFAREReader.MFRC522_SelectTag(uid)
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
if status == MIFAREReader.MI_OK:
read=MIFAREReader.MFRC522_Read(8)
if read[0] in db[0]:
if read[1]==0:
count+=1
logging.info("%s entered! %s" %(db[1][db[0].index(read[0])],"Script Paused!" if count==1 else ""))
print "%s entered! %s" %(db[1][db[0].index(read[0])],"Script Paused!" if count==1 else "")
else:
count-=1
logging.info("%s exited! %s" % (db[1][db[0].index(read[0])],"Script Resumed!" if count==0 else ""))
print "%s exited! %s" % (db[1][db[0].index(read[0])],"Script Resumed!" if count==0 else "")
read[1]=1 if read[1]==0 else 0
MIFAREReader.MFRC522_Write(8, read)
time.sleep(1)
else:
logging.warning("Unauthorised Attempt! %s" % uid)
MIFAREReader.MFRC522_StopCrypto1()
else:
logging.warning("Unauthorised Attempt! %s" % uid)
if count==0:
prevState = currState
currState = GPIO.input(sensorPin)
if currState != prevState:
if currState:
print "A MOTION HAS BEEN DETECTED !!! Recording Started!"
logging.warning("A MOTION HAS BEEN DETECTED !!! Recording Started!")
fileName = datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
try:
cam.start_preview()
cam.start_recording(fileName)
print (fileName)
except Exception, Argument:
logging.error(Argument)
sms.sendSMS(num1,msg_prob)
sms.sendSMS(num2,msg_prob)
else:
print "MOTION STOPPED! Recording Stopped!"
logging.info("Recording Stopped! %s" % fileName)
try:
cam.stop_preview()
cam.stop_recording()
except Exception, Argument:
logging.error(Argument)
sms.sendSMS(num1,msg_prob)
sms.sendSMS(num2,msg_prob)
else:
try:
print "Sending Mail Notification..."
subprocess.call("mail -s 'Motion Detected' [email protected] < /home/pi/message.txt", shell=True)
print "Uploading the footage to Dropbox..."
dropboxUpload(fileName)
except Exception, Argument:
logging.error(Argument)
sms.sendSMS(num1,msg_prob)
sms.sendSMS(num2,msg_prob)
else:
print "Sending SMS Notification..."
sms.sendSMS(num1,msg_warn)
sms.sendSMS(num2,msg_warn)
subprocess.call("mail -s 'Motion Detected' [email protected] < /home/pi/message.txt", shell=True)
print "Process Completed!"