guowenxue
2019-06-25 0c6d37d6eb4eae9a91c7433adc7e76ba54c67c0f
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
#!/usr/bin/python
#-*- coding: utf-8 -*-
 
import time
import signal
import RPi.GPIO as GPIO
 
# I/O Pin connected to Physical PIN#16, BCM code pin number is 20 and wPi pin number is 28
INFR_PIN=20
 
def sig_proc(signum, frame):
    print("Catch stop signal and program exit now...")
    exit()
 
signal.signal(signal.SIGINT, sig_proc)
signal.signal(signal.SIGTERM, sig_proc)
 
def init():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(INFR_PIN, GPIO.IN)
    pass
 
def detct():
    while True:
        if GPIO.input(INFR_PIN) == True:
            print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+"  Someone is closing!"
        else:
            print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+"  No one nearby!"
        time.sleep(1) 
init()
detct()
GPIO.cleanup()