Commit 5cb86003 authored by matteo perini's avatar matteo perini

test con due lettori rfid

parent 7b6127f7
import time
import os
import RPi.GPIO as GPIO
#from mfrc522 import SimpleMFRC522
#import sqlite3
from termcolor import colored
from my_lib import registra_cliente, registra_prodotto, cerca_cliente_per_prodotto, scarica_prodotto, lista_clienti, scarica_cliente, lista_prodotti, cerca_cliente_per_tessera
#from my_lib import NFC
def mostra_menu():
os.system('cls' if os.name == 'nt' else 'clear') # questa riga serve per cancellare il terminale
......@@ -26,35 +30,35 @@ def mostra_menu():
return 0 #serve per uscire dalla funzione
elif int(n) == 1:
registra_cliente()
time.sleep(3)
input()
mostra_menu()
elif int(n) == 2:
registra_prodotto()
time.sleep(3)
input()
mostra_menu()
elif int(n) == 3:
scarica_prodotto()
time.sleep(3)
input()
mostra_menu()
elif int(n) == 4:
lista_clienti()
time.sleep(3)
input()
mostra_menu()
elif int(n) == 5:
cerca_cliente_per_prodotto()
time.sleep(3)
input()
mostra_menu()
elif int(n) == 6:
scarica_cliente()
time.sleep(3)
input()
mostra_menu()
elif int(n) == 7:
lista_prodotti()
time.sleep(3)
input()
mostra_menu()
elif int(n) == 8:
cerca_cliente_per_tessera()
time.sleep(3)
input()
mostra_menu()
else:
print("ok")
......@@ -62,12 +66,20 @@ def mostra_menu():
mostra_menu()
else:
print("Numero selezionato non valido. Riprova!") #se il numero non è tra quelli previsti
time.sleep(3) #aspetto tre secondi
input() #aspetto tre secondi
mostra_menu() #faccio ripartire la funzione del menù
else:
print("Scelta non corretta. Deve essere un numero. Riprova!") #se l'immissione non è un numero
time.sleep(3) #aspetto tre secondi
input() #aspetto tre secondi
mostra_menu() #faccio ripartire la funzione del menù
if __name__=="__main__": #se sto chiamando il programma principale
mostra_menu() # avvia la funzione per mostrare il menù
\ No newline at end of file
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(25, GPIO.OUT)
#global nfc
#nfc = NFC()
#nfc.addBoard("rfid_cassa",25) #GPIO 25 -> pin 22
mostra_menu() # avvia la funzione per mostrare il menù
\ No newline at end of file
from gpiozero import LED
from time import sleep
led = LED(27)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
from mfrc522 import SimpleMFRC522, MFRC522
import sqlite3
import time
from gpiozero import LED
#import RPi.GPIO as GPIO
#from mfrc522 import SimpleMFRC522
import spidev
class NFC():
def __init__(self, bus=0, device=0, spd=1000000):
self.reader = SimpleMFRC522()
self.close()
#self.bus = bus
self.boards = {}
self.bus = bus
self.device = device
self.spd = spd
def reinit(self):
self.reader.READER.spi = spidev.SpiDev()
self.reader.READER.spi.open(self.bus, self.device)
self.reader.READER.spi.max_speed_hz = self.spd
self.reader.READER.MFRC522_Init()
def close(self):
self.reader.READER.spi.close()
def addBoard(self, rid, pin):
self.rid = rid
self.boards[rid] = pin
def selectBoard(self, rid):
if not rid in self.boards:
print("readerid " + rid + " not found")
return False
for loop_id in self.boards:
GPIO.output(self.boards[loop_id], loop_id == rid)
return True
def read(self, rid):
if not self.selectBoard(rid):
return None
self.reinit()
cid, val = self.reader.read_no_block()
self.close()
return cid, val
def readd(self):
if not self.selectBoard(self.rid):
return None
self.reinit()
cid, val = self.reader.read_no_block()
self.close()
return cid, val
def write(self, rid, value):
if not self.selectBoard(rid):
return False
self.reinit()
self.reader.write_no_block(value)
self.close()
return True
# if __name__ == "__main__":
# nfc = NFC()
# nfc.addBoard("reader1",5)
# nfc.addBoard("reader2",6)
# data = nfc.read("reader1")
# nfc.write("reader2",data)
def cerca_cliente_per_prodotto():
reader = SimpleMFRC522()
......@@ -35,24 +110,51 @@ def cerca_cliente_per_prodotto():
else:
print("Prodotto non associato a nessun cliente")
def cerca_cliente_per_tessera():
class PatchedSimpleMFRC522(SimpleMFRC522):
def __init__(self, **kwargs):
self.READER = MFRC522(**kwargs)
def leggi_rfid(testo,pin):
if pin==5:
lettore = LED(5)
nonlettore = LED(6)
lettore.on()
nonlettore.off()
elif pin==6:
lettore = LED(6)
nonlettore = LED(5)
lettore.on()
nonlettore.off()
else:
print("error")
return 1
reader = SimpleMFRC522()
#reader = PatchedSimpleMFRC522(bus=1, pin_rst=pin)
print()
try:
print(testo)
#id,text = reader.read()
data = None
#led_verde.on()
while data == None:
id,data = reader.read()
#id, data = nfc.read()
sleep(0.2)
#print(id)
#print(text)
except:
pass
finally:
GPIO.cleanup()
return int(id)
def cerca_cliente_per_tessera():
con = sqlite3.connect("mydb.db")
cur = con.cursor()
def leggi_cliente():
try:
print("Ciao, avvicina il tag del cliente al ricevitore!")
id,text = reader.read()
print(id)
#print(text)
except:
pass
finally:
GPIO.cleanup()
return int(id)
n = leggi_cliente()
n = leggi_rfid("Ciao, avvicina il tag del cliente al ricevitore!",5)
database = cur.execute('SELECT * FROM persone WHERE n_card=?;',(n,))
yy = database.fetchall()
......@@ -119,7 +221,7 @@ def registra_prodotto():
def scarica_prodotto():
reader = SimpleMFRC522()
#reader = SimpleMFRC522()
con = sqlite3.connect("mydb.db")
cur = con.cursor()
......@@ -157,11 +259,11 @@ def scarica_prodotto():
#c = input("Quale e il tuo cognome?")
#codice_cliente = leggi_cliente()
#time.sleep(2)
codice_prodotto = leggi_prodotto()
codice_prodotto = leggi_rfid("Ciao, avvicina il prodotto al ricevitore!",6)
try:
cur.execute("DELETE FROM prodotti WHERE n_rfid=?;",(codice_prodotto,))
#print("ciao")
print("Il prodotto col codice : ", codice_prodotto, " è stato correttamente smaltito.")
#print("Ciao ", codice_cliente , " abbiamo registrato il prodotto numero : ", codice_prodotto)
con.commit()
cur.close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment