Skip to content

Instantly share code, notes, and snippets.

@vmwsree
Last active November 11, 2019 08:38
Show Gist options
  • Save vmwsree/d31e289a765119ba66d1d491a029fbe9 to your computer and use it in GitHub Desktop.
Save vmwsree/d31e289a765119ba66d1d491a029fbe9 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# @Author: Vivek Mohan
# @Date: 2019-11-03 16:23:13
# @Last Modified by: Vivek Mohan
# @Last Modified time: 2019-11-04 16:00:25
#!/usr/bin/env python
import signal
import time
import sys
import os
from pirc522 import RFID
from bson.objectid import ObjectId
from pymongo import MongoClient
from dotenv import load_dotenv
from neopixel import *
def colorWipe(strip, color, wait_ms=50):
"""Wipe color across display a pixel at a time."""
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
time.sleep(wait_ms / 1000.0)
strip = Adafruit_NeoPixel(1,18)
strip.begin()
import RPi.GPIO as GPIO
# Card Register
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
def toggle():
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, True)
time.sleep(int(7))
GPIO.output(11, False)
clint = MongoClient('localhost', 27017)
db = clint.test_database
cards = db.cards
load_dotenv("/home/pi/.env")
door_id = os.environ.get("DOORID", "door01")
centre = os.environ.get("CENTRE_SLUG", "connaught-place")
delay = os.environ.get("DELAY", 7)
run = True
rdr = RFID()
util = rdr.util()
util.debug = True
def end_read(signal,frame):
global run
print("\nCtrl+C captured, ending read.")
run = False
rdr.cleanup()
sys.exit()
signal.signal(signal.SIGINT, end_read)
while run:
colorWipe(strip, Color(0, 0, 255))
rdr.wait_for_tag()
(error, data) = rdr.request()
if not error:
print("\nDetected: " + format(data, "02x"))
(error, uid) = rdr.anticoll()
if not error:
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
code=str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3])
try:
loc_rf_box = cards.find_one({'name': door_id})
except e as Exception:
print("It executed , have to see how long it took to execute")
if loc_rf_box == None:
print("Not Registered")
time.sleep(1)
else:
if (str(code) in loc_rf_box["cards"]):
colorWipe(strip, Color(255, 0, 0))
toggle()
else:
colorWipe(strip, Color(0, 255, 0))
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment