-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIR_Motion_Camera_Email.py
64 lines (47 loc) · 1.46 KB
/
PIR_Motion_Camera_Email.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
import RPi.GPIO as GPIO
import time
import datetime
import picamera
import os
import telebot
camera = picamera.PiCamera()
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN) #PIR
GPIO.setup(24, GPIO.OUT) #BUzzer
'''
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
'''
CHAT_ID = 1234567 # target chat_id AKA your user id, ideally
BOT_TOKEN = 'your bot token here'
bot = telebot.TeleBot(BOT_TOKEN)
COMMASPACE = ', '
def send_message(image):
try:
with open(image, 'rb') as f:
bot.send_photo(CHAT_ID, f)
print("Message sent!")
except:
print("Unable to send the message. Error: ", sys.exc_info()[0])
raise
try:
time.sleep(2) # to stabilize sensor
while True:
##Timeloop
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
if GPIO.input(23):
##If loop
GPIO.output(24, True)
time.sleep(0.5) #Buzzer turns on for 0.5 sec
print("Motion Detected at {}".format(st))
##Adds timestamp to image
camera.capture('image_Time_{}.jpg'.format(st))
image = ('image_Time_{}.jpg'.format(st))
send_message(image)
time.sleep(2)
GPIO.output(24, False)
time.sleep(5) #to avoid multiple detection
time.sleep(0.1) #loop delay, should be less than detection delay
except:
GPIO.cleanup()