ESP32 RFID Live MySQL
ESP32 RFID Live MySQL
Aim: Understanding RFID tags and card reader and developing a project to show RFID tags
information Live on a web dashboard. Also, the card information should be stored in MySQL
database.
Assumption(s):
1. You already have gone through our ESP32 SPIFFS session and installed ESP32 Sketch Data
Upload plugin to your Arduino IDE. If not, please DO NOT proceed until you've done this.
Long gone are the days when people used to stand and wait in long checkout lines at the grocery
store. Thanks to the Radio Frequency IDentification (RFID) technology. With this RFID based walk-
through automatic checkout solution, you can fill up your cart and walk right out the door. No
longer will you have to wait as someone rings up each item in your cart one at a time. Instead, the
RFID tags attached to items will communicate with RFID reader that will detect every item in the
cart and ring each up almost instantly.
Panasonic and Trial Company, Inc. together conducted a demonstration experiment of the
industry's first "RFID based walk-through automatic checkout solution" featuring Trial's "RFID
tagging," which manages manufacturing and distribution data.
You can quickly watch the demo of this solution here: https://youtu.be/TF8HAhUN_p4
Transceiver: A transceiver is a device that can both transmit and receive signals
Transponder: A transponder is a component with a processor programmed to monitor
incoming signals and with a preprogrammed reply in the communication network
The transceiver and transponder are different and they are not interchangeable.
RFID reader device, the tag transmits digital data, usually an identifying inventory number, back
to the reader. This number can be used to inventory goods.
1. Passive tags are powered by energy from the RFID reader's interrogating radio waves.
2. Active tags are powered by a battery and thus can be read at a greater range from the RFID
reader; up to hundreds of meters.
Unlike a barcode, the tag doesn't need to be within the line of sight of the reader, so it may be
embedded in the tracked object. RFID is one method of automatic identification and data capture.
Thus we see RFID or Radio Frequency Identification system consists of two main components:
A Reader consists of a Radio Frequency module and an antenna which generates high frequency
electromagnetic field. On the other hand, the tag is usually a passive device, meaning it doesn’t
contain a battery. Instead it contains a microchip that stores and processes information, and an
antenna to receive and transmit a signal. You can understand this from below diagram:
Reading Information:
To read the information encoded on a tag, it is placed in close proximity to the Reader (does not
need to be within direct line-of-sight of the reader). A Reader generates an electromagnetic field
which causes electrons to move through the tag’s antenna and subsequently power the chip. See
below diagram:
The powered chip inside the tag then responds by sending its stored information back to the
reader in the form of another radio signal. This is called backscatter (In physics, backscatter (or
backscattering) is the reflection of waves, particles, or signals back to the direction from which
they came). The backscatter, or change in the electromagnetic/RF wave, is detected and
interpreted by the reader which then sends the data out to a computer or microcontroller.
Thus we can say that RFID refers to a technology whereby digital data encoded in RFID tags or
smart labels are captured by a reader via radio waves. RFID is similar to barcoding in that data from
a tag or label are captured by a device that stores the data in a database. RFID, however, has
several advantages over systems that use barcode asset tracking software. The most notable is
that RFID tag data can be read outside the line-of-sight, whereas barcodes must be aligned with
an optical scanner. Also an RFID tag can hold much more data about an item than a barcode can.
In addition, RFID tags are not susceptible to the damages that may be incurred by barcode labels,
like ripping and smearing.
RFID Applications:
Inventory management
Asset tracking
Personnel tracking
Controlling access to restricted areas
ID Badging
Supply chain management
The RC522 RFID module based on MFRC522 IC from NXP is one of the most inexpensive RFID
options that we can get online very cheap. This module usually comes with a RFID card tag and
RFID key tag having 1KB memory. And best of all, it can write a tag, so you can store your some
sort of secret message in it. The complete module looks like below:
The RC522 RFID Reader module is designed to create a 13.56MHz electromagnetic field that it
uses to communicate with the RFID tags (ISO 14443A standard tags). The reader can communicate
with a microcontroller over a 4-pin Serial Peripheral Interface (SPI) with a maximum data rate of
10Mbps. It also supports communication over I2C and UART protocols.
The module comes with an interrupt pin which can be connected to our microcontroller. It alerts
our microcontroller when a tag comes nearby. It is handy because instead of constantly asking the
RFID module “is there a card in view yet?” the module will alert the microcontroller when a tag
comes into its vicinity.
RC522 Features:
Integrated MF RC522 IC
13.56MHz RFID module
Page 208 of 221
-: Internet of Things – The IoT World :-
Supplies power for the module. This can be anywhere from 2.5 to 3.3
VCC volts. You can connect it to 3.3V output from your Raspi. Remember
connecting it to 5V pin will likely destroy your module!
This is an input for Reset and power-down. When this pin goes low, hard
power-down is enabled. This turns off all internal current sinks including
RST
the oscillator and the input pins are disconnected from the outside
world. On the rising edge, the module is reset.
This is an interrupt pin that can alert the microcontroller i.e. your
IRQ
ESP/Raspi when RFID tag comes into its vicinity.
This is the Serial Clock. It accepts clock pulses provided by the SPI bus
SCK
Master i.e. ESP/Raspi.
This pin acts as Chip Select when SPI interface is enabled, acts as Serial
Data Line when I2C is enabled and acts as serial data receiver when
SS/SDA/RX
UART is enabled. This pin is usually marked by encasing the pin in a
square so it can be used as a reference for identifying the other pins.
RFID-RC522 ESP32
SDA_PIN GPIO# 21
RST_PIN GPIO# 22
SCK_PIN GPIO# 18
MISO_PIN GPIO# 19
MOSI_PIN GPIO# 23
GND GND
VCC 3.3V
IRQ_PIN Leave blank
1. First and foremost we need to record some information into our RFID cards. So, open the
Arduino IDE, connect to the ESP32 Dev Module board and COM port. Mine is COM4. Copy our
code ESP32_RFID_Read_Write.ino and make sure that the function writingData() is
uncommented and readingData() is commented and save it. The function writingData() writes
data into RFID cards in the format IMH-xxxxxxxxxxxx. See below:
2. Upload the code into ESP32. Once uploaded, open the serial monitor, press the RESET button
on ESP32 to restart the program. When it asks to swipe the care, please swipe one of the card
and observe the data written into the card. You can use all your cards and write data into those.
The program will write different & unique data in every card. The data looks like IMH-
050102821664. See below results from this code:
3. Once all our cards are ready with data, we are good to proceed. Now copy our code
ESP32_RFID_Live_Status.ino, provide your WiFi information and save.
4. Click on Sketch => Show Sketch Folder. It will take you to the current sketch folder. There you
need to copy our "data" folder. See below:
6. The php code post-esp32-generic.php is the server side code which you need to keep inside
your website server. This code will connect to your MySQL database and insert or delete RFID
data. Edit this code and provide your MySQL database credential as below:
Above config.php file actually contains the MySQL DB credentials in our server as below:
7. Edit the file script.js and provide your website URL of the php file post-esp32-generic.php as
below:
8. Now we are ready with our all web files inside "data" folder and we will move these files to our
ESP32. Close the serial monitor if opened, click on Tools => ESP32 Sketch Data Upload. This
will move all our files under "data" folder into ESP32 FILESYSTEM. See below:
Note: If you don’t have ESP32 Sketch Data Upload plugin installed, you MUST follow our SPIFFS
session first then try again.
9. Next, upload the code ESP32_RFID_Live_Status.ino into ESP32 by pressing upload button.
10. Great! We have uploaded all the codes and necessary data files into our ESP32. Only pending
activity is to create the MySQL database table on our webserver. For that, let's get connected
to our phpMyAdmin, select your database name, click on SQL tab, insert below query and click
Go to create the table. See below:
CREATE TABLE `imh_generic_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`record_num` int(11) DEFAULT NULL,
`record` varchar(1000) DEFAULT NULL,
CONSTRAINT imh_id_pk PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
11. That's it! We have finished all the activities and are ready to execute the code. Open the serial
monitor and press the RESET button on ESP32. You will see the IP address of your ESP32. Open
the web browser and go to the IP address. In my case it is http://192.168.0.105. See below:
Now swipe your cards on the reader and observe the live information on the web dashboard. See
below:
To store the data into MySQL DB, check the check box "Check this to SYNC with Database also"
and swipe your card again and observe the results on your web dashboard and DB table both. See
below:
If you wish, you can delete the data from the web dashboard and MySQL DB both by click on delete
action. See below:
Congratulations IoT magician! Another feather in your cap! You are really learning great stuff
every day! Many more to go… Keep growing magicians!!