-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfid_logger.py
executable file
·63 lines (51 loc) · 1.36 KB
/
rfid_logger.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
#!/usr/bin/env python3
from time import sleep
from dotenv import dotenv_values
from mfrc522 import SimpleMFRC522
import RPi.GPIO as GPIO
import mariadb
reader = SimpleMFRC522()
config = dotenv_values(".env")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT, initial=GPIO.LOW)
def _get_db_cxn():
"""
:return: a connection to the database
"""
conn_params = {
"user": config["DB_USER"],
"password": config["DB_PWORD"],
"host": "localhost",
"database": "checkin",
"autocommit": True,
}
try:
connection = mariadb.connect(**conn_params)
except Exception as e:
print("Error connecting to MariaDB Platform: ", e)
# exit(1)
return connection
def flash_green():
GPIO.output(16, GPIO.HIGH)
sleep(4)
GPIO.output(16, GPIO.LOW)
sleep(7)
def execute_query(query):
"""
:param query: the query to execute
"""
connection = _get_db_cxn()
cursor = connection.cursor()
try:
cursor.execute(query)
flash_green()
except Exception as e:
print("Error executing query: ", e)
# exit(1)
cursor.close()
connection.close()
# main loop; read RFID tags and insert into database
while True:
current_user_id, discard = reader.read()
execute_query(f"INSERT INTO timestamps (UID) VALUES ({current_user_id})")