Introduction: Simple Supermarket App Using RFID RC-522 and Arduino Mega

About: Dreamer...

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:

  1. Arduino Mega or Arduino Uno (I used Mega)
  2. RFID-RC522
  3. 7 male to female jumper wires
  4. Some ID cards (optional)
  5. RFID Library (Must , Link Below)
  6. Wamp server
  7. Processing IDE 2.2.1 (don't use greater than that)
  8. 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

  1. Install Wamp server for MySQL and configure it to store data (
  2. Run wamp serveropen MySQL console
  3. select database
  4. 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

  1. Download and Install the Processing IDE 2.2.1
  2. Extract the above given ZIP to MyDocuments/Processing/Libraries
  3. Now open processing IDE and check the library is installed correctly or not as in the above image
  4. 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...