-
Notifications
You must be signed in to change notification settings - Fork 15
/
rpi_sip_doorbell.py
executable file
·91 lines (81 loc) · 3.37 KB
/
rpi_sip_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
#!/usr/bin/env python3
# FemtoSIP -- A minimal SIP client
# Copyright (C) 2017-2018 Andreas Stöckel
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Command line arguments
import argparse
parser = argparse.ArgumentParser(
description='A program for the Raspberry PI calling the given SIP phone ' +
'number whenever a falling edge on the given GPIO pin is ' +
'detected.')
parser.add_argument('--gateway', required=True,
help='Hostname or IP address of the SIP server')
parser.add_argument('--port', default=5060, type=int,
help='Port of the SIP server (default 5060)')
parser.add_argument('--user', required=True,
help='Username used for authentication at the SIP server')
parser.add_argument('--password', default='',
help='Password used in conjunction with the user for authentication ' +
'at the SIP server. (default '')')
parser.add_argument('--displayname', default=None, help='Alter the displayed ' +
'caller name. (defaults to SIP configuration)')
parser.add_argument('--call', required=True,
help='Phone number of the endpoint that will be called.')
parser.add_argument('--delay', default=15.0, type=float,
help='Pause in seconds until the call is canceled (default 15.0)')
parser.add_argument('--gpio', default=27, type=int,
help='GPIO pin which is configured as input (default 27)')
args = parser.parse_args()
# Setup logging for this program
import logging
logger = logging.getLogger('rpi_sip_doorbell')
logging.basicConfig(level=logging.INFO)
# Setup the specified GPIO pin as input
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(args.gpio, GPIO.IN)
# Setup an asynchronous callback
got_event = { 'value': False }
def gpio_event_callback(_):
# Do some software low-pass filtering
value = 0.0
for i in range(0, 32):
if not GPIO.input(args.gpio):
value += 1.0
value *= 0.9
time.sleep(5e-3)
if value > 8.0:
logger.info('Door gong triggered.')
got_event['value'] = True
GPIO.add_event_detect(args.gpio, GPIO.FALLING,
callback=gpio_event_callback,
bouncetime = 200)
# Setup the SIP client
import femtosip
sip = femtosip.SIP(args.user, args.password, args.gateway, args.port, args.displayname)
# Loop eternally and trigger a call whenever an event is detected
import time
try:
logger.info('rpi_sip_doorbell.py ready')
while True:
time.sleep(0.1)
if got_event['value']:
logger.info('Detected door ring event, initiating SIP call.')
sip.call(args.call, args.delay)
logger.info('SIP call ended.')
got_event['value'] = False
except KeyboardInterrupt:
logger.info('Program interrupted, exiting')