2015-04-07 13:50:29 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
|
2015-04-07 14:23:15 -04:00
|
|
|
import socket
|
|
|
|
##################
|
|
|
|
# #
|
|
|
|
# Default values #
|
|
|
|
# #
|
|
|
|
##################
|
|
|
|
|
|
|
|
CONTROLLER = socket.gethostname()
|
|
|
|
SERVER = "localhost"
|
|
|
|
RFID_PATH = "/dev/ttyUSB0"
|
2015-04-22 17:07:21 -04:00
|
|
|
YELLOW_LED = 7
|
2015-04-07 13:50:29 -04:00
|
|
|
RED_LED = 11
|
|
|
|
GREEN_LED = 13
|
|
|
|
DOOR_STRIKE = 15
|
|
|
|
|
2015-04-07 14:03:58 -04:00
|
|
|
|
|
|
|
|
2015-04-07 14:23:15 -04:00
|
|
|
import os
|
2015-04-07 13:50:29 -04:00
|
|
|
####################
|
|
|
|
# #
|
2015-04-07 14:23:15 -04:00
|
|
|
# Read doorrc file #
|
2015-04-07 13:50:29 -04:00
|
|
|
# #
|
|
|
|
####################
|
|
|
|
|
2015-04-09 15:41:08 -04:00
|
|
|
for conf in [ "/etc/doorsrc", "./doorsrc" ]:
|
2015-04-07 14:23:15 -04:00
|
|
|
if os.path.exists(conf):
|
|
|
|
with open(conf) as f:
|
2015-04-09 15:41:08 -04:00
|
|
|
exec(compile(f.read(), "doorsrc", 'exec'))
|
2015-04-07 14:23:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-07 13:50:29 -04:00
|
|
|
import atexit
|
|
|
|
import RPi.GPIO as GPIO
|
2015-04-07 14:23:15 -04:00
|
|
|
####################
|
|
|
|
# #
|
|
|
|
# Set up GPIO Pins #
|
|
|
|
# #
|
|
|
|
####################
|
2015-04-07 13:50:29 -04:00
|
|
|
|
|
|
|
GPIO.setmode(GPIO.BOARD)
|
|
|
|
atexit.register(GPIO.cleanup)
|
|
|
|
|
|
|
|
GPIO.setup(DOOR_STRIKE, GPIO.OUT)
|
|
|
|
GPIO.setup(RED_LED, GPIO.OUT)
|
|
|
|
GPIO.setup(GREEN_LED, GPIO.OUT)
|
2015-04-22 17:07:21 -04:00
|
|
|
GPIO.setup(YELLOW_LED, GPIO.OUT)
|
2015-04-07 13:50:29 -04:00
|
|
|
|
2015-04-22 17:13:45 -04:00
|
|
|
|
2015-04-23 10:28:04 -04:00
|
|
|
OFF = GPIO.LOW
|
|
|
|
ON = GPIO.HIGH
|
2015-04-07 13:50:29 -04:00
|
|
|
|
|
|
|
|
2015-04-23 08:41:50 -04:00
|
|
|
def unlock_door():
|
|
|
|
print("Door is unlocked");
|
2015-04-22 17:13:45 -04:00
|
|
|
GPIO.output(DOOR_STRIKE, ON)
|
|
|
|
GPIO.output(RED_LED, OFF)
|
|
|
|
GPIO.output(GREEN_LED, ON)
|
|
|
|
GPIO.output(YELLOW_LED, OFF)
|
2015-04-07 13:50:29 -04:00
|
|
|
|
|
|
|
#When door is closed and locked
|
2015-04-23 10:18:58 -04:00
|
|
|
def lock_door():
|
|
|
|
print("Door is locked");
|
2015-04-22 17:13:45 -04:00
|
|
|
GPIO.output(DOOR_STRIKE, OFF)
|
|
|
|
GPIO.output(RED_LED, ON)
|
|
|
|
GPIO.output(GREEN_LED, OFF)
|
|
|
|
GPIO.output(YELLOW_LED, OFF);
|
|
|
|
|
2015-04-22 21:10:57 -04:00
|
|
|
# Turn all LEDs on
|
|
|
|
def leds_on():
|
|
|
|
GPIO.output(RED_LED, ON)
|
|
|
|
GPIO.output(GREEN_LED, ON)
|
|
|
|
GPIO.output(YELLOW_LED, ON)
|
|
|
|
|
|
|
|
# Turn all LEDs off
|
|
|
|
def leds_off():
|
|
|
|
GPIO.output(RED_LED, OFF)
|
|
|
|
GPIO.output(GREEN_LED, OFF)
|
|
|
|
GPIO.output(YELLOW_LED, OFF)
|
|
|
|
|
2015-04-23 10:18:58 -04:00
|
|
|
lock_door()
|
2015-04-07 13:50:29 -04:00
|
|
|
|
2015-04-07 14:03:58 -04:00
|
|
|
|
|
|
|
|
2015-04-07 14:23:15 -04:00
|
|
|
import time
|
|
|
|
from urllib import request
|
2015-04-07 13:50:29 -04:00
|
|
|
#############################################
|
|
|
|
# #
|
|
|
|
# Verify a key with openings.workantile.com #
|
|
|
|
# #
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
# Blocks for 5 seconds before resetting the door
|
|
|
|
def verify_key(key):
|
2015-04-22 17:13:45 -04:00
|
|
|
GPIO.output(YELLOW_LED, ON);
|
2015-04-07 14:30:19 -04:00
|
|
|
with request.urlopen(SERVER + ("/%s" % key)) as f:
|
|
|
|
if f.read().decode() == "OK":
|
2015-04-23 08:41:50 -04:00
|
|
|
unlock_door()
|
2015-04-07 14:30:19 -04:00
|
|
|
time.sleep(5)
|
2015-04-23 10:18:58 -04:00
|
|
|
lock_door()
|
2015-04-07 13:50:29 -04:00
|
|
|
|
2015-04-07 14:27:02 -04:00
|
|
|
|
|
|
|
#python-pyserial package. Not sure we need this. Grabbed based on
|
|
|
|
#http://allenmlabs.blogspot.se/2013/01/raspberry-pi-parallax-rfid-reader.html
|
|
|
|
import serial
|
|
|
|
###################
|
|
|
|
# #
|
|
|
|
# Run RFID Reader #
|
|
|
|
# #
|
|
|
|
###################
|
|
|
|
|
|
|
|
RFID_SERIAL = serial.Serial(RFID_PATH, 2400, timeout=1)
|
|
|
|
|
2015-04-09 09:33:27 -04:00
|
|
|
def read_rfid():
|
2015-04-07 14:05:50 -04:00
|
|
|
string = RFID_SERIAL.read(12)
|
2015-04-07 13:50:29 -04:00
|
|
|
if len(string) == 0:
|
2015-04-07 14:27:02 -04:00
|
|
|
print("No tag read")
|
2015-04-09 09:33:27 -04:00
|
|
|
#continue
|
2015-04-07 13:50:29 -04:00
|
|
|
else:
|
2015-04-09 09:33:27 -04:00
|
|
|
key = string[1:11].decode() #exclude start x0A and stop x0D bytes
|
2015-04-07 14:05:50 -04:00
|
|
|
print(key)
|
|
|
|
verify_key(key)
|
2015-04-07 14:24:55 -04:00
|
|
|
RFID_SERIAL.flushInput() # ignore errors, no data
|
2015-04-09 09:33:27 -04:00
|
|
|
|
|
|
|
|
2015-04-22 21:10:57 -04:00
|
|
|
def blink_leds():
|
|
|
|
for i in range(5):
|
|
|
|
if (i % 2) == 0:
|
|
|
|
leds_on()
|
|
|
|
else:
|
|
|
|
leds_off()
|
|
|
|
time.sleep(1)
|
2015-04-23 10:18:58 -04:00
|
|
|
lock_door()
|
2015-04-22 21:10:57 -04:00
|
|
|
|
2015-04-22 20:44:08 -04:00
|
|
|
|
|
|
|
def main():
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
read_rfid()
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2015-04-23 10:18:58 -04:00
|
|
|
lock_door()
|
2015-04-22 21:10:57 -04:00
|
|
|
blink_leds()
|
2015-04-22 20:44:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": main()
|