Introduction :
In this article we going to implement one IOT base project Python Code For Attendance System Using Arduino With RFID. In the previous article we clearly see How to RFID data display from serial port to laptop or pc screen using python software.
so in this project we are use RFID base system for student attendance purposed.
pre- requirement :
- Arduino IDE
- Arduino UNO
- RFID- RC522
- Python compiler
- Installation of XAMPP server:
In this project we are using phpMyAdmin that is the web interface of MySQL server
If you want to installed it then use command: sudo apt-get install phpMyAdmin.
You need to connect ethernet shield first as shown and then you need to make the connection of RFID with arduino uno. if you dont’t know about this then please goes to arduino official site and refer it. (link is available below)
Table of Contents
Detailed Pin connection for RFID and Arduino uno
MOSI 11 51
MISO 12 50
SCK 13 52
RST 9 9
SDA(SS) 4/10 4/53
VCC 3.3 v 3.3v
GND GND GND
Remember: IRQ is not nee to connect.
Step 1: First you need to make the all physical connection between Arduino UNO and RFID reader.
Step 2: Then you need to install supporting Arduino library
You need to install one library for rfid rc522
Step 3: Compile all program (python /Arduino)
Arduino code (Python Code For Attendance System Using Arduino With RFID)
#include <SPI.h>
#include<MFRC522.h>
#include <Ethernet.h>
#define SS_PIN 10
#define RST_PIN 9
#define No_Of_Card 3
MFRC522 rfid(SS_PIN,RST_PIN);
MFRC522::MIFARE_Key key;
byte id[No_Of_Card][4]={
{142,76,58,42}, //RFID NO-1
{112,224,72,84}, //RFID NO-2
{151,94,80,84} //RFID NO-3
};
byte id_temp[3][3];
byte i;
int j=0;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
for(byte i=0;i<6;i++)
{
key.keyByte[i]=0xFF;
}
}
//------------------------------------------------------------------------------
/* Infinite Loop */
void loop()
{int m=0;
if(!rfid.PICC_IsNewCardPresent())
return;
if(!rfid.PICC_ReadCardSerial())
return;
for(i=0;i<4;i++)
{
id_temp[0][i]=rfid.uid.uidByte[i];
delay(50);
}
for(i=0;i<No_Of_Card;i++)
{
if(id[i][0]==id_temp[0][0])
{
if(id[i][1]==id_temp[0][1])
{
if(id[i][2]==id_temp[0][2])
{
if(id[i][3]==id_temp[0][3])
{
Serial.println("Card detected:");
for(int s=0;s<4;s++)
{
Serial.print(rfid.uid.uidByte[s]);
}
Serial.print(" ");
Sending_To_db();
j=0;
rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); return;
}
}
}
}
else
{j++;
if(j==No_Of_Card)
{
Serial.println("Card detected:");
for(int s=0;s<4;s++)
{
Serial.print(rfid.uid.uidByte[s]);
}
Serial.print(" ");
Sending_To_db();
j=0;
}
}
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
void Sending_To_db()
{
if(j!=No_Of_Card)
{
Serial.print('1');
Serial.print(" ");
}
else
{
Serial.print('0');
Serial.print(" ");
}
}
Python Code For Attendance System Using Arduino With RFID
Python code to push data to phpMyAdmin
import serial
import MySQLdb
import time
#establish connection to MySQL. You'll have to change this for your database.
dbConn = MySQLdb.connect("localhost","root","","Your DB Name") or die ("could not connect to database")
#open a cursor to the database
cursor = dbConn.cursor()
device = 'COM6' #this will have to be changed to the serial port you are using
try:
print "Trying...",device
arduino = serial.Serial(device, 9600)
except:
print "Failed to connect on",device
while True:
time.sleep(1)
try:
data=arduino.readline()
print data
pieces=data.split(" ")
try:
cursor=dbConn.cursor()
cursor.execute("""INSERT INTO <your table name> (ID,Member_ID,allowed_members) VALUES (NULL,%s,%s)""", (pieces[0],pieces[1]))
dbConn.commit()
cursor.close()
except MySQLdb.IntegrityError:
print "failed to insert data"
finally:
cursor.close()
except:
print "Processing"
Congrats Finally our project code know ready
Summary : In this article we saw Python Code For Attendance System Using Arduino With RFID project. so about this project code if you have any problem then please inform me
Resource : Arduino official site
BEST OF LUCK!!!
Also see following python arduino project
automatic dino game using python arduino
python code for smartphone controlled mouse