-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_numpad.py
96 lines (61 loc) · 1.85 KB
/
read_numpad.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
from time import sleep # this lets us have a time delay (see line 12)
from datetime import datetime
from threading import Thread
print(GPIO.VERSION)
# In[2]:
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
# In[3]:
channel = 24
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # GPIO Assign mode
edge = "FALLING"
# In[4]:
def set_edge(specify):
global edge
edge = specify
return
# In[5]:
def get_edge():
global edge
return edge
# In[6]:
def reset_event_detect(channel):
edge = get_edge()
GPIO.remove_event_detect(channel)
if edge == "RISING":
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback, bouncetime=100)
else:
GPIO.add_event_detect(channel, GPIO.FALLING, callback=my_callback, bouncetime=100)
return
# In[7]:
def my_callback(channel):
now = datetime.now().strftime("%D %H:%M:%S")
print("{0} detected on pin {1}".format(get_edge(),channel))
if get_edge() == "FALLING":
msg = "Door unlocked at {}".format(now)
print(msg)
client.publish("Pad_Event", msg)
set_edge("RISING")
reset_event_detect(channel)
else:
msg = "Door locked at {}".format(now)
print(msg)
client.publish("Pad_Event", msg)
set_edge("FALLING")
reset_event_detect(channel)
# In[8]:
client = mqtt.Client()
client.on_connect = on_connect
# In[9]:
client.connect("localhost", 1883, 60)
GPIO.add_event_detect(channel, GPIO.FALLING, callback=my_callback, bouncetime=100)
client.loop_forever()