I have 4 threads running in my application using the method Thread:
1) tkinter gui main thread
2) USB reader for reading rfid cards thread
3) Flask application
4) Function waiting for a response from 2 exit buttons.
My issue is that GPIO.input(4) randomly gets activated to be false thereby unlocking the door. Is there anyway i can improve the code such that the door will stay lock unless the buttons are pressed. The door is controlled by GPIO 12.
My code is as below:
Code: Select all
#!/usr/bin/env python3
import sys
import os
import config
from flask import Flask, jsonify, request, render_template, send_from_directory, Response
import pymysql.cursors
from threading import Thread
import threading
import time
import RPi.GPIO as GPIO
import json
from evdev import InputDevice
from select import select
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(12, GPIO.LOW)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
app = Flask(__name__)
@app.route('/unlock/',methods=['GET','POST'])
def opendoor():
if request.content_type=='application/json':
print (request.json)
if request.method=='POST':
data = request.get_json()
doorid = data.get('doorid')
if doorid==somevalue
try:
GPIO.output(12, GPIO.HIGH)
time.sleep(5)
GPIO.output(12,GPIO.LOW)
return jsonify("Access Granted - Welcome.")
except:
return jsonify("Access Denied - You are not authorized to access the premise, please contact the administrator")
else:
return jsonify("Failed - please contact the administrator")
else:
return jsonify("Application needs to reboot")
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico',mimetype='image/vnd.microsoft.icon')
try:
# python 2
import Tkinter as tk
import ttk
except ImportError:
# python 3
import tkinter as tk
from tkinter import ttk
class Fullscreen_Window:
def __init__(self):
self.show_idle()
t = Thread(target=self.flask_main)
t2 = Thread(target=self.listen_rfid)
t.daemon = True
t2.daemon = True
t.start()
t2.start()
self.buttonpress =self.tk.after(100,self.button)
def flask_main(self):
app.run(parameters for flask)
def button(self):
if GPIO.input(5)==0:
print ("exit button is pressed")
GPIO.output(12, GPIO.HIGH)
time.sleep(5)
GPIO.output(12, GPIO.LOW)
time.sleep(.1)
if GPIO.input(4)==0:
print("receptionist button is pressed")
GPIO.output(12, GPIO.HIGH)
time.sleep(5)
GPIO.output(12, GPIO.LOW)
time.sleep(.1)
self.buttonpress = self.tk.after(100, self.button)
def show_idle(self):
...gui set up...
def listen_rfid(self):
global accessLogId
keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
dev = InputDevice('/dev/input/event0')
rfid_presented = ""
while True:
r,w,x = select([dev], [], [])
for event in dev.read():
if event.type==1 and event.value==1:
if event.code==28:
connect to database and perform validation.
else:
rfid_presented += keys[ event.code ]
if __name__ == '__main__':
w = Fullscreen_Window()
w.tk.mainloop()