-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorbell.py
90 lines (74 loc) · 2.63 KB
/
doorbell.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
#coding=utf-8
#author: Fredrik Tell
from pushbullet import Pushbullet
import wget
import shutil
import time
from datetime import datetime
import RPi.GPIO as GPIO
import requests
import threading
import sys
#Hikvision settings
cameraImageUrl = "http://admin:[email protected]/ISAPI/Streaming/channels/101/picture"
cameraTwoWayUrl = "http://admin:[email protected]/ISAPI/System/TwoWayAudio/channels/1/audioData"
#Pushbullet settings
message = "Visitor at the front door"
outputFolder = "/home/pi/picture.jpg"
#Google Home settings
google_home_url = 'http://127.0.0.1/Notify?Doorbell'
#Pushbullet settings
pb_channel_name = "lerkil"
pb_api_key = "o.9NgIOV3yKFrMALNXKwFlPjlU2s2T02yd"
pb = Pushbullet(pb_api_key)
pb_channel = pb.get_channel(pb_channel_name)
#Raspberry Pi settings
GPIO_Pin = 5
GPIO_Steady = 2000
GPIO.setmode(GPIO.BOARD)
GPIO.setup(GPIO_Pin, GPIO.IN)
#Send sound to Google Home
def GoogleHomeNotice():
try:
requests.get(google_home_url)
except:
print("ERROR - Can't send sound to Google Home")
#Send Pushbullet with image
def PushbulletNotice():
#Get image from camera and replace the old one
filename = wget.download(cameraImageUrl, out=outputFolder)
shutil.move(filename, outputFolder)
filename = outputFolder
#Upload image to Pushbullet server
with open(filename, "rb") as pic:
file_data = pb.upload_file(pic, "picture.jpg")
#Get current time
now = datetime.now() # current date and time
date_time = now.strftime("%Y-%m-%d %H:%M")
#Publish notification
pb_channel.push_file(body=date_time, title=message, **file_data)
#Send sound to Hikvision IP camera
def DoorbellSpeakerNotice():
headers = {'content-type': 'application/binary'}
data = open('output.ulaw')
requests.put(cameraTwoWayUrl, headers=headers, data=data)
#Button event
def Doorbell(channel):
#Google Home notice
GoogleHomeNoticeThread = threading.Thread(target=GoogleHomeNotice)
GoogleHomeNoticeThread.start()
#Pushbullet notice
PushbulletNoticeThread = threading.Thread(target=PushbulletNotice)
PushbulletNoticeThread.start()
#DoorbellSpeaker notice
DoorbellSpeakerNoticeThread = threading.Thread(target=DoorbellSpeakerNotice)
DoorbellSpeakerNoticeThread.start()
#Add event detection
GPIO.add_event_detect(GPIO_Pin, GPIO.FALLING, callback=Doorbell, bouncetime=GPIO_Steady)
print("Doorbell is started")
while True:
try:
time.sleep(10)
except KeyboardInterrupt:
print("Thanks for this time!")
sys.exit()