title: RFID scanner doesn't scan afterwards
body: <p>I am very new to coding and I'm working on this project that I have no idea why is it not running as expected. I am sorry if I'm not posting this correctly because this is my first time posting on stackoverflow. Pardon my English if there's any point where I didn't explain it correctly.</p>
  <p>This is a project supposed to be a security system one, where we work with RFID Scanner, Camera, LED, Servo and PIR Sensor.</p>
  <p>The conditions are as, when the time is 7am to 11pm, the pir sensor would be off and when time is 11pm onwards to 7am in the morning, the PIR sensor and camera will be turned on. Where during anytime, as long as the number of people in the house is = 0 (determined by the RFID), the camera and PIR Sensor would turn on too.</p>
  <p>I haven't got a chance to even test out my code's logic, because when I tried to run the code, an error occurs at the loop part. &quot;Scan a registered card to skip registration&quot; would print and &quot;checkpoint&quot; would print, but afterward the code just stops there. It is supposed to be listening to the RFID scanner to read for scans, but when I tapped my card on the RFID scanner there was no response. And my code just dies there. As I'm very new to coding and python, I don't know where the errors could be. Would appreciate the help given!!</p>
  <pre><code>import RPi.GPIO as GPIO
  from time import sleep
  import sys
  import requests
  from datetime import datetime
  from mfrc522 import SimpleMFRC522 #RFID
  import os
  import pygame, sys
  import smtplib
  from email.mime.text import MIMEText
  from email.mime.multipart import MIMEMultipart
  from email.mime.image import MIMEImage
  from pygame.locals import *
  import pygame.camera
  
  width = 640
  height = 480
  
  #initialise pygame
  pygame.init()
  pygame.camera.init()
  cam = pygame.camera.Camera(&quot;/dev/video0&quot;,(width,height))
  no=0
  
  
  GPIO.setmode(GPIO.BCM)
  GPIO.setwarnings(False)
  
  GPIO.setup(24,GPIO.OUT) #LED
  GPIO.setup(26,GPIO.OUT) #Servo
  GPIO.setup(17,GPIO.IN) #PIR Sensor
  
  current_time =datetime.now() #Current time
  
  sleep(5)
  
  reader = SimpleMFRC522() #RFID reader
  auth = []
  in_house =[]
  p = open(&quot;presentlist.txt&quot;,&quot;a+&quot;)
  p = open(&quot;presentlist.txt&quot;,&quot;r+&quot;)
  present = 0
  
  PIR_state =0 
  
  PWM=GPIO.PWM(26,50) #Set 50hz PWM output at GPIO26
  PWM.start(12) #To edit to actual 3oclock (Locked)
  
  
  fromaddr = &quot;pythonprogramtest123@gmail.com&quot; # change the email address accordingly
  toaddr = &quot;pythonprogramtest123@gmail.com&quot; # Same email
  #password: abcdefgpython
  mail = MIMEMultipart()
   
  mail['From'] = fromaddr
  mail['To'] = toaddr
  mail['Subject'] = &quot;Attachment&quot;
  body = &quot;Unusual movement detected&quot;
  
  
  print('Door Locked') 
  print(&quot;Hold card near the reader to register it in the database.&quot;)
  print(&quot;Registration up to 10 cards&quot;)
  print(&quot;Scan a registered card to skip registration&quot;)
  
  for count in range (0,11):
      print(&quot;checkpoint1&quot;)
      id = reader.read_id()
      id = str(id)
      f = open(&quot;authlist.txt&quot;, &quot;a+&quot;)
      f = open(&quot;authlist.txt&quot;, &quot;r+&quot;)
      print(&quot;checkpoint2&quot;)
      if f.mode == &quot;r+&quot;:
          auth=f.read()
      if id not in auth:
          f.write(id)
          f.write('\n')
          f.close()
          pos = auth.count('\n')
          print(&quot;New card with UID&quot;, id, &quot;detected: registered as entry #&quot;,pos)
          count = count +1
      else:
          number = auth.split('\n')
          pos = number.index(id)
          print(&quot;Card with UID&quot;, id, &quot;already registered as entry #&quot;, pos)
          count = 11
          break
      
  
  while (True):
      id = reader.read_id()
      id = str(id)
      f = open(&quot;authlist.txt&quot;, &quot;a+&quot;)
      f = open(&quot;authlist.txt&quot;, &quot;r+&quot;)
      if current_time.hour &gt;= 7 or current_time.hour &lt;= 23 or present == 0: #awake or nobody in house
          #RFID is on
          if f.mode == &quot;r+&quot;:
              auth=f.read()
          if id in auth and id not in in_house:
              p.write(id)
              p.write('\n')
              p.close()
              present = in_house.count('\n')
              print(&quot;Recognition successful \n Allowing Access&quot;)
              PWM.start(1) #Servo to actual 12 o clock position (Unlocking)
              print(&quot;Door unlocked for 30 seconds. Tap once more to change to 1 minute&quot;)
              if f.mode == &quot;r+&quot;:
                  auth=f.read()
              if id in auth:
                  print(&quot;Unlocking door for 1 minute&quot;)
                  sleep(60)
                  PWM.start(12) #To edit to actual 3oclock (Locked)
                  f.close()
          
              else: #no tap detected
                  sleep(30)
                  PWM.start(12) #To edit to actual 3oclock (Locked)
                  f.close()
                  break
  
          elif id in auth and id in in_house:
              p.remove(id)
              print(&quot;Recognition successful \n Allowing Access&quot;)
              PWM.start(1) #Servo to actual 12 o clock position (Unlocking)
              print(&quot;Door unlocked for 30 seconds. Tap once more to change to 1 minute&quot;)
              if f.mode == &quot;r+&quot;:
                  auth=f.read()
              if id in auth:
                  print(&quot;Unlocking door for 1 minute&quot;)
                  sleep(60)
                  PWM.start(12) #To edit to actual 3oclock (Locked)
                  f.close()
          
              else: #no tap detected
                  sleep(30)
                  PWM.start(12) #To edit to actual 3oclock (Locked)
                  f.close()
                  break
          else:
              print(&quot;Card not recognised \n Access Denied&quot;)
             
          break
      
      elif current_time.hour &lt; 7 or current_time.hour &gt; 23: #sleeping
          id = reader.read_id()
          id = str(id)
          f = open(&quot;authlist.txt&quot;, &quot;a+&quot;)
          f = open(&quot;authlist.txt&quot;, &quot;r+&quot;)
          #RFID is on
          if f.mode == &quot;r+&quot;:
              auth=f.read()
          if id in auth:
              print(&quot;Recognition successful \n Allowing Access&quot;)
              PWM.start(1) #Servo to actual 12 o clock position (Unlocking)
              print(&quot;Door unlocked for 30 seconds. Tap once more to change to 1 minute&quot;)
              if f.mode == &quot;r+&quot;:
                  auth=f.read()
              if id in auth:
                  print(&quot;Unlocking door for 1 minute&quot;)
                  sleep(60)
                  PWM.start(12) #To edit to actual 3oclock (Locked)
          
              else: #no tap detected
                  sleep(30)
                  PWM.start(12) #To edit to actual 3oclock (Locked)
                  break
          else:
              print(&quot;Card not recognised \n Access Denied&quot;)
          break
  
  
          #Camera
          if GPIO.input(17): #read a HIGH i.e. motion is detected
  
              #LED Blink
              GPIO.output(24,1)
              sleep(1)
              GPIO.output(24,0)
              sleep(1)
              GPIO.output(24,1)
              sleep(1)
              GPIO.output(24,0)
              
              if PIR_state==0:
                  print('detected HIGH i.e. motion detected')
                  PIR_state=1
                  
                  #Camera
                  cam.start() #start
                  image = cam.get_image() #take pic
                  cam.stop()
                  no = no + 1
                  saved_img=pygame.image.save(windowSurfaceObj,'picture' + &quot;{:03d}&quot;.format(no) + '.jpg')
  
                  #Thingspeak
                  print(&quot;Updating Thingpseak&quot;)
                  resp = requests.get(&quot;GET https://api.thingspeak.com/update?api_key=PQ44ZG5JR49ABODY&amp;field1=0&quot;)
                  #Email
                  def sendMail(data):
                      mail.attach(MIMEText(body, 'plain'))
                      print (data)
                      dat='%s.jpg'%data
                      print (dat)
                      attachment = open(dat, 'rb')
                      image=MIMEImage(attachment.read())
                      attachment.close()
                      mail.attach(image)
                      server = smtplib.SMTP('smtp.gmail.com', 587)
                      server.starttls()
                      server.login(fromaddr, &quot;abcdefgpython&quot;)
                      text = mail.as_string()
                      server.sendmail(fromaddr, toaddr, text)
                      server.quit()
                  
          else: #read a LOW i.e. no motion is detected
              if PIR_state==1:
                  print('detected LOW i.e. no motion detected')
                  PIR_state=0
          sleep(1)
          print(&quot;...&quot;)
          break
      
      sleep(2)
  
  
      
           
  
  
  
  
           
  
  
      
           
  
  
  
  
          
  </code></pre>