This commit is contained in:
Filip Nilsen 2022-03-16 12:58:53 +01:00
commit 79483becc0
5 changed files with 135 additions and 0 deletions

11
reader.py Normal file
View File

@ -0,0 +1,11 @@
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
while True:
id, text = reader.read()
print(id)
print(text)
except KeyboardInterrupt:
GPIO.cleanup()
raise

2
readme.md Normal file
View File

@ -0,0 +1,2 @@
# Raspberry Pi
Python kode for å lese rfid med RFID-RC522 og sende det til en server med HTTP POST.

47
registrer.py Normal file
View File

@ -0,0 +1,47 @@
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

66
registrer_lag.py Normal file
View File

@ -0,0 +1,66 @@
from tkinter import *
from tkinter import ttk
from requests import post
def get_rfid(rfid):
return
def main():
global LagNavn, Bedrift, rfid, Lagleder, Telefon, Deltagere, Brukernavn, Passord
root = Tk()
notebook = ttk.Notebook(root)
notebook.pack()
tab0 = ttk.Frame(notebook)
notebook.add(tab0, text='Registrereing')
ttk.Label(tab0, text='LagNavn').grid(row=1)
LagNavn = ttk.Entry(tab0)
LagNavn.grid(column=1, row=1)
ttk.Label(tab0, text='Bedrift').grid(row=2)
Bedrift = ttk.Entry(tab0)
Bedrift.grid(column=1, row=2)
ttk.Label(tab0, text='RFID').grid(row=3)
rfid = ttk.Entry(tab0)
rfid.grid(column=1, row=3)
ttk.Button(tab0, command=get_rfid(rfid), text='Les fra pinne').grid(column=2, row=3)
ttk.Label(tab0, text='Lagleder').grid(row=4)
Lagleder = ttk.Entry(tab0)
Lagleder.grid(column=1, row=4)
ttk.Label(tab0, text='Telefon').grid(row=5)
Telefon = ttk.Entry(tab0)
Telefon.grid(row=5, column=1)
ttk.Label(tab0, text='Deltagere').grid(row=6)
Deltagere = ttk.Entry(tab0)
Deltagere.grid(row=6, column=1)
tab1 = ttk.Frame(notebook)
ttk.Label(tab1, text='Brukernavn').grid()
Brukernavn = ttk.Entry(tab1)
Brukernavn.grid(column=1, row=0)
ttk.Label(tab1, text='Passord').grid(row=1)
Passord = ttk.Entry(tab1, show='*')
Passord.grid(column=1, row=1)
notebook.add(tab1, text='Servertilkobling')
ttk.Button(tab0, text='Registrer', command=send).grid(row=7, column=1)
root.mainloop()
def send():
global LagNavn, Bedrift, rfid, Lagleder, Telefon, Deltagere, Brukernavn, Passord
r = post('http://localhost/backend/registrer_lag.php', data={'LagNavn':LagNavn.get(), 'Bedrift':Bedrift.get(), 'Kortnummer':rfid.get(), 'Lagleder':Lagleder.get(), 'Telefon':Telefon.get(), 'Deltagere':Deltagere.get(), 'Brukernavn':Brukernavn.get(), 'Passord':Passord.get()})
print(r.status_code)
print(r.headers)
print(r.text)
r.close()
if __name__ == '__main__':
main()

9
settings.json Normal file
View File

@ -0,0 +1,9 @@
{
"url": "http://192.168.0.110/stafett/mottakkort.php",
"localdb":{
"host":"localhost",
"user":"root",
"password":"password",
"database":"stafett"
}
}