Introduction: Simple Supermarket App Using RFID RC-522 and Arduino Mega
Its good to see you guys again here on my another tutorial , here i'm going to help you to create a simple supermarket application using RFID RC-522 and Arduino with Processing to create simple GUI.
Note:don't run Arduino serial monitor while running processing code because port conflict will occur as both have to use the same port
You Need:
- Arduino Mega or Arduino Uno (I used Mega)
- RFID-RC522
- 7 male to female jumper wires
- Some ID cards (optional)
- RFID Library (Must , Link Below)
- Wamp server
- Processing IDE 2.2.1 (don't use greater than that)
- BezierSQLib-0.2.0 library for processing (Download link below)
Then Download the Below RFID library and Add it to your Arduino IDE by clicking Sketch->Include Library-> Add .Zip Library in file menu
Step 1: Setting Up Arduino and RFID RC-522 (Physical Connection)
simply connect the arduino with RFID-RC522 as shown in the above image .
Warning : supply only 3.3V otherwise module will burn out
Pin Out for Uno/Nano and Mega
RC522 MODULE Uno/Nano MEGA
SDA D10 D9 SCK D13 D52 MOSI D11 D51 MISO D12 D50 IRQ N/A N/A GND GND GND RST D9 D8 3.3V 3.3V 3.3V
Step 2: Arduino Code.,
Copy the Below code then upload it on your Arduino
/*
PINOUT: RC522 MODULE Uno/Nano MEGA SDA D10 D9 SCK D13 D52 MOSI D11 D51 MISO D12 D50 IRQ N/A N/A GND GND GND RST D9 D8 3.3V 3.3V 3.3V */ /* Include the standard Arduino SPI library */ #include /* Include the RFID library */ #include/* Define the DIO used for the SDA (SS) and RST (reset) pins. */ #define SDA_DIO 9 #define RESET_DIO 8
/* Create an instance of the RFID library */ RFID RC522(SDA_DIO, RESET_DIO); int reader=0;
void setup() { Serial.begin(9600); /* Enable the SPI interface */ SPI.begin(); /* Initialise the RFID reader */ RC522.init(); }
void loop() { /* Temporary loop counter */ byte i;
/* Has a card been detected? */ if (RC522.isCard()) { /* If so then get its serial number */ RC522.readCardSerial();
/* Output the serial number to the UART */ for(i = 0; i <= 2; i++) { Serial.print(RC522.serNum[i],DEC); //Serial.print(RC522.serNum[i],HEX); } Serial.print(","); Serial.print(reader++); Serial.println(); } delay(1000); }
Step 3: Setting Up MySQL
- Install Wamp server for MySQL and configure it to store data (
- Run wamp serveropen MySQL console
- select database
- Then create the table for your data
create table rfid(ID int(8),token int(1),Name varchar(20),Amount int(4));
Now view this link to learn how to get your RFID tag value then use the below code to insert data .don't forget to replace ID value with your RFID tag value
insert into rfid values(3756178,1,'Pencil',20);
use token value as 1 so that after reading the tag value for first time it will automatically changes to 2, don't use 0 for token value when reading card that is not inserted in DB it will assign 0 then display it as Unknown Card..
Step 4: Setting Up Processing IDE
- Download and Install the Processing IDE 2.2.1
- Extract the above given ZIP to MyDocuments/Processing/Libraries
- Now open processing IDE and check the library is installed correctly or not as in the above image
- Then Copy the below code to processing and name it of your own
import de.bezier.data.sql.*;
import processing.serial.*; //import java.math.BigInteger;// created 2005-05-10 by fjenett // updated fjenett 20080605
MySQL dbconnection; String s=" "; int Wheight=700; int Wwidth=1200; long ID; int token; int Amount; int Total=0;
String[] a={"NULL","NULL"}; int end = 10; // the number 10 is ASCII for linefeed (end of serial.println), later we will look for this to break up individual messages String serial; // declare a new string called 'serial' . A string is a sequence of characters (data type know as "char") Serial port; String curr,prev,Name; PFont f;
void setup() { //size( Wwidth,Wheight ); size(700,500); f=createFont("Arial",24,true); // this example assumes that you are running the // mysql server locally (on "localhost"). // // replace --username--, --password-- with your mysql-account. // String user = "root"; String pass = ""; // name of the database to use // String database = "IOT_Database"; // name of the table that will be created String table = ""; // connect to database of server "localhost" dbconnection = new MySQL( this, "localhost", database, user, pass ); port = new Serial(this, Serial.list()[0], 9600); // initializing the object by assigning a port and baud rate (must match that of Arduino) port.clear(); // function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino serial = port.readStringUntil(end); // function that reads the string from serial port until a println and then assigns string to our string variable (called 'serial') serial = null; } void draw() { background(255); textFont(f,24); fill(0); text("Total Amount Rs:",400,400); text(Total,585,400); data(); while (port.available() > 0) { //as long as there is data coming from serial port, read it and store it serial = port.readStringUntil(end); } if (serial != null) { prev=curr; curr=a[1]; a = split(serial, ','); //a new array (called 'a') that stores values into separate cells (separated by commas specified in your Arduino program) if((curr).equals(prev)) { // } else { //println("curr",curr); //println("Prev",prev); function(); } } }
void function() { if ( dbconnection.connect() ) { // now read it back out // dbconnection.query( "SELECT * from rfid where ID="+a[0]+"" ); while (dbconnection.next()) { ID = dbconnection.getInt("ID"); token = dbconnection.getInt("token"); Amount = dbconnection.getInt("Amount"); } if(token==0) { println("Ok"); textFont(f,54); fill(255,0,0,160); text("Unknown Item Detected",50,300); delay(2000); } else if(token==1) { Total=Total+Amount; dbconnection.query("update rfid set token=2 where ID="+a[0]+"" ); println("Ok"); textFont(f,24); fill(255,0,0,160); //text("Item Added",10,30); delay(1000); } else if(token==2) { Total=Total-Amount; dbconnection.query("update rfid set token=1 where ID="+a[0]+"" ); println("Ok"); textFont(f,24); fill(255,0,0,160); //text("Item Removed",10,30); delay(1000); } else { } dbconnection.close(); } else { // connection failed ! } }
void data() { int position=100; if ( dbconnection.connect() ) { dbconnection.query( "SELECT * from rfid where token=2"); while (dbconnection.next()) { Name = dbconnection.getString("Name"); Amount = dbconnection.getInt("Amount"); textFont(f,24); fill(0,0,255,160); text(Name,10,position); fill(0,0,0,160); text(Amount,215,position); position=position+30; } } dbconnection.close(); }
Step 5: Executing the Program
Run the program by clicking the run button don't close the popup window closing will stop execution and below query to view stored data in MySQL...
Step 6: Conclusion
I would like to thank you for reading my tutorial. I would appreciate if you found it useful and drop a like (favorite) or ask me anything as it keeps me motivated to do these instructables. feel free to ask any questions that you need to know...
Happy Coding Arduino...
15 Comments
Tip 4 years ago
can you convert this program into simple attendance system? if you its BETTER i swear ,..
Reply 4 years ago
sure dude will upload in days and will surely notify you through message...
Reply 4 years ago
can arduino support multiple RFID reader?
Reply 4 years ago
sure it will but not all the arduino boards support multiple RFID reader. RFID reader uses master slave connection so if an arduino board have 1 master and 2 slave ability you can connect 2 Reader at a same time. for example with arduino mega you can connect up to 4 RFID but in arduino uno only one is possible.
Reply 4 years ago
thanks bro :)
4 years ago
good work but if a scan the rfid card that registered in database and then i scan the unregistered tag, it simply read and generate some amout, and when i put the unregistered tag first it works, how can i fix this?
plss hellp
Reply 4 years ago
will you able to send me the code and screenshots of the error via mail
check your inbox you will find my mail ID there/...
Reply 4 years ago
its now running,:)
i just want to ask how to read the card one by one?like new card scan new information will display and if another card scan the previous display will update by the new card.. thanks. :)
Reply 4 years ago
its simple dude you can do it by using iterations. refer my another tutorial https://www.instructables.com/id/Interfacing-RFID-RC522-With-Arduino-MEGA-a-Simple-/
Reply 4 years ago
thanks bro, its so much help for me
Reply 4 years ago
welcome dude..
Reply 4 years ago
there is error, what does it mean? i cant find
Reply 4 years ago
the number of values send from arduino and number of variables in processing must be same in count..
for example
if you are sending a single value like 12 the array in processing should be a[0], if the values are 12,43 the array declared should be a[1] that is you must use all the array space you declared.
Question 4 years ago on Step 1
can i use any other rfid reader other than rc522?
Answer 4 years ago
Sure you can but you have to focus on master slave connection....