-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
195 lines (162 loc) · 4.21 KB
/
reader.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python
#
# RFID READER
# version 0.9
# r.grokett
#
# Watches for RFID card.
# When detected, looks for ID# in CARDS list
# If ID# is found, turns on LED,buzzer,relay
#
# Note that relay can be either GPIO driven type or I2C driven type
#
# TODO List:
# Get card IDs from external source (file, DB, etc.)
# Add User name to card ID
# Write Date/Time, Card ID, Name to log when detected
# Restart program on Raspi boot up
import smbus
import time
import serial
import sys, signal, os, datetime
import RPi.GPIO as GPIO
# LIST VALID CARD IDs HERE
CARDS = [
'1234567890',
'E0043DBB52'
]
# DEFAULTS
ON = 1
OFF = 0
# SET RELAY TYPE (default is GPIO)
I2C = OFF # Turn this ON if using an I2C Relay
# else it uses GPIO Relay
# Set up GPIO Pins (change as needed)
LED = 23
BUZZER = 24
RELAY = 25
BUTTON = 4
# BEGIN INITIALIZATION
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)
GPIO.setup(BUZZER, GPIO.OUT)
GPIO.setup(RELAY, GPIO.OUT)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Get I2C bus
bus = smbus.SMBus(1)
i2c_addr = 0x41 # I2C Address of Relay
# GO TO MAIN
############################
# METHODS
############################
# TURN ON/OFF LED
def led(value):
print("led()")
if (value):
GPIO.output(LED, GPIO.HIGH)
print("LED state is : HIGH")
else:
GPIO.output(LED, GPIO.LOW)
print("LED state is : LOW")
# TURN ON/OFF BUZZER
def buzzer(value):
print("buzzer()")
if (value):
GPIO.output(BUZZER, GPIO.HIGH)
print("BUZZER state is : HIGH")
else:
GPIO.output(BUZZER, GPIO.LOW)
print("BUZZER state is : LOW")
# TURN ON/OFF GPIO RELAY
def relay(value):
print("relay()")
if (value):
GPIO.output(RELAY, GPIO.HIGH)
print("RELAY state is : HIGH")
else:
GPIO.output(RELAY, GPIO.LOW)
print("RELAY state is : LOW")
# TURN ON/OFF I2C RELAY
def i2c_relay(value):
print("i2c_relay()")
if not I2C:
return
if (value):
# PCA9536_R11 address, i2c_addr(65)
# Select output port register, 0x01(01)
bus.write_byte_data(i2c_addr, 0x01, 0x01)
print("Relay-1 state is : HIGH")
else:
# PCA9536_R11 address, i2c_addr(65)
# Select output port register, 0x01(01)
bus.write_byte_data(i2c_addr, 0x01, 0x00)
print("Relay-1 state is : LOW")
# UNLOCK DOOR
def unlock_door():
print("unlock_door()")
led(ON)
buzzer(ON)
relay(ON)
i2c_relay(ON)
# LOCK DOOR
def lock_door():
print("lock_door()")
led(OFF)
buzzer(OFF)
relay(OFF)
i2c_relay(OFF)
# EXIT
def signal_handler(signal, frame):
print("Exiting!")
relay(ON) # NOTE! UNLOCKS DOOR ON FAILURE/EXIT (Jurassic Park!)
i2c_relay(ON)
GPIO.cleanup()
ser.close()
sys.exit(0)
############################
# MAIN
############################
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
# Select configuration register, 0x03(03)
# 0x00(00) Set all pins as OUTPUT
if (I2C):
bus.write_byte_data(i2c_addr, 0x03, 0x00)
time.sleep(0.5)
ser = 0
# Selects which version of Raspi you have
if (GPIO.RPI_REVISION == 3):
# For Pi3 use
print("This is a Pi 3")
ser = serial.Serial('/dev/ttyS0', 9600, timeout=1)
else:
# For Pi2, B+, Zero use
print("This is a Pi B+, 2, or Zero")
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
# LOCK THE DOOR ON STARTUP
lock_door()
# MAIN LOOP
while True:
buffer = ser.read(12)
if len(buffer) == 0:
print("Please wave a tag")
else:
card_id = buffer[1:11] # Strip header/trailer
print("Card ID : ", card_id)
if (card_id in CARDS):
print("FOUND CARD ID: ", card_id)
unlock_door()
time.sleep(5)
lock_door()
else:
print("CARD NOT IN LIST!")
# EXIT THE BUILDING
# Add an indoor button
if not GPIO.input(BUTTON):
print("Button Pressed")
unlock_door()
time.sleep(5)
lock_door()
time.sleep(0.1)
# END WHILE
# END MAIN