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

test con due lettori rfid

parent 7b6127f7
import time import time
import os import os
import RPi.GPIO as GPIO
#from mfrc522 import SimpleMFRC522
#import sqlite3
from termcolor import colored 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 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(): def mostra_menu():
os.system('cls' if os.name == 'nt' else 'clear') # questa riga serve per cancellare il terminale os.system('cls' if os.name == 'nt' else 'clear') # questa riga serve per cancellare il terminale
...@@ -26,35 +30,35 @@ def mostra_menu(): ...@@ -26,35 +30,35 @@ def mostra_menu():
return 0 #serve per uscire dalla funzione return 0 #serve per uscire dalla funzione
elif int(n) == 1: elif int(n) == 1:
registra_cliente() registra_cliente()
time.sleep(3) input()
mostra_menu() mostra_menu()
elif int(n) == 2: elif int(n) == 2:
registra_prodotto() registra_prodotto()
time.sleep(3) input()
mostra_menu() mostra_menu()
elif int(n) == 3: elif int(n) == 3:
scarica_prodotto() scarica_prodotto()
time.sleep(3) input()
mostra_menu() mostra_menu()
elif int(n) == 4: elif int(n) == 4:
lista_clienti() lista_clienti()
time.sleep(3) input()
mostra_menu() mostra_menu()
elif int(n) == 5: elif int(n) == 5:
cerca_cliente_per_prodotto() cerca_cliente_per_prodotto()
time.sleep(3) input()
mostra_menu() mostra_menu()
elif int(n) == 6: elif int(n) == 6:
scarica_cliente() scarica_cliente()
time.sleep(3) input()
mostra_menu() mostra_menu()
elif int(n) == 7: elif int(n) == 7:
lista_prodotti() lista_prodotti()
time.sleep(3) input()
mostra_menu() mostra_menu()
elif int(n) == 8: elif int(n) == 8:
cerca_cliente_per_tessera() cerca_cliente_per_tessera()
time.sleep(3) input()
mostra_menu() mostra_menu()
else: else:
print("ok") print("ok")
...@@ -62,12 +66,20 @@ def mostra_menu(): ...@@ -62,12 +66,20 @@ def mostra_menu():
mostra_menu() mostra_menu()
else: else:
print("Numero selezionato non valido. Riprova!") #se il numero non è tra quelli previsti 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ù mostra_menu() #faccio ripartire la funzione del menù
else: else:
print("Scelta non corretta. Deve essere un numero. Riprova!") #se l'immissione non è un numero 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ù mostra_menu() #faccio ripartire la funzione del menù
if __name__=="__main__": #se sto chiamando il programma principale if __name__=="__main__": #se sto chiamando il programma principale
#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ù 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 import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522 from mfrc522 import SimpleMFRC522, MFRC522
import sqlite3 import sqlite3
import time 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(): def cerca_cliente_per_prodotto():
reader = SimpleMFRC522() reader = SimpleMFRC522()
...@@ -35,16 +110,38 @@ def cerca_cliente_per_prodotto(): ...@@ -35,16 +110,38 @@ def cerca_cliente_per_prodotto():
else: else:
print("Prodotto non associato a nessun cliente") print("Prodotto non associato a nessun cliente")
def cerca_cliente_per_tessera(): class PatchedSimpleMFRC522(SimpleMFRC522):
reader = SimpleMFRC522() def __init__(self, **kwargs):
con = sqlite3.connect("mydb.db") self.READER = MFRC522(**kwargs)
cur = con.cursor()
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
def leggi_cliente(): reader = SimpleMFRC522()
#reader = PatchedSimpleMFRC522(bus=1, pin_rst=pin)
print()
try: try:
print("Ciao, avvicina il tag del cliente al ricevitore!") print(testo)
id,text = reader.read() #id,text = reader.read()
print(id) data = None
#led_verde.on()
while data == None:
id,data = reader.read()
#id, data = nfc.read()
sleep(0.2)
#print(id)
#print(text) #print(text)
except: except:
pass pass
...@@ -52,7 +149,12 @@ def cerca_cliente_per_tessera(): ...@@ -52,7 +149,12 @@ def cerca_cliente_per_tessera():
GPIO.cleanup() GPIO.cleanup()
return int(id) return int(id)
n = leggi_cliente() def cerca_cliente_per_tessera():
con = sqlite3.connect("mydb.db")
cur = con.cursor()
n = leggi_rfid("Ciao, avvicina il tag del cliente al ricevitore!",5)
database = cur.execute('SELECT * FROM persone WHERE n_card=?;',(n,)) database = cur.execute('SELECT * FROM persone WHERE n_card=?;',(n,))
yy = database.fetchall() yy = database.fetchall()
...@@ -119,7 +221,7 @@ def registra_prodotto(): ...@@ -119,7 +221,7 @@ def registra_prodotto():
def scarica_prodotto(): def scarica_prodotto():
reader = SimpleMFRC522() #reader = SimpleMFRC522()
con = sqlite3.connect("mydb.db") con = sqlite3.connect("mydb.db")
cur = con.cursor() cur = con.cursor()
...@@ -157,11 +259,11 @@ def scarica_prodotto(): ...@@ -157,11 +259,11 @@ def scarica_prodotto():
#c = input("Quale e il tuo cognome?") #c = input("Quale e il tuo cognome?")
#codice_cliente = leggi_cliente() #codice_cliente = leggi_cliente()
#time.sleep(2) #time.sleep(2)
codice_prodotto = leggi_prodotto() codice_prodotto = leggi_rfid("Ciao, avvicina il prodotto al ricevitore!",6)
try: try:
cur.execute("DELETE FROM prodotti WHERE n_rfid=?;",(codice_prodotto,)) 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) #print("Ciao ", codice_cliente , " abbiamo registrato il prodotto numero : ", codice_prodotto)
con.commit() con.commit()
cur.close() 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