After several hours of running the code, get the following error:
can't open device: Too many open files
Mi code is:
import signal
import os
from module.MFRC522 import MFRC522
from module.pinos import PinoControle
import RPi.GPIO as GPIO
import time
import sys
import Basedatos
import webcam
import correo
import subprocess
##import emergencia
import MySQLdb
#Impor tar modulos r e que r ido s
from time import strftime, localtime
import datetime
#from unidecode import unidecode
##from MFRC522 import MFRC522
##from pinos import PinoControle
class Nfc522(object):
pc = PinoControle()
MIFAREReader = None
RST1 = 22 #GPIO
RST2 = 22 #GPIO
SPI_DEV0 = '/dev/spidev0.0'
SPI_DEV1 = '/dev/spidev0.1'
def obtem_nfc_rfid(self, autenticacao=False):
try:
self.MIFAREReader = MFRC522(self.RST1, self.SPI_DEV0)
## while True:
(status, TagType) = self.MIFAREReader.MFRC522_Request(self.MIFAREReader.PICC_REQIDL)
(status, uid) = self.MIFAREReader.MFRC522_Anticoll()
if status == self.MIFAREReader.MI_OK:
## print "Ganesh 1"
gid1 = self.obtem_tag(self.MIFAREReader, status, uid, autenticacao)
## return gg
## print "GID1:" + str(gg)
else:
self.pc.atualiza(self.RST1, self.pc.baixo())
## print "GID1: No"
gid1 = 0
except Exception as e:
print e
try:
self.MIFAREReader = MFRC522(self.RST2, self.SPI_DEV1)
#while True:
(status, TagType) = self.MIFAREReader.MFRC522_Request(self.MIFAREReader.PICC_REQIDL)
(status, uid) = self.MIFAREReader.MFRC522_Anticoll()
if status == self.MIFAREReader.MI_OK:
gid2= self.obtem_tag(self.MIFAREReader, status, uid, autenticacao)
## print "GID2:" + str(ggg)
else:
self.pc.atualiza(self.RST2, self.pc.baixo())
## print "GID2: No"
gid2=0
## return None
except Exception as e:
print e
#finally:
#self.MIFAREReader.fecha_spi()
return gid1,gid2
def obtem_tag(self, MIFAREReader, status, uid, autenticacao):
global TagID
try:
if autenticacao:
# Chave padrão para a autenticação
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
MIFAREReader.MFRC522_SelectTag(uid)
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
if status == MIFAREReader.MI_OK:
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
print "Erro na autenticação!"
return None
tag_hexa = ''.join([str(hex(x)[2:4]).zfill(2) for x in uid[:-1][::-1]]) #Returns in hexadecimal
return int(tag_hexa.upper(), 16) #Returns in decimal
except Exception as e:
print e
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "trl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
def ledencendido():
GPIO.output(5, True)
def inicializarGpio():
GPIO.setwarnings(False)#Secolocanl a sadve rte nc i asen f a l s o
#
GPIO.setmode(GPIO.BCM)#Mododet rabajoesGPIO.BOARD d i f e r e n t edebcm
GPIO.setup(16,GPIO.OUT)#Colocae lpin11comos a l i d a
GPIO.setup(17,GPIO.OUT)#Colocae lpin13comos a l i d a
GPIO.setup(18,GPIO.OUT)#Colocae lpin15comos a l i d a
GPIO.setup(19,GPIO.OUT)
GPIO.setup(20,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
nfc = Nfc522()
continue_reading = True
print "\nEsperando Tag\n---------------"
GPIO.cleanup()
while continue_reading:
global TagID
TagID=0
print_opt = 0
gid1,gid2 = nfc.obtem_nfc_rfid()
inicializarGpio()
ledencendido()
doc = open('emergencia.txt', 'r+')# Abre el doc
doc.seek(0,0)
emer = doc.read(1)
doc.close #cierra el doc
if (emer == '1'):#Sil
print('Estado de Emergencia')
else:
if gid1!=0:
print "ID del primer Lector es:" + str(gid1)
TagID=str(gid1)
usuario= Basedatos.Validar_Acceso(TagID)
print (usuario)
if (usuario == None):#Siusuarioesnulo
GPIO.output(13, True)
GPIO.output(16, True)
Basedatos.Insertar_Alerta(TagID)
print('Acceso Denegado')#Indique
time.sleep(1)
GPIO.output(13, False)
GPIO.output(16, False)
else:
print('Bienvenido')#Imprimabienvenido
GPIO.output(13, True)
GPIO.output(17, True)
GPIO.output(24, True)
time.sleep(3)
GPIO.output(13, False)
GPIO.output(17, False)
GPIO.output(24, False)
Basedatos.Insertar_Acceso(TagID)#Ing re s ee laccesoenl abase dedatos
print_opt = 1
if gid2!=0:
print "ID del segundo Lector es:" + str(gid2)
TagID=str(gid2)
usuario2= Basedatos.Validar_Acceso(TagID)
print (usuario2)
if (usuario2 == None):#Siusuarioesnulo
GPIO.output(13, True)
GPIO.output(16, True)
Basedatos.Insertar_Alerta(TagID)
print('Acceso Denegado')#Indique
time.sleep(1)
GPIO.output(13, False)
GPIO.output(16, False)
else:
print('Saliendo')
GPIO.output(13, True)
GPIO.output(17, True)
GPIO.output(24, True)
time.sleep(3)
GPIO.output(13, False)
GPIO.output(17, False)
GPIO.output(24, False)
Basedatos.Insertar_Acceso(TagID)#
print_opt = 1
if print_opt==1:
print "\nEsperando Tag\n---------------\n"
time.sleep(1)