forked from DolceVii/RFID_Rasp_Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainscript.py
More file actions
111 lines (95 loc) · 3.05 KB
/
Copy pathmainscript.py
File metadata and controls
111 lines (95 loc) · 3.05 KB
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
# !/usr/bin/python3
import RPi.GPIO as GPIO
import sqlite3
import time
import serial
import datetime
global database
global who
global username
global data
global runok
runok = 0
database = "/var/www/html/database/raspberry.db"
username = "Άγνωστος"
who = "Άγνωστο"
now = datetime.datetime.now().strftime('%d-%m-%Y %H:%M:%S')
DoorRelay = 21
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(DoorRelay, GPIO.OUT)
def main():
def rserial():
global who
global runok
try:
ser=serial.Serial("/dev/ttyUSB0",9600)
ser.baudrate = 9600
who = str(ser.readline().decode("utf-8").strip())
who = who.lstrip("b")
who = who.strip("'")
who = who.rstrip("\\r\\n")
who = who.rstrip(" ")
ser.close()
runok == 0
except Exception:
print("Error USB chek usb connection and try again")
runok = 1
def wsql():
global who
global username
global runok
if username == "None":
username = "Άγνωστος"
try:
connection = sqlite3.connect(database)
cursor = connection.cursor()
val = ('1', who, username, now, data)
cursor.execute ("INSERT INTO data (door, who, UserName, day, isactive) VALUES (?, ?, ?, ?, ?)", (val))
connection.commit()
connection.close()
runok == 0
except Exception:
print("Error MySQL check Mysql server and try again")
runok = 1
def rsql():
global who
global data
global username
global runok
try:
connection = sqlite3.connect(database)
cursor = connection.cursor()
cursor.execute('SELECT count(*) FROM active WHERE activerfid = 1 AND userrfid = ?', (who,))
data = cursor.fetchone()[0]
cursor.execute('SELECT UserName FROM active WHERE userrfid = ?', (who,))
username = cursor.fetchone()
username = str(username)
username = username.replace("('","")
username = username.replace("',)","")
connection.close()
runok == 0
except Exception:
print("Erorr SQL connection check and try again")
runok = 1
while True:
global username
if runok == 0:
rserial()
if runok == 0:
rsql()
if runok == 0:
wsql()
if username == "None":
username = "Άγνωστος"
if data == 1:
print("The user: " + username + ", with ID: " + who + ", has access!")
GPIO.output(DoorRelay, GPIO.HIGH)
time.sleep(1)
GPIO.output(DoorRelay, GPIO.LOW)
else:
print("The user: " + username + ", with ID: " + who + ", has no access!")
GPIO.output(DoorRelay, GPIO.LOW)
time.sleep(3)
if __name__== "__main__":
main()