stafett/registrer.py

48 lines
1.6 KiB
Python

from mfrc522 import SimpleMFRC522#rfid reader library
from requests import post, get #make post & get requests
from RPi import GPIO
import json#json
from mysql import connector#mysql
import datetime
def timestamp():
#https://stackoverflow.com/questions/2150739/iso-time-iso-8601-in-python
return [datetime.datetime.now().astimezone().replace(microsecond=0).isoformat()]
with open('./settings.json') as json_settings:#open json file with settings
settings = json.loads(json_settings.read())#read the json to a python object
reader = SimpleMFRC522()
localdb = connector.connect(#connect to a local mariadb database
host=settings['localdb']['host'],
user=settings['localdb']['user'],
password=settings['localdb']['password'],
database=settings['localdb']['database']
)
cursor = localdb.cursor()
cursor.execute("SHOW GLOBAL VARIABLES LIKE 'time_zone';")
cursor.execute("SHOW GLOBAL VARIABLES LIKE 'system_time_zone';")
cursor.execute("SELECT NOW();")
localdb.commit()
print(mycursor.fetchall())
get(str(settings['url'] + '/stafett/time.php'))
try:
while True:
id, text = reader.read()#read rfid
rfid = [str(id) + str(text)]
print(rfid)
cursor.execute("INSERT INTO tidtabell (rfid) VALUES(%s);", (rfid))
#^sends the rfid into the database, the time column is automatically CURRENT_TIMESTAMP whitch is the same as NOW();
localdb.commit()#write the sql
post(settings['url'], {'rfid': rfid[0], 'timestamp': timestamp()})#send POST with rfid to kvadda.no
except KeyboardInterrupt:#ctrl + c
GPIO.cleanup()#clean up the gpio
raise#exit program